C
C
ckatala2020-01-19 01:00:44
React
ckatala, 2020-01-19 01:00:44

How to replace componentWillReceiveProps?

I'm getting a warning in the browser console
Warning: componentWillReceiveProps has been renamed, and is not recommended for use.
Please explain how to fix it on getDerivedStateFromProps
If possible clearly, in other cases I think I'll figure it out myself! Thanks
My component

constructor(props) {
    super(props)
    this.state = {
      query: '',
      toggle: 'off'
    }
  }
  componentWillReceiveProps(nextProps) {
    if (!nextProps.location.pathname.includes('/search')) {
      this.setState({
        query: '',
        toggle: 'off'
      })
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-01-19
@halalala

static getDerivedStateFromProps(nextProps){
   if(!nextProps.location.pathname.includes('/search')){
     return {
        query: '',
        toggle: 'off'
      };
  }
  else {
      return null;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question