Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Javascript 如何使用React.js将状态属性传递给构造函数中的子组件?_Javascript_Reactjs_This_Bind_Prop - Fatal编程技术网

Javascript 如何使用React.js将状态属性传递给构造函数中的子组件?

Javascript 如何使用React.js将状态属性传递给构造函数中的子组件?,javascript,reactjs,this,bind,prop,Javascript,Reactjs,This,Bind,Prop,如何在构造函数中将父组件的状态属性作为子组件的道具传递?我有这样的想法: class FatherComponent extends Component { constructor(props) { super(props); this.state = { status: true components: [ { component: ( <ChildComponent stat

如何在构造函数中将父组件的状态属性作为子组件的道具传递?我有这样的想法:

class FatherComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      status: true
      components: [
        { component: (
            <ChildComponent
              statusAsProps={this.state.status} />
          )
        }
      ]
    };
  }

render(){
  return(
     <div>{this.state.components[0].component}</div>
  )
}
类父组件扩展组件{
建造师(道具){
超级(道具);
此.state={
状态:正确
组成部分:[
{组成部分:(
)
}
]
};
}
render(){
返回(
{this.state.components[0].component}
)
}
但显示了此错误。状态未定义。
是否有一种方法可以将状态的“this”绑定为子组件的道具?

在状态上存储组件不是一个好主意。您可以在
渲染中定义它

类父组件扩展组件{
建造师(道具){
超级(道具);
此.state={
状态:正确
};
}
render(){
返回(

{在状态中存储组件不是一个好主意。您可以在
渲染中定义组件

类父组件扩展组件{
建造师(道具){
超级(道具);
此.state={
状态:正确
};
}
render(){
返回(

{在定义和赋值之前,您正在使用
this.state
本身。这就是
未定义的
的原因。避免复杂性,只需从父组件的状态传递所需的道具即可渲染子组件

class FatherComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      status: true
    };
  }

  render() {
    return (
      <div>
        <ChildComponent statusAsProps={this.state.status} />
      </div>
    )
  }
}
类父组件扩展组件{
建造师(道具){
超级(道具);
此.state={
状态:正确
};
}
render(){
返回(
)
}
}

在定义和分配值之前,您正在使用
此.state
本身。这就是
未定义的
的原因。避免复杂性,只需通过从父组件的状态传递所需的道具来渲染子组件

class FatherComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      status: true
    };
  }

  render() {
    return (
      <div>
        <ChildComponent statusAsProps={this.state.status} />
      </div>
    )
  }
}
类父组件扩展组件{
建造师(道具){
超级(道具);
此.state={
状态:正确
};
}
render(){
返回(
)
}
}

这样做的情况是什么?为什么不能渲染组件并从州传递相关道具?这样做的情况是什么?为什么不能渲染组件并从州传递相关道具?