【发布时间】:2019-02-24 10:19:53
【问题描述】:
我已经阅读了this answer,但我想稍微改变一下代码的结构,
实际上我想从子组件设置父状态,但我不想在父组件中添加任何功能
实际上预期的结果是这样的:
class Parent extends React.Component {
constructor(props) {
super(props)
this.state={modalVisible:false}
}
render() {
return (
<Child modalVisible={this.state.modalVisible} />
<Button onClick={()=>this.setState({modalVisible:true})/>
)
}
}
class Child extends React.Component {
handler(e) {
//handle parent moadlVisible state to false again
}
render() {
return
<Modal
modalVisible = {this.props.modalVisible}>
<Button title="Close Modal" onPress={()=>this.handler(e)}/>
</Modal>
}
}
所以我想让调用child component 变得容易,而不需要在parent 中添加一些函数来处理child component 本身,即使关闭child component 的modal 也是如此
有没有办法实现我想要的?
【问题讨论】:
标签: react-native components state