the-road-to-learn-react / use-combined-reducers

Custom hook to combine all useReducer hooks for one global state container.
https://www.robinwieruch.de
MIT License
75 stars 9 forks source link

Do not re create dispatch on every call. #5

Open amsterdamharu opened 4 years ago

amsterdamharu commented 4 years ago

Dispatch has a different reference every time you call createDispatch. That is not how React.useReducer works and this should have consistant behavior.

Reference to issiue: https://github.com/the-road-to-learn-react/use-combined-reducers/issues/4

ahungrynoob commented 4 years ago

@amsterdamharu you can use https://github.com/ahungrynoob/use-root-reducer

Tsourdox commented 4 years ago

Why not just use the useRef hook to solve this? This PR seems overly complicated to me, maybe I'm missing something.

const { current: dispatch } = useRef(action =>
    Object.keys(combinedReducers)       
       .map(key => combinedReducers[key][1])        
       .forEach(fn => fn(action));
);