您的位置:首页 > Web前端 > React

React子组件怎么改变父组件的state

2019-10-25 11:00 1691 查看
原文链接:https://www.mk2048.com/blog/blog.php?id=h012221habaa&title=React%E5%AD%90%E7%BB%84%E4%BB%B6%E6%80%8E%E4%B9%88%E6%94%B9%E5%8F%98%E7%88%B6%E7%BB%84%E4%BB%B6%E7%9A%84state

React子组件怎么改变父组件的state

 

1.父组件

class Father extends React.Component {

construtor(props){
super(props);
this.state={
isRed: 0
}
}
onChangeState(isTrue){
this.setState(isTrue)
}
render(){
<p>颜色:{this.state.isRed}</p>
<Child onClicked={this.onChangeState.bind(this)}/>
}
}
[/code]

  

2.子组件

class Child extends React.Component {
render(){
<Button onClicked={()=>this.props.onClicked({isRed: 1})}/>
}
}
[/code]

  


更多专业前端知识,请上【猿2048】www.mk2048.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: