Updating deprecated react native code using componentDidUpdate

Viewed 29

I am updating some deprecated code in our react native app, and I when I replace this:

  UNSAFE_componentWillReceiveProps(newProps) {
    if(newProps.visit.status && this.props.visit.status != newProps.visit.status) {
      const { setParams } = this.props.navigation;
      setParams({
        status: newProps.visit.status,
      });
    }
  }

with this, using componentDidUpdate():

  componentDidUpdate(newProps) {
    if(newProps.visit.status && this.props.visit.status != newProps.visit.status) {
      const { setParams } = this.props.navigation;
      setParams({
        status: newProps.visit.status,
      });
    }
  }

... everything appears to work as expected. Since there is a conditional check in componentDidUpdate() I don't run into any issues with infinite loops.

While the static getDerivedStateFromProps() is mentioned in the react docs as well, I don't see why this would be a better option in this particular instance. Is there anything I'm missing, in terms of this specific implementation that would suggest getDerivedStateFromProps() would be a better option than componentDidUpdate()?

0 Answers
Related