[solved] React batch state changes

Is it possible to set multiple state variables simultaneously? I need to have all the variables change before the component updates. I could use another state flag to block shouldComponentUpdate, but I was hoping there was a more elegant solution.

I may be misunderestimating (lol) your question, but you can do this:

this.setState({ "foo": "1", "bar": "2", "baz": "3" });

This updates multiple state attributes, and leaves the other attributes you may have alone.

2 Likes

Thanks, great answer.

1 Like