gpt4 book ai didi

javascript - React.js : Props not being received by child component

转载 作者:行者123 更新时间:2023-11-28 14:48:20 25 4
gpt4 key购买 nike

我试图将状态变量作为 Prop 传递给子组件,但子组件没有接收任何内容。我在某处看到状态变量不会立即更新,因此我已将调用以将两个子组件交换到 setState 回调函数中。这是一些代码..

父组件

//function called by app component to unmount from parent
handleLandingUnmount(enteredCode){
console.log(enteredCode + ' received in index.js');
//updating state to give enteredCode to be passed to Chatpage component in props
this.setState({sessionCode: enteredCode}, function(){
this.onSessionInState();
});
}

onSessionInState(){
console.log(this.state.sessionCode);
this.setState({renderLandingScreen:false});
this.setState({renderChatScreen:true});
}

render(){
//if app component has unmounted, check if chat screen is to be rendered and pass the code as a prop
const toRender = this.state.renderLandingScreen?<App unmount={this.handleLandingUnmount}/>:
this.state.renderChatScreen?<Chatpage sessionCode={this.state.enteredCode}/>:<h1>Something else could go here...</h1>;
return toRender;
}

聊天页面组件

constructor(props){
super(props);
console.log(JSON.stringify(props));
}

render(){
return(
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>{this.props.sessionCode}</h2>
</div>
</div>
);
}

预先感谢您的帮助。

最佳答案

当代码存储在 this.state.sessionCode 中时,您似乎正在传递 this.state.enteredCode

关于javascript - React.js : Props not being received by child component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45525615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com