Skip to content

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

Closed
bfink13 opened this issue Sep 24, 2015 · 8 comments
Closed

Valid to dispatch multiple actions from an event handler? #797

bfink13 opened this issue Sep 24, 2015 · 8 comments
Labels

Comments

@bfink13
Copy link

bfink13 commented Sep 24, 2015

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:

var item = this.props.items[index];
if (!item.error) {
    this.actionCreators.saveItem(item.name, item.value);
}

this.actionCreators.touchItem(item.name);

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?

@gaearon
Copy link
Contributor

gaearon commented Sep 24, 2015

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 name in an action, if the reducer already has access to this state by the virtue of managing it? It's best to only put ids or original input in the actions—not the state you read.

@gaearon
Copy link
Contributor

gaearon commented Sep 24, 2015

Also, check out redux-form which does most of this for you.

@bfink13
Copy link
Author

bfink13 commented Sep 24, 2015

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:

handleBlur = (event) => {
    var item = this.props.items[event.target.name];
    if (!item.error) {
        this.actionCreators.saveItem(event.target.name, item.value);
    }

    this.actionCreators.touchItem(event.target.name);
}

Does this make more sense?

@gaearon
Copy link
Contributor

gaearon commented Sep 24, 2015

I guess this confuses me:

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.

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.

@bfink13
Copy link
Author

bfink13 commented Sep 24, 2015

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.

@gaearon
Copy link
Contributor

gaearon commented Sep 24, 2015

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.

@bfink13
Copy link
Author

bfink13 commented Sep 24, 2015

I'll take it. Thanks for the help!

@robertleeplummerjr
Copy link

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

No branches or pull requests

3 participants