A global state for React with Hooks API.
$ npm install react-hooks-shared-state --save
https://kelp404.github.io/react-hooks-shared-state
const {useSharedState} = require('react-hooks-shared-state');
const Component = () => {
const [state, setState] = useSharedState(null, {value: 'hello'});
const onChange = e => {
setState(Object.assign({}, state, {value: e.target.value}));
};
return (
<input value={state.value} onChange={onChange}/>
);
};
const {useSharedState} = require('react-hooks-shared-state');
const sharedState = require('react-hooks-shared-state');
sharedState.useState()
useSharedState
and sharedState.useState
are the same one.
const {setSharedState} = require('react-hooks-shared-state');
Assign a new stateful value.
Type: any
Required: required
const {useSharedState} = require('react-hooks-shared-state');
Returns a stateful value, and a function to update it.
Type: string
Required: optional
The object path of the state.
When the path is null it will return the state(root).
If you just want to use a part of state in the component, pass the object path.
Type: any
Required: optional
The initial state.