New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-provide-state

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-provide-state

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

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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 fieldsDescription
nameThe name of the state. It will be also used as one of the prop names past to the wrapped components.
namespaceOptional 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.
aliasOptional. 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.
initValueOptional 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');

Keywords

FAQs

Package last updated on 13 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc