【发布时间】:2017-05-17 09:22:17
【问题描述】:
我有一个显示项目列表的页面,当用户单击该特定项目时,会在 render() 中调用视图组件。
家长:
constructor(props) {
super(props);
this.state = {
showData : [],
view : false,
projectId: ''
};
this.buttonHandler = this.buttonHandler.bind(this);
this.back = this.back.bind(this);
};
// change view state to true to render diff component
buttonHandler(){
this.setState({view:true})
};
back(){
this.setState({view:true})
}
render(){
let compA = (
<Paper>
<List>
<Subheader >New
Projects</Subheader>
{this.state.showData.map(item =>
<div key={item.title}>
<ListItem onClick={()=>
this.buttonHandler()} leftAvatar=
{<Avatar icon={<Wallpaper />} />} primaryText=
"test" secondaryText="test" />
<Divider inset={true} />
</div>
)}
</List>
</Paper>
);
let compB = (
<ReviewProject
back={this.back}/>
);
return(
<div>
{this.state.view?compB:compA}
</div>
);
}
儿童补偿 B:
constructor(props) {
super(props);
this.state = {
//some code
};
}
//calls function back from parent which sets state the "view" to false
dismiss() {
this.props.back();
};
当子进程调用dismiss()函数返回列表组件compA时,会弹出警告:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the compB component.
有没有办法解决这个问题?以及从一个组件到另一个组件来回导航的好习惯是什么
【问题讨论】:
-
dismiss函数怎么调用,back函数的定义能不能添加
-
如果你可以使用
routes来渲染不同的组件,那就太好了。 -
我在 compB 调用了解除函数,然后将 compA 中的视图状态设置为 false。我已经重新编辑了
-
你在后面的函数中分配了true,你应该将它设置为false
-
@Panther,嗯,是的,但是如果我只想让父级的状态根据状态的值呈现组件,这就是我想要实现的。它正在工作,但它向我发出警告
标签: reactjs ecmascript-6