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

Allow applyMiddleware to be applied more than once #3451

Closed
ironcoconut opened this issue Jun 18, 2019 · 2 comments
Closed

Allow applyMiddleware to be applied more than once #3451

ironcoconut opened this issue Jun 18, 2019 · 2 comments

Comments

@ironcoconut
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

ApplyMiddleware can only be called once for a given store. If you have 2 instances of applyMiddleware in a single store, dispatch will not function as expected. Calling dispatch from the first group of applied middleware will not call middleware in the second batch. This is because the applyMiddleware function references a local dispatch function, rather than a single, canonical dispatch that lives on the store object.

What is the expected behavior?

I would argue the expected behavior is applyMiddleware can be applied several times. The docs are unclear on this point beyond stating it should be the first enhancer applied as the middleware may be asynchronous.

Allowing multiple invocations of applyMiddleware enables store enhancers that wrap different stores. Using the enhancer pattern, complex applications can be broken into smaller, composable pieces that are easier to test and reason about. This pattern requires that each enhancer can separately call applyMiddleware.

Which versions of Redux, and which browser and OS are affected by this issue? Did this work in previous versions of Redux?

Based on the history of the appllyMiddleware file, all versions of redux are affected. But because most people don't call applyMiddleware more than once, it probably has never come up before.

@markerikson
Copy link
Contributor

This is covered in the FAQ:

https://redux.js.org/faq/store-setup#is-it-ok-to-have-more-than-one-middleware-chain-in-my-store-enhancer-what-is-the-difference-between-next-and-dispatch-in-a-middleware-function

We can try to make that more clear when we rewrite the docs (#3313 ), but no, I don't see any reason to allow applying it more than once.

@ironcoconut
Copy link
Author

Hey that's great. Glad for the clarification.

Yes, please update the docs as I couldn't figure out if this was the intended behavior or a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants