【发布时间】:2019-12-12 04:37:03
【问题描述】:
我正在开发一个 React Native 应用程序,我正在从子组件调用父函数,它被调用但它不会改变状态。基本上我正在从子打开一个模态,并希望通过更改模态上的某些内容来更改父组件的状态并关闭模态。
这是父级:
constructor(props) {
super(props);
this._toggleModal = this._toggleModal.bind(this)
this.state = {
isActive: false}}
_toggleModal = async() => {
this.setState({ isModalVisible: !this.state.isModalVisible })
}
doSomthing(x) {
console.log(x)
this._toggleModal;
}
render() {
return (
<RateModal toggleCall1={this.doSomthing}/>
)}
这是孩子:
ratingCompleted = async(rating)=> {
console.log("Rating is: " + rating)
await this.props.toggleCall1(false)
}
在这里,当给出评分时,我从孩子那里获得了false 道具,但它不会改变父母的状态。如何解决?
【问题讨论】:
-
如果我这样做。_toggleModal();然后它显示未处理的承诺拒绝,并且 this._toggleModal() 不是一个函数。
-
你在构造函数中绑定了
doSomthing()吗? -
我已经更新了这个问题。我确实像更新的问题一样绑定。
-
不,你没有。你绑定了
_toggleModal,它甚至不需要绑定,因为它是一个箭头函数。需要绑定doSomthing或者变成箭头函数。
标签: react-native