Skip to content

Instantly share code, notes, and snippets.

@venil7
Created November 30, 2018 11:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save venil7/9f4acddbe75571d0dd8b3ffbb5aab1ef to your computer and use it in GitHub Desktop.
Save venil7/9f4acddbe75571d0dd8b3ffbb5aab1ef to your computer and use it in GitHub Desktop.
Using thunk with React hooks
const useThunk = (reducer, initState) => {
const [state, dispatch] = useReducer(reducer, initState);
const thunkDispatch = action => {
if (typeof action === "function") {
action(dispatch, () => state);
} else {
dispatch(action);
}
};
return [state, thunkDispatch];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment