Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 传递给子级的Prop值具有父级的最新值';s状态-反应_Reactjs - Fatal编程技术网

Reactjs 传递给子级的Prop值具有父级的最新值';s状态-反应

Reactjs 传递给子级的Prop值具有父级的最新值';s状态-反应,reactjs,Reactjs,我正在使用React,并将模式的值传递给孩子,如下所示: <Child mode={this.state.mode}/> 这就是我在我的父类中的内容我有: class Parent extends BaseComponent { state = { mode: 'mode 1', } updateModeState(mode) { this.setState({ mode: mode

我正在使用React,并将
模式的值传递给孩子,如下所示:

<Child mode={this.state.mode}/>
这就是我在我的父类中的内容我有:

class Parent extends BaseComponent {

    state = {
        mode: 'mode 1',
    }

    updateModeState(mode) {
        this.setState({
            mode: mode
        });
    }

    render() {
        return <Child updateModeState={this.updateModeState} eyemode={this.state.eyemode}/>
    }

}
如代码中的注释所示。我正在更新状态,但是道具没有更新。我认为道具总是与状态同步的,那么为什么道具在我设置状态后不立即更新呢

我还研究了Redux,以及在父类中创建另一个方法来返回state的值


我追求的选择可行吗?或者我提到的另外两个是更好的实践吗?

安装组件时,组件的支柱是固定的。安装后更改它们不会在安装的组件内更新它们

你有几种可能的方法来解决这个问题。首先看一下
组件willreceiveprops
getDerivedStateFromProps
挂钩。本文可能有助于理解它们:


基本上,您希望在childs状态下保存父母的道具并使用它。当父对象将新道具传递给子对象时,您希望检测并更新子对象的状态。这些挂钩将帮助您做到这一点。

安装组件时会设置组件的支柱。安装后更改它们不会在安装的组件内更新它们

你有几种可能的方法来解决这个问题。首先看一下
组件willreceiveprops
getDerivedStateFromProps
挂钩。本文可能有助于理解它们:


基本上,您希望在childs状态下保存父母的道具并使用它。当父对象将新道具传递给子对象时,您希望检测并更新子对象的状态。这些钩子将帮助您做到这一点。

一般信息:我不建议您直接发送所有状态对象或将作为对象更改的状态对象。React使用浅比较来了解哪些道具已更改。它不会检查对象的每个部分。浅比较,将检查对象的参考。您可以根据需要发送州的每个部分。它将被更新

您可以在组件中使用
getDerivedStateFromProps
。它是静态的 反应的生命周期方法。但每次道具改变都会触发。而且你仍然应该在生命周期方法中检查你新需要的道具,以避免不必要的重新渲染

作为代码片段:

class Child extends BaseComponent
{
  static getDerivedStateFromProps(newProps, currentState)
  {
    if (newProps.some !== currentState.some)
    {
      return ({ some: newProps.some }); // it will make change the state with new prop value
    }

    console.log(newProps); // also you can listen the changes for simple debugging

    return null; // if nothing changed
  }

....
}
官方文档:(不足以理解,但仍然)


另一方面,您可以为您的子组件设置一个键,如下所示:
一般信息:
我不建议您直接发送所有状态对象,或者将其更改为对象。React使用浅比较来了解哪些道具已更改。它不会检查对象的每个部分。浅比较,将检查对象的参考。您可以根据需要发送州的每个部分。它将被更新

您可以在组件中使用
getDerivedStateFromProps
。它是静态的 反应的生命周期方法。但每次道具改变都会触发。而且你仍然应该在生命周期方法中检查你新需要的道具,以避免不必要的重新渲染

作为代码片段:

class Child extends BaseComponent
{
  static getDerivedStateFromProps(newProps, currentState)
  {
    if (newProps.some !== currentState.some)
    {
      return ({ some: newProps.some }); // it will make change the state with new prop value
    }

    console.log(newProps); // also you can listen the changes for simple debugging

    return null; // if nothing changed
  }

....
}
官方文档:(不足以理解,但仍然)


另一方面,您可以为您的子组件设置一个键,例如
您是否绑定了作为prop发送给child的类方法?因为它将使用setState,它是父组件的函数,
this
需要知道它在哪个范围内。您是否绑定了作为prop发送给child的类方法?因为它将使用setState,这是父组件的功能,而
需要知道它在哪个范围内。
class Child extends BaseComponent
{
  static getDerivedStateFromProps(newProps, currentState)
  {
    if (newProps.some !== currentState.some)
    {
      return ({ some: newProps.some }); // it will make change the state with new prop value
    }

    console.log(newProps); // also you can listen the changes for simple debugging

    return null; // if nothing changed
  }

....
}