An open API service indexing awesome lists of open source software.

https://github.com/dioklecijan/react-use-model

How to use custom hooks in React to expose state, actions and state getters.
https://github.com/dioklecijan/react-use-model

hooks-api-react react reducer

Last synced: about 1 year ago
JSON representation

How to use custom hooks in React to expose state, actions and state getters.

Awesome Lists containing this project

README

          

How to use custom hook to expose state, actions and state getters.

```js
const useModel = () => {
const [state, dispatch] = useReducer(reducer, initState, initFunc);

return {
state,
actions: useMemo(() => mapDispatchToActions(dispatch), [dispatch]),
getters: useMemo(() => mapStateToGetters(state), [state]),
};
};

const App = () => {
const model1 = useModel(); // instance one with state, actions and getters.)
const model2 = useModel(); // instance two

const renderModel = (model, modelName) => {
const { state, actions, getters } = modelName;
return (


actions.inc()}>
Click to inc counter in model {num}



{JSON.stringify(state)}



isCounterEven: {getters.isCounterEven()}



actions.incLazy()}>
inc lazy {JSON.stringify(state)}



isLazyEven: {getters.isLazyEven()}



isCounterSmall: {getters.isCounterSmall}


);
};

...
}
```