Javascript 状态更新,但组件不响应
我在渲染函数中有下一个react代码:Javascript 状态更新,但组件不响应,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,我在渲染函数中有下一个react代码: {this.state.info} <MyComponent index={0} selectedInfo={this.state.info} /> {this.state.info} 如果我向MyComponent传递字符串,如: <MyComponent index={0} selectedInfo={
{this.state.info}
<MyComponent index={0}
selectedInfo={this.state.info}
/>
{this.state.info}
如果我向MyComponent传递字符串,如:
<MyComponent index={0}
selectedInfo={"Hello"}
/>
那我去看看你好。但是,如果我使用:
{this.state.info}
<MyComponent index={0}
selectedInfo={this.state.info}
/>
{this.state.info}
即使{this.state.info}打印Hello(即this.state.info=='Hello'),我也看不到任何内容。我怀疑这是与/react的渲染系统有关的问题,但我不知道如何跟踪这一点
开始时,this.state.info为“”。在axios调用大约需要2-3秒后,此状态会被更新,但MyComponent不会反映此更改,即使{this.state.info}显示hello(即this.state.info是通过axios调用使用setState更新的) 有什么建议吗?
Thx这可能是因为组件在道具更改时不渲染,而仅在其状态更改时渲染
解决方案可能是检查componentWillReceiveProps中的道具是否发生更改,如果应该进行渲染,则调用setState。这可能是因为组件在道具更改时不渲染,而仅在其状态更改时渲染
一个解决方案可能是检查componentWillReceiveProps中的道具是否发生了变化,如果应该进行渲染,则调用setState。很难说确切的问题是什么,因为您没有发布足够的代码……但这里有一个工作示例,我相信您正在尝试做的事情……最终就像其他人所说的那样,只需通过“this.setState()”更新状态,侦听组件将获取更新
constmycomponent=({selectedInfo,index})=>{
返回(
{selectedInfo}
{index}
)
}
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={selectedInfo:'initial',计数:0};
this.btnPressed=this.btnPressed.bind(this);
}
btnPressed(){
this.setState({selectedInfo:'按钮按下=>'+this.state.count,count:this.state.count+1});
}
render(){
返回(
状态转换工作
)
}
}
log(document.getElementById('app');
ReactDOM.render(,document.getElementById('app'))代码>
很难说确切的问题是什么,因为你没有发布足够的代码…但这里有一个工作示例,我相信你正在做你想做的事情…最终就像其他人说的那样,只需要通过“this.setState()”更新状态,监听组件就会接收更新
constmycomponent=({selectedInfo,index})=>{
返回(
{selectedInfo}
{index}
)
}
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={selectedInfo:'initial',计数:0};
this.btnPressed=this.btnPressed.bind(this);
}
btnPressed(){
this.setState({selectedInfo:'按钮按下=>'+this.state.count,count:this.state.count+1});
}
render(){
返回(
状态转换工作
)
}
}
log(document.getElementById('app');
ReactDOM.render(,document.getElementById('app'))代码>
此处是this.state.info
changing?开始时,this.state.info为空,但通过使用axios调用数据库执行componendddimount来更新此信息。此axios调用需要一些时间才能结束。但是在更新了this.state.info之后,MyMonent不会更新您是否可以使用setState
更新您的状态?React不会知道状态已更改,除非您使用setState
,并且直接更改它(this.state.info='something'
)不会导致重新渲染。我正在使用setState。此命令的结果是{this.state.info}打印Hello,但该状态值似乎没有传递给MyComponentwhere isthis.state.info
changing?开始时,this.state.info为null,但通过使用axios对数据库的调用执行Componendddimount来更新此信息。此axios调用需要一些时间才能结束。但是在更新了this.state.info之后,MyMonent不会更新您是否可以使用setState
更新您的状态?React不会知道状态已更改,除非您使用setState
,并且直接更改它(this.state.info='something'
)不会导致重新渲染。我正在使用setState。这个命令的结果是{this.state.info}打印Hello,但该状态值似乎没有传递给MyComponent@Juan门德斯,你说得对。答案已经更新了。在MyComponent的代码中,我使用了componentWillReceiveProps,并在componentWillReceiveProps中重新运行componentDidMount中的启动指令。我仍然需要了解更多关于react的信息。所有这些启动指令都是在更新了内部信息值的setState之后执行的MyComponent@Juan门德斯,你说得对。答案已经更新了。在MyComponent的代码中,我使用了componentWillReceiveProps,并在componentWillReceiveProps中重新运行componentDidMount中的启动指令。我仍然需要了解有关react的更多信息。所有这些启动指令都是在更新MyComponent中信息值的setState之后执行的