easy-story-hooks
Based on the minimal global state management implemented by
react hooks
andEventTarget
, global states can be shared across components. High performance is as simple as usinguseState
.
Simple to use, it may be the easiest way to use the global state management tool!
There are only two steps to use, the global state is initialized, and the component state is bound to the global state in both directions. Compared to other global state management tools, using this library does not require much modification to the original code. Only the state warehouse is managed, and the method of modifying the global state is returned to the internal call of the component. Just as easy to use useState! I hope that there will be no more difficult global state management in the world.
Contrast with redux, it's very simple! Discard redux!
Based on
react hooks
andEventTarget
The global state can be used in any component, and the outermost layer of all components does not need to wrapcontext.provider
redux
is mainly composed ofstore
,action
,reducer
, etc., too large and complicated, cumbersome, too many useless refresh of components, low performance
Bind component state to global state
Global state change when component state changes Component state change when global state changes High performance, reducing useless component refreshes Instead of using context to refresh the component, use setstate to refresh only the individual components. If you use context, it will cause a useless refresh of a large number of components
How to get started
npm i easy-story-hooks -S
In your React reference
;
initState
The function
initState
is used to generate the initial value of the state and can be used multiple times. The parameter is anobject
, the key name is the global state name, and the key value is the global state initial value.
useStore
The function
useStore
is used to subscribe to the global state, and the component state is bound to the global state in both directions. The first parameter is astring
, which is the global state name. The return value is an 'Array`, which returns a stateful value and a function that updates it.
getState
The function
getState
is used to read the global state.
changeState
The component update status used to change the global state and notify all subscription statuses,The first parameter is the global state name, the second parameter is the updated state value, or the function returns a new state value.
API in Typescript
: [any, Function];function changeState(keyname: string, newvalue: any): void;function getState(): { key: string: any;};: { key: string: any;};
Basic grammar
Just as easy to use useState!
;const count setCount = ;
easy-story-hooks
;;const App = { const number setnumber = ; return <> <h1>number</h1> <button onClick=>Add one</button> </> };
changeState
Of course, you can use ;;const App = { const increment = { ; } const number setnumber = ; return <> <h1>number</h1> <button onClick=increment>Add one</button> </> };
Subcomponent reads global state
;;const Test = { let test = count const remove = { ; } return <> <h1>test</h1> <button onClick=remove>Subcomponent changes count</button> </> };