N
N
Nikita Gimon2019-03-24 20:17:07
React
Nikita Gimon, 2019-03-24 20:17:07

How to change the code so that the error does not appear?

componentWillReceiveProps = nextProps => {
        if(nextProps.auth.isAuthenticated){
            this.props.history.push('/dashboard')
        }
        if(nextProps.errors){
            this.setState({
                errors: nextProps.errors
            })
        }
    };

this error appears, please help;
×
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-03-24
@prod357

When the error property arrives, you update the component in an endless loop. The componentWillReceiveProps method itself is not recommended for use.
It is not clear why you need to write each new property value in the state of the same name, if you can use it directly?
Decision:

static getDerivedStateFromProps(props, state) {
  if (props.message) {
    return { message: props.message };
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question