3 Matching Annotations
- Sep 2022
-
legacy.reactjs.org legacy.reactjs.org
-
this.setState((state, props) => ({ counter: state.counter + props.increment }));
This version of this.setState() takes a callback and passes the "previous" state as the first parameter. USE THIS to calculate with states, as state update asynchronously.
-
Do Not Modify State Directly For example, this will not re-render a component: // Wrong this.state.comment = 'Hello'; Instead, use setState(): // Correct this.setState({comment: 'Hello'}); The only place where you can assign this.state is the constructor.
Change state using setState() or with the hooks, changing the state out of this will NOT re-render the component.
-
State is similar to props, but it is private and fully controlled by the component.
States are PRIVATE.
-