【问题标题】:I'm getting this error Invariant Violation: Maximum update depth exceeded but can't find an infinite loop in my code我收到此错误 Invariant Violation: Maximum update depth exceeded 但在我的代码中找不到无限循环
【发布时间】:2019-08-12 20:14:14
【问题描述】:

我在网上发现此错误是由调用无限循环引起的,但我无法弄清楚具体是什么导致了该错误,如果更有经验的开发人员可以帮助我,这将是一个巨大的帮助我花了很多钱时间就可以了

还想知道我是否将代码的 javascript 部分放在了错误的位置

export default class App extends React.Component {

  constructor(props) {
     super(props);

     this.state = {
       soldPrice: 0,
       shippingCost: 0,
       shippingCharge: 0,
       itemCost: 0,
       profit: '',
       paypalFee: 0.30,
       paypalFeePercentage: 0.029,
       ebayFee: 0.1,
       profitMargin: '',
       paypalFeeTotal: '',
       ebayFeeTotal: '',
       profitColor: '',
       componentStyle: 'styles.inputStyleInactive',


     };
}


  render() {
     const { soldPrice, shippingCost, shippingCharge, itemCost,
       paypalFee, ebayFee, paypalFeePercentage,
      } = this.state;

     /*
       Define helper function that recovers a 0 as a default value
       if "non-a-number" results from parseFloat
       */
       const safeParseFloat = (str) => {
         const value = Number.parseFloat(str);

         return Number.isNaN(value) ? 0 : value;
       };

       let sp = safeParseFloat(soldPrice);
       const sc = safeParseFloat(shippingCost);
       const sCharge = safeParseFloat(shippingCharge);
       const ic = safeParseFloat(itemCost);
       const pf = safeParseFloat(paypalFee);
       const ef = safeParseFloat(ebayFee);
       const pfp = safeParseFloat(paypalFeePercentage);


      sp += sCharge;

      const calculation = sp - sp * pfp -
      pf
      - sp * ef - sc
      - ic;

      // if profit is more than 10 than the profit will be displayed as 00.00
      // otherwise it will be displayed as 0.00
      let i;

      if (calculation > 1000) {
        i = 6;
      } else if (calculation > 100) {
        i = 5;
      } else if (calculation > 100) {
        i = 3;
      } else if (calculation > 1) {
        i = 2;
      }

      const roundedNumber = calculation.toPrecision(i);
      this.setState({
       profit: roundedNumber
      });

      if (roundedNumber > 0) {
       this.setState({
         profitColor: '#26ae60'
       });
      } else if (roundedNumber < 0) {
       this.setState({
         profitColor: '#BA2F16'
       });
      }


      const ebayTotal = ef * sp;
      this.setState({
       ebayFeeTotal: ebayTotal
      });

      let p;

      if (ebayTotal > 1000) {
       p = 6;
      } else if (ebayTotal > 100) {
       p = 5;
      } else if (ebayTotal > 2.5) {
       p = 3;
      } else if (ebayTotal > 0) {
       p = 2;
      }

      const paypalTotal = pfp * sp + pf;
      this.setState({
       paypalFeeTotal: paypalTotal.toPrecision(p)
      });


      const profitPercentage = roundedNumber / ic * 100;
      this.setState({
      profitMargin: profitPercentage.toPrecision(i)
      });


   return (
     <View style={styles.container}>

     <View style={styles.header}>
     <Text style={styles.headerText}>Efees</Text>
     </View>

     <View style={styles.blocks}>

     <View style={styles.inputs} >
     <Text style={styles.inputText}>1. Sold Price</Text>

     </View>

     <View style={styles.inputs}>
     <Text style={styles.inputText}>2. Shipping Charge</Text>

     {/*<NumberInput
     onchange={(shippingCharge) => this.setState({ shippingCharge })}
     />*/}
     </View>

     <View style={styles.inputs}>
     <Text style={styles.inputText}>3. Shipping Cost</Text>

     {/*<NumberInput
     onchange={(shippingCost) => this.setState({ shippingCost })}
     />*/}

     </View>

     <View style={styles.inputs}>
     <Text style={styles.inputText}>4. Item Cost</Text>

     {/*<NumberInput
     onchange={(itemCost) => this.setState({ itemCost })}
     />*/}

     </View>

     <View style={styles.inputs}>
     <Text style={styles.inputText}>5. Ebay Store?</Text>

     {/*<NumberInput
     onchange={(itemCost) => this.setState({ itemCost })}
     />*/}

     </View>

     <View style={styles.inputs}>
     <Text style={styles.inputText}>6. Top Rated Seller?</Text>

     {/*<NumberInput
     onchange={(itemCost) => this.setState({ itemCost })}
     />*/}

     </View>
     </View>

     <View style={styles.results}>
      <Text style={styles.resultText}>eBay Fee:{this.state.ebayFeeTotal}</Text>
      <Text style={styles.resultText}>Paypal Fee:{this.state.paypalFeeTotal}</Text>
      <Text style={styles.resultText}>Profit %{this.state.profitMargin}</Text>
      <Text style={styles.profitResult}>
      TOTAL PROFIT:{this.state.profit}</Text>
      </View>

      <TouchableOpacity
      style={styles.calcButton}
      onPress={this.calculateProfit}
      >
         <Text style={styles.calcText}>Calculate </Text>
       </TouchableOpacity>

      </View>
 );
 }
}

【问题讨论】:

  • 图片是不必要且令人讨厌的——您还应该可以访问错误消息的文本,这样会更合适,而且要小得多。
  • 注意到我刚刚开始使用 stackoverflow 现在我知道未来

标签: reactjs react-native


【解决方案1】:

您在render 中设置状态,这几乎总是一个坏主意,可能会导致无限重新渲染。

您有很多处于状态的东西似乎不是有状态的,而是可能(或可能不需要)需要传递给子组件的简单局部变量——但不是有状态的,因为您的组件中没有任何内容除了 state 和 render

【讨论】:

  • 我是一个真正的菜鸟,你能帮我多拼一下吗,甚至可以把不应该出现在渲染中的代码移出并解释你为什么这样做
  • @prog 您的组件不需要如图所示的 任何 状态——一切都立即计算并使用。删除所有状态,将所有内容更改为 render 中的本地变量。
  • 我建议关注Thinking in React => 删除所有状态,只使用let & const 变量(步骤2),然后再决定是否需要交互(步骤3)跨度>
  • 感谢您抽出宝贵时间将其拼写出来,并检查一下反应中的思考
【解决方案2】:

您已将 setstate 放入渲染中,理想情况下应该是纯函数。您可以在类中添加一些函数,并且在组件生命周期中,您可以触发所有设置状态的方法一次,这些方法现在正在渲染方法中完成。

在 render 方法中调用 setState 方法会导致无限循环。

https://github.com/facebook/react/issues/5591

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多