I'm new to JS and React. Went through the documentation given for setState()
and found this here:
For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.
setState()
does not always immediately update the component. It may batch or defer the update until later.
How exactly does it decide which components are to be updated immediately and which are to be deferred?
More precisely, I have something similar to what is given below... Is it a bad idea to have one common setState()
outside and another one depending on the conditions? Would it be better performance-wise to do the second option given below even if it means that there'd be a repetition of code?
// First
this.setState({msg: 'Hello!'});
if (someCondition) {
this.setState({
res: true
});
} else {
this.setState({
res: false
});
}
// Second
if (someCondition) {
this.setState({
msg: 'Hello!'
res: true,
});
} else {
this.setState({
msg: 'Hello!'
res: false,
});
}