Error: 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.
每当我想在我的反应代码中更新我的状态时,我总是遇到这个错误
<块引用>状态
state = {
total: 0,
items:[10,20,30]
}
<块引用>
对项目求和并更新我的状态的函数
checkTotal() {
let { items, total } = this.state;
let itemTotal = itemObjectQuantity.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(total);
let totalObject = { total };
let newTotal = { total: itemTotal };
total = newTotal;
this.setState({ total });
// console.log(totalObject);
// console.log(newTotal);
答案 0 :(得分:0)
你正在改变状态,如果你想更新你的状态,请使用 setState 来这样做。 我不确定我是否理解你正在尝试做的事情。 如果您希望 newTotal 成为总状态的下一个值,则使用 this.setState({total: newTotal}),如果您希望 itemTotal 成为新值,则使用 this.setState({total: itemTotal}).< /p>