Closed
Description
Extend the standard useReducer
with a dispatcher that can handle "thunks" similar to redux-thunk
. It could look something like this:
export const useThunkReducer = (reducer, initialState) => {
const [state, dispatch] = useReducer(reducer, initialState);
const thunkDispatch = action => {
if (typeof action === "function") {
return action(dispatch, state);
}
return dispatch(action);
};
return [state, thunkDispatch];
}
Something similar was described in this article: https://medium.com/yld-engineering-blog/rolling-your-own-redux-with-react-hooks-and-context-bbeea18b1253
Activity