-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Valid to dispatch multiple actions from an event handler? #797
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
Comments
You can use redux-thunk which lets you both dispatch several times and read state in between these dispatches. In general, though, why do you need to put |
Also, check out redux-form which does most of this for you. |
Thanks for the quick reply! In this case I'm saving/touching onBlur. The code snippet above takes place within the onBlur handler. My example above is actually quite poor - here is an updated version:
Does this make more sense? |
I guess this confuses me:
Where is the outdated state? If you fire two actions in a row, they will synchronously update state, each one building on top of the most recent state. I'm not sure what's outdated here. |
Yes, you're right....I think I might be over thinking this. I'm also using react-redux - I was under the impression that render would be called every time an action is dispatched (and the redux state is set), but maybe that is a non-issue. |
This is correct, but it's not really a problem until you see it being a problem. You can also fire just one action, and have multiple reducers handling it. |
I'll take it. Thanks for the help! |
handled here I believe https://github.com/reactjs/redux/pull/1813/commits |
I'm in a situation where I'd like to dispatch two actions when a text box is blurred - I want to mark it as "touched" and also save it if there are no validation errors. I keep track of validation in the "change" reducer and only show the error message if a box has been "touched". I only want to save if there are no errors:
I'm using API middleware similar to the Real World example. It doesn't seem right to dispatch multiple actions in a single event handler since the second action will be working with state that is out of date.
I feel like I'm missing something simple, as this seems to be a pretty common scenario.
Does anyone have recommendations?
The text was updated successfully, but these errors were encountered: