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(){
返回(
)
}
}
这样做的情况是什么?为什么不能渲染组件并从州传递相关道具?这样做的情况是什么?为什么不能渲染组件并从州传递相关道具?