-
-
Save jloiola/810169c740b07d87c545a52ba3fadabe to your computer and use it in GitHub Desktop.
Using thunk with React hooks
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
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