Created
November 30, 2018 11:23
-
-
Save venil7/9f4acddbe75571d0dd8b3ffbb5aab1ef 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