use-combined-reducers icon indicating copy to clipboard operation
use-combined-reducers copied to clipboard

Do not re create dispatch on every call.

Open amsterdamharu opened this issue 5 years ago • 2 comments

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

amsterdamharu avatar Oct 30 '19 12:10 amsterdamharu

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

ahungrynoob avatar Nov 11 '19 06:11 ahungrynoob

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));
);

Tsourdox avatar Jun 28 '20 09:06 Tsourdox