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

react-provide-state

v1.0.6

Published

A simple higher order function for managing a small amount of React state

Downloads

18

Readme

react-provide-state

A simple higher order function for managing a small amount of state. It allows:

  1. Easy provision of state to a components without the need to implement the details with React's setState.
  2. Share states with multiple components.

Redux is recommended for managing main application state. However, there are times when you need to manage a small piece of state that is only relevant to a few components.

For example:

  1. Selected font size or color of a paragraph;
  2. Selected tabs, checkbox or radio buttons;
  3. Temporary text input ( while user typing );
  4. Controlling modal's open/close state;

These states are most likely UI states that only concerns a handful of components However, with that said, Redux is often a better solution for sharing states with multiple components.

Example

import provideState from 'react-provide-state';

const ComponentA = ({value, setValue}) => {
    return (<div>
        <span>Please enter something: </span>
        <input value={value} onChange={(e) => setValue(e.target.value)} />
    </div>);
}

const ComponentB = ({text, setText}) => {
    return (<div>
        <span>You have entered: </span>
        <span>{value}</span>
    </div>);
}

const stateConfig = {
    namespace: 'optional namespace',
    name: 'value',
    initValue: 'initial value',
};

export const ComponentAWithState = provideState(stateConfig)(ComponentA);
export const ComponentBWithState = provideState({...stateConfig, alias: 'text'})(ComponentB);

Live demo: demo/dist/index.html

How it works

  1. API: provideState([config])
  2. config is an object with the following fields:

Config fields|Description ---|--- name|The name of the state. It will be also used as one of the prop names past to the wrapped components. namespace|Optional namespace for avoiding name collision when providing multiple components with the same state name. By default, all components provided with same state name will share the the same state. You can choose to use a different namespace so that it is isolated from state in other namespace. If you do not provide a namespace, the default namespace of defaultNamespace is used. alias|Optional. The wrapped component will be past with two props. One of them share the same name of the state (e.g. value) with the state value; The other prop is a callback function in the format of set[Name] (e.g. setValue). If you want to use different names, you can use 'alias' config. For example, if alias is text, the two property past will be text and setText. initValue| Optional initial value. It can be array, object and any value.

Tips

  1. If you want to make sure your state name will not collide with ANY existing states, you can use a Symbol as the namespace. e.g. {namspace: Symbol(), name: 'text'};
  2. The wrapped component is responsible for calling the set[Name] function with new value when update of the state value is required;
  3. If you are sharing the same state amongst multiple components. It is recommended to create another higher order function for providing the same state. e.g.:
....

const stateConfig = {
    namespace: 'optional namespace',
    name: 'value',
    initValue: 'initial value',
};

const provideInputState = (component, alias) => provideState({...stateConfig, alias})(compoennt);

export const ComponentAWithState = provideInputState(ComponentA);
export const ComponentBWithState = provideInputState(ComponentB, 'text');