static getDerivedStateFromProps (props, state)


  • administrators

    React 16.3

    getDerivedStateFromProps is being added as a safer alternative to the legacy componentWillReceiveProps.

    1. static prefix is needed
    2. it will be called every time local state changes as well.
    3. this is useful when your props is async generated.

    To make it only runs initially, you can do this:

    static getDerivedStateFromProps(props, state) {
        if (props.opportunities !== state.opportunities && !state.opportunities.length) {
          return Object.assign(state, {opportunities: props.opportunities})
        }
        return null;
      }