javascript-performUpdateIfNecessary:意外的批号

在componentWillReceiveProps函数内部,我调用了另一个函数:

componentWillReceiveProps(nextProps){

  if(nextProps.cart){
    var cart = nextProps.cart;
    this.setState({cart:cart,
      offCart:cartHelper.getOffCart(cart),
      totalPay:cartHelper.getTotalPayCart(cart)});

      this.useFinance()//at this line makes this error

  }

  useFinance(){

    if(this.state.useFinance)
    {
      this.setState({useFinance:false})
    }else{
      if(this.state.totalPay > this.state.user.user.finance)
      {
        this.setState({useFinance:false})
      }else{
        this.setState({useFinance:true})
      }
    }

  }

我正在使用react-redux,并且应该在购物车更新时调用useFinance函数.

我收到此错误消息:

warning.js?8a56:33 Warning: performUpdateIfNecessary: Unexpected batch number (current 2, pending 1)

解决方法:

如果值仍然相同,则不要每次在componentWillReceiveProps中调用setState.检查从nextprops接收的值和状态内的实际值(如果它们不相等),然后调用setState.

componentWillReceiveProps(nextProps) {

    if (nextProps.cart && nextProps.cart != this.props.cart) { //Do this in case of object => JSON.stringify(nextProps.cart) != JSON.stringify(this.props.cart)
        var cart = nextProps.cart;
        this.setState({
            cart: cart,
            offCart: cartHelper.getOffCart(cart),
            totalPay: cartHelper.getTotalPayCart(cart)
        });
        this.useFinance() //at this line makes this error

    }
}

useFinance() {

    if (this.state.useFinance) {
        this.setState({
            useFinance: false
        })
    } else {
        if (!(this.state.totalPay > this.state.user.user.finance)) {
            this.setState({
                useFinance: true
            })
        } 
    }

}
上一篇:JavaScript-导入jquery webpack反应盖茨比


下一篇:javascript-在JS或React中生成UUID