3 Matching Annotations
  1. Sep 2022
    1. 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.

    2. 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.

    3. State is similar to props, but it is private and fully controlled by the component.

      States are PRIVATE.