Skip to content

Instantly share code, notes, and snippets.

@0x04
Created May 4, 2020 01:03
Show Gist options
  • Save 0x04/7fc22f7ec682909e691c5394a8453466 to your computer and use it in GitHub Desktop.
Save 0x04/7fc22f7ec682909e691c5394a8453466 to your computer and use it in GitHub Desktop.

Collected/created React code snippets

// @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