Created
May 4, 2020 01:03
-
-
Save 0x04/7fc22f7ec682909e691c5394a8453466 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @see: https://www.robinwieruch.de/redux-with-react-hooks | |
const useCombinedReducer = combinedReducer => | |
{ | |
const entries = Object.entries(combinedReducer); | |
// Global state | |
const state = entries | |
.reduce( | |
(current, [ key, [ state ] ]) => ({ ...current, [key]: state }), | |
{} | |
); | |
// Global dispatch function | |
const dispatch = action => entries | |
.forEach(([ , [ , fn ] ]) => fn(action)); | |
return [ state, dispatch ]; | |
} | |
export default useCombinedReducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment