简体   繁体   English

为什么在 componentDidUpdate 中使用 setState 在 React 中被视为不好的做法?

[英]Why is using setState inside componentDidUpdate seen as a bad practice in React?

I need to update a child's state when a prop gets updated, so I made something like this:当道具更新时,我需要更新孩子的状态,所以我做了这样的事情:

componentDidUpdate(prevProps) {
    const { searchValue, searchCriterion } = this.props;

    if (searchValue !== prevProps.searchValue) {
      this.setState({ searchValue });
    }
    if (searchCriterion !== prevProps.searchCriterion) {
      this.setState({ searchCriterion });
    }
  }

ESLint's airbnb guide is complaining about it and throwing this warning , even though I can't see any noticeable rendering or performance issues. ESLint 的 airbnb 指南正在抱怨它并抛出这个警告,即使我看不到任何明显的渲染或性能问题。

The warning says:警告说:

Updating the state after a component update will trigger a second render() call and can lead to property/layout thrashing.在组件更新后更新状态将触发第二次 render() 调用,并可能导致属性/布局抖动。

Is there a better way to update a child's state when a prop has a new value?当道具具有新值时,是否有更好的方法来更新孩子的状态?

Or should I ignore the warning?或者我应该忽略警告?

The problem of your code is that it might create an infinite loop.您的代码的问题是它可能会创建一个无限循环。 When you call the setState method, you trigger a second render , just like the warning says, and consequently a new componentDidUpdate .当您调用setState方法时,您会触发第二次render ,就像警告所说的那样,因此会触发一个新的componentDidUpdate

In order to solve this problem, you might want to create a break condition for certain values of your state to get out of your loop.为了解决这个问题,您可能希望为状态的某些值创建一个中断条件以退出循环。 For example, you could simply use a flag like so:例如,您可以简单地使用如下标志:

componentDidUpdate(prevProps) {
const { searchValue, searchCriterion } = this.props;

if (this.state.isAlreadyUpdated === true) {
  this.setState({ isAlreadyUpdated: false });
  break;
} else {
  this.setState({ isAlreadyUpdated: true });
}

if (searchValue !== prevProps.searchValue) {
  this.setState({ searchValue });
}
if (searchCriterion !== prevProps.searchCriterion) {
  this.setState({ searchValue });
}

} }

暂无
暂无

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

相关问题 在 componentDidUpdate 内反应 setState 导致无限循环 - React setState inside componentDidUpdate causing infinite loop componentDidUpdate() 内部的 setState() - setState() inside of componentDidUpdate() 在 React 中接受 setState 作为 function 参数是不好的做法吗? - Is it bad practice to accept setState as a function parameter in React? 在循环的每次迭代中使用 setState 是不好的做法吗? - Is using setState in every iteration of a loop bad practice? 如何修复“超出最大更新深度。在 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? 在 componentDidUpdate 错误中反应本机调用 setState - react native call setState within componentDidUpdate error 如何使用带有 componentDidUpdate 和 setState 的 react js 更新 state? - How to update state with react js with componentDidUpdate and setState? 在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState。 React 限制嵌套更新的数量以防止无限循环 - repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops 组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState。 React 限制了编号。 嵌套更新以防止无限循环 - component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the no. of nested updates to prevent infinite loops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM