npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unstated-props

v1.0.1

Published

Provides access to your Unstated containers from component props.

Downloads

9

Readme

unstated-props

NPM travis-ci

A higher-order component that enables access to your unstated containers from component props. No need to set up a Provider or Subscribe wrappers.

👋 Hi there!

unstated-props is an abstraction layer on top of unstated by @jamiebuilds: https://github.com/jamiebuilds/unstated

This is a higher-order component that enables access to your unstated containers from component props. No need to set up a Provider or clunky Subscribe wrappers. This one is a humble attempt to make a masterpiece like unstated even better. ✨

🐣 < 990 bytes when used directly in the browser ⚛️ React / preact compatible (thanks to preact-compat)

Installation

To install the stable version:

npm install --save unstated-props

This assumes you are using npm as your package manager.

Usage

Since unstated-props is an extension on top of unstated, we create Containers as we would normally do. It's recommended to put all of your containers into a single folder. Your application structure would look similar to the following:

src/
  app.jsx
  components/
    Button.jsx
    Menu.jsx
    ...
  containers/
    PlaylistContainer.js
    SettingsContainer.js
    ...
    index.js

Step 1. Put unstated containers into "containers" folder

Your containers are the original unstated containers:

// containers/PlaylistContainer.js
// A good old unstated Container, simple and easy to read.

import { Container } from 'unstated';

class PlaylistContainer extends Container {
  state = { shuffleMode: false };

  shuffle = async () => {
    await this.setState({ shuffleMode: true });
    console.log(this.state.shuffleMode); // true
  };
}

export default PlaylistContainer;

Step 2. Create index.js in the "containers" folder

Then you need to create the index.js file in your containers/ folder. This is a place where you import unstated-props. You can organize it in a way similar to this:

// containers/index.js
// The major trick happens here.

import { connect } from 'unstated-props';
import PlaylistContainer from './PlaylistContainer';
import SettingsContainer from './SettingsContainer';

// "playlist" and "settings" will become available
// through "this.props.containers" in your components
export default connect({
  playlist: PlaylistContainer,
  settings: SettingsContainer
});

Step 3. Set up the root component

Let's say that our demo app has the core component named App. This is the root of the whole thing.

We wrap it with a root flag:

// Root component "src/app.jsx".
// TLDR: don't forget about the root flag! 😉

import React from 'react';
import withContainers from './containers';

const App = () => (
  <div>
    Many different things will be added here.
  </div>
)

export default withContainers(App, { root: true })

Step 4. Use containers where needed

Now we can access containers from our components. It's really easy and the end result looks great:

// components/Menu.jsx

import React from 'react';
import withContainers from '../containers';

const Menu = ({ containers: { playlist }}) => (
  <button onClick={()=> { playlist.shuffle() }}>
    Shuffle mode: {playlist.state.shuffleMode}
  </button>
)

export default withContainers(Menu);

That's it. Enjoy! ❤️

Feel free to star this repo and follow me on Twitter:

Github Twitter URL

Related


MIT License, 2019. Max Makarochkin