Skip to content

dioklecijan/react-use-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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 (
      <div>
        <div onClick={() => actions.inc()}>
          <u>Click to inc counter in model {num}</u>
        </div>
        <hr />
        <div>{JSON.stringify(state)}</div>
        <hr />
        <div>isCounterEven: {getters.isCounterEven()}</div>
        <hr />
        <div onClick={() => actions.incLazy()}>
          inc lazy {JSON.stringify(state)}
        </div>
        <hr />
        <div>isLazyEven: {getters.isLazyEven()}</div>
        <hr />
        <div>isCounterSmall: {getters.isCounterSmall}</div>
      </div>
    );
  };
  
  ...
}

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors