0

我正在开发一个 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从孩子那里获得道具,但它不会改变父母的状态。如何解决这个问题?

4

2 回答 2

1

尝试这个

doSomthing = (x) => {
  console.log(x)
  this._toggleModal();
}

您需要绑定此上下文或将函数更改为箭头函数

于 2019-08-04T09:25:48.067 回答
0

您需要调用该函数,

this._toggleModal()
于 2019-08-04T09:18:08.533 回答