componentWillReceiveProps method

  1. @override
  2. @mustCallSuper
  3. @Deprecated('Only supported in the deprecated Component, and not in Component2. See doc comment for more info.')
void componentWillReceiveProps(
  1. Map nextProps
)
inherited

ReactJS lifecycle method that is invoked when a Component is receiving new props (nextProps).

DEPRECATED - DO NOT USE

This will be removed along with Component in a future major release

Depending on your use-case, you should use getDerivedStateFromProps or getSnapshotBeforeUpdate instead. (See the examples below if you're not sure which one to use)

If you used componentWillReceiveProps to update instance state, use getDerivedStateFromProps

// Before
@override
componentWillReceiveProps(Map nextProps) {
  if (nextProps['someValueUsedToDeriveState'] != props['someValueUsedToDeriveState']) {
    setState({'someDerivedState': nextProps['someValueUsedToDeriveState']});
  }
}

// After
@override
getDerivedStateFromProps(Map nextProps, Map prevState) {
  // NOTE: Accessing `props` is not allowed here. Change the comparison to utilize `prevState` instead.
  if (nextProps['someValueUsedToDeriveState'] != prevState['someDerivedState']) {
    // Return a new Map instead of calling `setState` directly.
    return {'someDerivedState': nextProps['someValueUsedToDeriveState']};
  }
  return null;
}

If you did not use componentWillReceiveProps to update instance state, use getSnapshotBeforeUpdate

// Before
@override
componentWillReceiveProps(Map nextProps) {
  if (nextProps['someValue'] > props['someValue']) {
    _someInstanceField = deriveNewValueFromNewProps(nextProps['someValue']);
  }
}

// After
@override
getSnapshotBeforeUpdate(Map prevProps, Map prevState) {
  // NOTE: Unlike `getDerivedStateFromProps`, accessing `props` is allowed here.
  // * The reference to `nextProps` from `componentWillReceiveProps` should be updated to `props` here
  // * The reference to `props` from `componentWillReceiveProps` should be updated to `prevProps`.
  if (props['someValue'] > prevProps['someValue']) {
    _someInstanceField = deriveNewValueFromNewProps(props['someValue']);
  }

  // NOTE: You could also return a `snapshot` value from this method for later use in `componentDidUpdate`.
}

Implementation

@override
@mustCallSuper
@Deprecated('Only supported in the deprecated Component, and not in Component2. See doc comment for more info.')
void componentWillReceiveProps(Map nextProps) => throw _unsupportedLifecycleError('componentWillReceiveProps');