N
N
nikmil2020-01-25 11:01:42
React
nikmil, 2020-01-25 11:01:42

How to fix Maximum update depth exceeded error in componentDidUpdate?

constructor(props) {
    super(props)
    this.state = {
      battery: this.props.data.battery,
      network: this.props.data.network
    }
  }
componentDidMount() {
    this.batteryCharge();
  }
  componentDidUpdate(prevProp, prevState) {
    if ( this.state.battery !== prevProp.data.battery) {
      this.batteryCharge();
    }
  }
batteryCharge() {
    switch (this.state.battery) {
      case 11:
        this.setState({ battery: '---' })
        break;
      case 10:
        this.setState({ battery: '----' })
        break;
 default:
        break;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question