React Native Navigation覆盖onBack

问题描述 投票:0回答:2

如何覆盖反应navigate.goBack()。我的目标是将数据从子传递到父级,用于更新父级。

这里有一个类似的解决方案react-navigation-goback-and-update-parent-state,但他们仍然称为this.props.navigation.goBack(),我希望能够使用标题栏中的后退按钮。

react-native react-navigation
2个回答
1
投票

您可以通过使用回调功能来实现此目的。

例如:

家长班:

在父类中添加回调函数,并使用props将该函数传递给子类。

...
...

//Navigating button 
 _didTapOnButton() {
    this.props.navigation.navigate("NextScreen", {
      callBackMethod: this._callBackMethod.bind(this)
    });
  }

//Call back method
_callBackMethod(dataFromChild) {
  //Your logic...
}

...
...

儿童班:

...
...

//Child class back button
 _didTapOnBackButton() {
   const { params } = this.props.navigation.state; // Take parameter from navigation
   params.callBackMethod('data 1'); // Call back function with data: 'data 1'
   this.props.navigation.pop();
 }
...
...

0
投票

使用带有componentWillUnmount的callBack解决了我的问题

© www.soinside.com 2019 - 2024. All rights reserved.