简体   繁体   English

在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState。 React 限制嵌套更新的数量以防止无限循环

[英]repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops

componentDidUpdate(prevProps, prevState) {
const { data } = this.state;

if (prevState.data.date !== data.date) {
  const startDate = new Date(data.date.startDate);
  const endDate = new Date(data.date.endDate);
  const countDuration = new Date(endDate - startDate).getDate();
  this.setState({
    data: {
      ...this.state.data,
      duration: countDuration,
    },
  });
}

if (prevState.data.duration !== data.duration) {
  const startDate = new Date(data.date.startDate);
  const endDate = new Date(
    startDate.setDate(startDate.getDate() + +data.duration - 1)
  );
  this.setState({
    ...this.state,
    data: {
      ...this.state.data,
      date: {
        ...this.state.data.date,
        endDate: endDate,
      },
    },
  });
}

} }

But this gives me error, saying Maximum update depth exceeded.但这给了我错误,说超出了最大更新深度。 This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况。 React limits the number of nested updates to prevent infinite loops. React 限制了嵌套更新的数量以防止无限循环。

It looks like you're changing the data.date and data.duration properties in the wrong place.看起来您在错误的位置更改了 data.date 和 data.duration 属性。 As a result, the conditions prevState.data.date.== data.date and prevState.data.duration.== data.duration are never evaluating to true and thus the setState calls happen each time, causing an infinite loop结果,条件prevState.data.date.== data.dateprevState.data.duration.== data.duration永远不会评估为true ,因此每次都会发生setState调用,从而导致无限循环

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState。 React 限制了编号。 嵌套更新以防止无限循环 - component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the no. of nested updates to prevent infinite loops 在componentDidUpdate中调用函数。 错误:React限制了嵌套更新的数量 - Calling functions in componentDidUpdate. Error: React limits the number of nested updates 如何修复“超出最大更新深度。在 componentWillUpdate 或 componentDidUpdate 中调用 setState。” 反应错误? - How to fix “Maximum update depth exceeded.calls setState inside componentWillUpdate or componentDidUpdate.” error in react? 在componentWillUpdate或componentDidUpdate中反复调用setState? - Repeatedly calling setState inside componentWillUpdate or componentDidUpdate? 超出最大更新深度。 当组件在componentWillUpdate或componentDidUpdate中重复调用setState时,可能会发生这种情况 - Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 不变违反:最大更新深度..当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时 - Invariant Violation: Maximum update depth..when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 未捕获的错误:当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况 - Uncaught Error: This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 错误:超过最大更新深度。 当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况 - Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 错误:超出最大更新深度。 当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况 - Error: Max update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate ReactJs:错误:超出最大更新深度。 React 限制嵌套更新的数量以防止无限循环 - ReactJs :Error: Maximum update depth exceeded. React limits the number of nested updates to prevent infinite loops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM