首页 > 解决方案 > 重定向到子/大子页面后如何继续执行父函数 - React Native

问题描述

我是 React Native 的新手,在 JS 方面也不是很强大,所以请在这种情况下帮助我并帮助我。

我有一个页面 A。当点击一个开关时,它会触发一些功能,比如说

父页面 A - enableInsurance

function switch(insuredID){
   if (noCreditCard){
     navigator.navigate('paymentPage',{navigateKey:"ObjectsDetailPage"}); //Takes user to payment page to choose payment mode
   }
   
   /* On re-navigation after adding CreditCard I need to continue doing below lines */
   object.isInsured = true;
   const { updateAction } = this.props;
   updateAction();
   this.setState({ showFlatrateText: false });
}

在我们的场景中,没有信用卡,因此用户被导航到paymentPage,他可以在其中选择支付方式,如 Card、paytm 等。

goToPaymentWebview = (Type) => {
  NetInfo.fetch()
    .then(resp => { 
       if(resp.isConnected){
            this.setState({showNetDownModal:false});
            NavigationService.navigate('paymentWebview',{navigateTo: this.props.navigation.getParam('navigateKey'));
        } else {
            this.setState({showNetDownModal:true});
        }
     });        
  }

选择模式后,用户将被带到大型子页面 - paymentWebview并加载到 webview 中。

用户填写详细信息并提交后,我会在 webview 中详细信息提交的成功或失败回调将用户导航回父页面 A ,如下所示

大子页面 - paymentWebview

if(respJSON.status === 'success') {
   const { navigate } = this.props.navigation;
   setTimeout(() => {
      switch(this.state.navigateTo){
         case "ObjectsDetailPage":
             navigate('enableInsurance');
         break;
         case "ObjectPage":
             navigate('ObjectPageScreen');
         break;
         default:
             navigate('Second',{ccUpdate: respJSON});
       }
    }, 2000);
 }

现在我回到了使用navigateKey参数重定向到支付页面的页面,在我们的例子中是它的父页面 A - enableInsurance

但是在导航回到这里之后,我如何执行相同的功能 switch(insuredID){ ... } 并继续使用该功能

为 iOS 和 Android 创建应用程序,导航使用 Stacknavigator ,没有标签导航。

标签: javascriptreact-nativenavigationreact-navigationstack-navigator

解决方案


推荐阅读