Jeff Lee
@RebootJeff
Philosophy
Details
Why? Just imagine if it were synchronous instead.
Lots of diffing/updating (reconciliation) would be done with each setState call.
setState is asynchronous
updateX() {
this.setState({ x: this.state.x + 1 });
this.setState({ x: this.state.x + 1 });
}
Best Practices & Potential Guidelines
⚠️ Only when absolutely necessary
(triple-check the following during PR review)...
Best Practices & Potential Guidelines
setState with functions instead of objects
// broken
updateX() {
this.setState({ x: this.state.x + 1 });
this.setState({ x: this.state.x + 1 });
}
// works as intended
updateXWithFunctions() {
this.setState((prevState, props) => {
return { x: prevState.x + 1 };
});
this.setState((prevState, props) => {
return { x: prevState.x + 1 };
});
}
twitter.com/
github.com/
linkedin.com/in/
Find me at
and...