简体   繁体   English

为什么我在道具改变后得到旧的状态值

[英]Why I am getting the old state values after the props change

I just want to understand why in the application I have following situation, below is my constructor of class component:我只是想了解为什么在应用程序中我有以下情况,下面是我的类组件的构造函数:

  constructor(props) {
    super(props);
    this.state = {
      tableAlerts: props.householdAlerts,
      initialAlerts: props.householdAlerts
    }

    console.log('householdAlerts', props.householdAlerts)
  }

in render function I have:在渲染功能中,我有:

const { householdAlerts } = this.props;

My issue is that in constructor I got empty array, but in render funtion I have the data.我的问题是在构造函数中我得到了空数组,但在render函数中我有数据。 Is it possible to get the data in constructor?是否可以在构造函数中获取数据?

This is a very bad pattern when using the class component.在使用类组件时,这是一个非常糟糕的模式。 You are ignoring any props updates when you copy the value into state.当您将值复制到状态时,您将忽略任何道具更新。 to manage it:管理它:

  1. It requires you to manage two sources of data for the same variable: state and props.它要求您为同一个变量管理两个数据源:state 和 props。 Thus, you need to add another render each time your prop change by setting it into state (don't forget to test on equality from prev and next values to avoid being in an infinite loop).因此,您需要在每次道具更改时通过将其设置为状态来添加另一个渲染(不要忘记测试 prev 和 next 值的相等性以避免陷入无限循环)。

  2. You can avoid setting the state each time your props change by using the getderivedstatefromprops lifecycle method.您可以避免在每次 props 更改时设置状态,使用getderivedstatefromprops生命周期方法。

So the recommendation is: just use the props; do not copy props into state所以建议是: just use the props; do not copy props into state just use the props; do not copy props into state . just use the props; do not copy props into state

To learn more why you shouldn't, I highly recommend this article .要了解更多为什么不应该这样做,我强烈推荐这篇文章

It is not recommended to set your initial component state in the constructor like so because you gonna lose the ability to use { setState } method after to update this property/state.不建议像这样在构造函数中设置初始组件状态,因为在更新此属性/状态后您将失去使用 { setState } 方法的能力。

The best practice is indeed to refer directly to the prop with { this.prop.householdAlerts }, and keep the state usage for local (or in child components} cases.最佳实践确实是使用 { this.prop.householdAlerts } 直接引用 prop,并保留本地(或子组件)情况下的状态使用。

if anyhow you want to store props in component state for some reason, call it in lifeCycle -如果您出于某种原因想要将道具存储在组件状态中,请在生命周期中调用它 -


  componentDidMount() { 
        const { tableAlerts, initialAlerts } = this.props;
        this.setState({ tableAlerts, initialAlerts });
     }

Hagai Harari is right. Hagai Harari 是对的。 Nevertheless, your actual problem seems to be that during your initial rendering the array is empty.尽管如此,您的实际问题似乎是在初始渲染期间数组为空。 Can you ensure that the array has some items, when your component is rendered for the first time?当你的组件第一次被渲染时,你能确保数组有一些项目吗?

First rendering -> calls constructor第一次渲染-> 调用构造函数

<YourComponent householdAlerts={[]} />

Second rendering -> updates component第二次渲染-> 更新组件

<YourComponent householdAlerts={[alert1, alert2, alert3]} />

If you want initial state to have the prop value.Try something like this with 'this' keyword如果您希望初始状态具有 prop 值。尝试使用 'this' 关键字

  constructor(props) {
    super(props);
    this.state = {
      tableAlerts: this.props.householdAlerts,
      initialAlerts: this.props.householdAlerts
    }

    console.log('householdAlerts', props.householdAlerts)
  }

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

相关问题 为什么我没有得到更新道具价值的反应 - why I am not getting updated props value in react 为什么我得到 TypeError: _this.props.&quot;functionName&quot; is not a function ReactJS - Why am I getting TypeError: _this.props."functionName" is not a function ReactJS 为什么在尝试显示两个状态值的平均值时会得到 NaN? - Why am I getting NaN when trying to display average from two state values? 我正在使用 this.setState 更新我的状态,但我仍然在 render() 中收到旧值 - I am updating my state using this.setState but still i am recieving the old values in render() 为什么我的React孙子组件在收到其父组件的道具更改后没有更新? 或者:我需要使用状态吗? - Why does my React grandchild component not update after receiving a change in props from its parent? Or: do I need to use state? 在用JS编辑某些值后提交表单时,为什么会得到空值? - Why am I getting null values when submitting a form after editing some values with JS? 使用 for-each 和 props 更改 state 的值 - Change the values of state using for for-each and props 使用 API 值作为道具来更改 React 中的 State - Using API Values as props to change State in React CKEditor 在每次状态/道具更改后重新渲染 - CKEditor rerenders after every State/ Props Change 为什么我没有从反应 redux state 获得更新的值 - Why I am not getting the updated value from react redux state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM