Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.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_State - Fatal编程技术网

Javascript React.js:子组件未接收道具

Javascript React.js:子组件未接收道具,javascript,reactjs,state,Javascript,Reactjs,State,我试图将状态变量作为道具传递给子组件,但子组件没有接收任何信息。我在某处看到状态变量不会立即更新,因此我将调用移动到setState回调函数中,以交换两个子组件。这里有一些代码 父组件 //function called by app component to unmount from parent handleLandingUnmount(enteredCode){ console.log(enteredCode + ' received in index.js'); //up

我试图将状态变量作为道具传递给子组件,但子组件没有接收任何信息。我在某处看到状态变量不会立即更新,因此我将调用移动到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>
    );
}
//应用程序组件调用的从父级卸载的函数
手动卸载(输入代码){
log(在index.js中输入的代码+’);
//正在更新状态以提供要传递给props中的Chatpage组件的输入代码
this.setState({sessionCode:enteredCode},function()){
this.onSessionInState();
});
}
onSessionInState(){
console.log(this.state.sessionCode);
this.setState({renderLandingScreen:false});
this.setState({renderChatScreen:true});
}
render(){
//如果应用程序组件已卸载,请检查是否要呈现聊天屏幕,并将代码作为道具传递
const toRender=this.state.renderLandingScreen?:
这个.state.renderChatScreen?:这里可能有其他东西。。。;
返回到接收器;
}
聊天页面组件

//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>
    );
}
构造函数(道具){
超级(道具);
log(JSON.stringify(props));
}
render(){
返回(
{this.props.sessionCode}
);
}

提前感谢您的帮助。

应该是this.state.sessionCode。您正在传递此.state.enteredCode,它应该是this.state.sessionCode。您正在传递此.state.enteredCode

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

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

您正在传递的
sessionCode
聊天页面
而不是
应用程序
,这就是应该是这样的。对不起,代码可能很模糊!应用程序组件已卸载,然后父组件将返回的代码作为道具传递到chatpage组件。请更新您的问题,不清楚哪个组件是which@GershonPapi更新!你正在将
sessionCode
传递到
Chatpage
而不是
App
这就是它应该的方式。对不起,代码可能很模糊!应用程序组件已卸载,然后父组件将返回的代码作为道具传递到chatpage组件。请更新您的问题,不清楚哪个组件是which@GershonPapi更新!非常感谢。这是漫长的一天哈谢谢你!这是漫长的一天哈哈