-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
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
"dispatch" passed to middlewares only passes through first argument #2501
Comments
Yeah, go ahead and make a PR for this. I'd be interested to see if there are any objections. |
Erm... potentially. This is very similar to the issue discussed in #1813 . As far as I know, all existing middlewares and enhancers assume that there's only one argument being passed to Out of curiosity, @Asvarox , what is your current use case? What does your custom middleware do? |
@markerikson sure. Given a dispatch is called with function as first argument and an array as second, it will // Written "by hand",this is the "core" of actual code
const middleware = ({ dispatch }) => next => (actionCreator, args) => dispatch(actionCreator(...args)); The reasoning is to make testing thunks easier. Ihave quite a few of these that dispatch other actions - by dispatching the result of calling another action creator. That forced me to assert what these action creators return - if one of them started to return another thunk instead of plain object, it broke the tests. More so it was then pretty hard to fix the assertion. The example from my project. I had an async action I couldn't find feasible solution online so I came up with a few. The middleware is one of the ones I wanted to experiment with (writing middlewares is fun ;) ) and then I found this little bug. |
@Asvarox the middlewareApi variable wraps the original dispatch function which is here: var middlewareAPI = {
getState: store.getState,
dispatch: (...args) => dispatch(...args)
} won't have any effect, since only the first argument is taken into account. |
Hey @eddyw that's not entirely true as later in Otherwise you would only be able to dispatch plain actions in middlewares (or pass the action through next-in-chain middleware) and not "start over". Dispatching thunk actions in thunks wouldn't be possible. |
@Asvarok yeah, you are right about it. I kind of overlooked that part. I guess passing the arguments to the function without hitting performance would be the challenge. |
Do you want to request a feature or report a bug? Bug
What is the current behavior?
Right now a "dispatch" function that's passed to middlewares passes through only the first argument.
From
applyMiddleware.js
:I have a custom middleware that accepts a dispatch call with 2 arguments. This works fine in, say,
mapDispatchToProps
(fromreact-redux
) as it usesstore.dispatch
, but indispatch
calls in redux-thunk, the second argument disappears.What is the expected behavior?
All the arguments are passed to the dispatch:
There are ways to work around it (you can put all the arguments in an array or object), additional arguments can also be stripped by any middleware in middlewares chain, but still it would make things more consistent.
If it makes sense I can submit a PR.
The text was updated successfully, but these errors were encountered: