Skip to content

Instantly share code, notes, and snippets.

@jloiola
Forked from venil7/useThunk.js
Created January 27, 2020 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jloiola/810169c740b07d87c545a52ba3fadabe to your computer and use it in GitHub Desktop.
Save jloiola/810169c740b07d87c545a52ba3fadabe 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