-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Middleware is broken #3019
Comments
The middleware init process was explicitly changed in 4.0 in the PR you linked. This was a deliberate design decision, and intended to resolve #1240. I'm not sure I have an immediate suggestion for your use case, other than to try changing your middleware's logic to wait for some manually-dispatched "store is ready" action. You could also consider staying with 3.x instead. |
@markerikson Yeah I've seen that issue. The key problem that the new design "solves" is this:
This is IMO, an implementation detail that the application developer should handle, not something that should break how most middleware works. With a warning instead of throwing an error, the application developer can identify the issue, and refactor their own middleware to address it. In its current state, all existing middleware, including 3rd party middleware, that uses this internal dispatch pattern (basically anything that needs to instantiate listeners on anything), is broken. Staying with 3.x isn't an option for us unfortunately, as we use Typescript, and only 4.x has the correct typings, and in general, we'd rather not be stuck with a legacy version of a core part of our application logic even if typings weren't an issue. And in my opinion, even the error message itself hints at how unimportant it really is: the "problem" is that It's important to note, the "fix" in redux right now breaks the code it was intended to fix too. It breaks the router middleware. Now authors of that middleware (and all other similar middleware) need to release 4.x compatible versions of their libs, which now need to expose the implementation details of a manually dispatched "store ready" action, which the consumer must implement. Use of middleware libraries has changed from simply importing the package, and inserting into the middlewares array, to now additionally require app authors to find some hacky way to dispatch an arbitrary "store ready" action some time after the store has finished being created, but before it is being used. And now ALL their middleware must hopefully adhere to the same action type, otherwise app authors will need to dispatch multiple "store ready" actions, with different types matching whatever API the middleware authors implemented. This is a real mess, and the "cure" is clearly way worse than the "disease" in this case. |
It's still possible to make every one of your examples work. Just dispatch after applyMiddleware. Perhaps in some sort of The Error was intentionally introduced as the alternative is to allow users to hit an edge case that has a very non-obvious cause (unless you're familiar with the internals of One alternative might be some sort of callback the middleware can register as a sort of |
Bug
An important middleware pattern has been broken. IMO the majority of middleware solutions encapsulate their own concerns and are independent of the existence of other middleware. A change was made recently to break a pattern that only caused a minor issue for a specific subset of middleware: those that exhibit different behaviour when interacting with other middleware during the
applyMiddleware
initialisation path.Here are some examples:
Another example:
Another example:
What is the current behaviour?
The following error is thrown during
applyMiddleware
, essentially breaking the application:Uncaught Error: Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch
Refactoring the above middleware to be closed over by the
next =>
function (like below) makes no difference (but is obviously not a viable solution either).What is the expected behavior?
Do not break the app. Log a warning if you want, so I can ignore it, because in 100% of my use cases I do not care if other middleware can handle that dispatch. I strongly suspect that there are (or should be) very few legitimate reasons why any dispatch from within a middleware context should ever interact with any other middleware in the stack, and if it did, I would consider that a code smell (it would place implicit order dependencies on middleware at the very least).
Which versions of Redux, and which browser and OS are affected by this issue? Did this work in previous versions of Redux?
Worked until this PR.
The text was updated successfully, but these errors were encountered: