I am setting multiple states in setState, but one of the states will not always change though. Is React smart enough to bypass setting that state it remains unchanged?
Asked
Active
Viewed 618 times
0
-
Can you provide a code sample of what you're talking about? – Sterling Archer Mar 07 '18 at 19:42
-
2Possible duplicate of [ReactJS - Does render get called any time "setState" is called?](https://stackoverflow.com/questions/24718709/reactjs-does-render-get-called-any-time-setstate-is-called) – Sulthan Mar 07 '18 at 21:10
1 Answers
2
No, by default React will update your component with every setState
call.
You can use shouldComponentUpdate
to let the component know that it shouldn't update when certain conditions are met.
shouldComponentUpdate(nextProps, nextState) {
// If these match, we know we don't need a redraw
if (this.state.foo === nextState.foo) return false;
// Otherwise, continue with the update
return true;
}
Note that shouldComponentUpdate
doesn't affect child-components, who will each check their own update conditions when they receive new props or new state.
You can read more about the React component lifecycle over here.

Etheryte
- 24,589
- 11
- 71
- 116