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:
- Easy provision of state to a components without the need to implement
the details with React's
setState
. - 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:
- Selected font size or color of a paragraph;
- Selected tabs, checkbox or radio buttons;
- Temporary text input ( while user typing );
- 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
- API:
provideState([config])
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
- 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'}
; - The wrapped component is responsible for calling the
set[Name]
function with new value when update of the state value is required; - 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');