Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useReducer middleware #164

Closed
wardoost opened this issue Mar 24, 2019 · 4 comments · Fixed by #375
Closed

useReducer middleware #164

wardoost opened this issue Mar 24, 2019 · 4 comments · Fixed by #375
Labels
enhancement New feature or request released

Comments

@wardoost
Copy link
Contributor

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

@streamich
Copy link
Owner

An interesting hook would be that allows to add any Redux middleware.

const useMyReducer = applyMiddleware(useReducer, [logging, thunks, ...]);

@wardoost wardoost changed the title useThunkReducer useReducer middleware Mar 25, 2019
@wardoost wardoost added the enhancement New feature or request label Mar 31, 2019
@wardoost wardoost mentioned this issue Apr 15, 2019
@wardoost
Copy link
Contributor Author

I have created a createReducer factory function the that takes middleware like Redux's applyMiddleware and returns a reducer that can be used like React's useReducer. While it looks handy to be able to use redux middleware, I am not sure if it's useful enough to add to this library.

Also, there are already a few repositories that have been doing Redux things with hooks (redux-react-hook, redux-hooks) but experienced issues and react-redux is working on a hooks API: reduxjs/react-redux#1179.

@wardoost
Copy link
Contributor Author

wardoost commented Apr 20, 2019

I noticed that getState always returns the current state which is problematic for redux middleware. With the current storybook example you can see the prev state and next state are the same which is incorrect behaviour:

Screen Shot 2019-04-20 at 12 11 22 pm

In order to fix this we need a getState function that always returns the correct state. As far as I know this is not possible with useReducer? This issue does not stop us from creating a useThunkReducer hook though.

streamich added a commit that referenced this issue May 24, 2019
streamich pushed a commit that referenced this issue Jun 4, 2019
# [9.4.0](v9.3.0...v9.4.0) (2019-06-04)

### Bug Fixes

* fix TypeScript error ([72e3036](72e3036))
* **docs:** syntax error, improve examples ([8bbe9d8](8bbe9d8))

### Features

* improve createReducer function ([6ba2d93](6ba2d93)), closes [#164](#164)
@streamich
Copy link
Owner

🎉 This issue has been resolved in version 9.4.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants