Component Update

suggest change

These are the React 15 class update methods. componentWillReceiveProps and componentWillUpdate were later deprecated.

componentWillReceiveProps(nextProps)

This is the first function called when properties change.

When a component’s properties change, React calls this function with the new properties. You can access the old props with this.props and the new props with nextProps.

With these variables, you can compare the old and new props, or run a function because a property changed.

componentWillReceiveProps(nextProps){
  if (nextProps.initialCount && nextProps.initialCount > this.state.count){
    this.setState({
      count : nextProps.initialCount
    });
  }
}

shouldComponentUpdate(nextProps, nextState)

This is the second function called on properties changes and the first on state changes.

By default, if another component or your component changes a property or the state of your component, React will render a new version of your component. In that case, this function always returns true.

You can override this function and choose more precisely whether your component must update.

This function is mostly used for optimization.

If the function returns false, the update pipeline stops immediately.

shouldComponentUpdate(nextProps, nextState){
  return this.props.name !== nextProps.name ||
    this.state.count !== nextState.count;
}

componentWillUpdate(nextProps, nextState)

This function works like componentWillMount(). Changes aren’t in the DOM yet, so you can do some work just before the update is applied.

/!\ : you cannot use this.setState().

componentWillUpdate(nextProps, nextState){}

render()

There are some changes, so re-render the component.

componentDidUpdate(prevProps, prevState)

Same stuff as componentDidMount() : DOM is refreshed, so you can do some work on the DOM here.

componentDidUpdate(prevProps, prevState){}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents