使用深层链接进行导航-问题访问子操作和参数

时间:2019-06-18 13:06:54

标签: reactjs react-native react-navigation

我正在尝试使Deep Link与React Navigation一起工作。我的流程是:

用户点击“深层链接”  应用程式开启  在我的App Navigator中,我检查用户是否通过深度链接访问  我照做:

const App = createSwitchNavigator({
  MyDrawerNavigator: MyDrawerNavigator,
  AuthLoading: AuthLoadingScreen,
  Auth: AuthStack,
  Main: MainTabNavigator,
  Profile: ProfileNavigator,
  ActivityListNav: {screen: ActivityListNavigator,
                  path: 'activity'}, 
},{
  initialRouteName: 'AuthLoading',
})

const previousGetActionForPathAndParams = App.router.getActionForPathAndParams;

Object.assign(App.router, {
  getActionForPathAndParams(path, params) {
    //alert('path : ' + path + ' params : ' + params);
    const isAuthLink = path.startsWith('activity');
    //alert('isAuthLink : ' + isAuthLink);
    if (isAuthLink) {
      return NavigationActions.navigate({
        routeName: 'AuthLoading',
        params: { ...params, path },
      });
    }
    return previousGetActionForPathAndParams(path, params);
  },
});

const XYZApp = createAppContainer(App);

const prefix = 'https://XYZApp.com/referral/';

export default MainApp = () => <XYZApp uriPrefix={prefix} />;

控件已成功传递到AuthLoading组件。这就是我正在做的:

const params = this.props.navigation.state.params;

let action;

if (params && params.path) {
      const routeParams = params;
      const routeName = params.path;
      action = NavigationActions.navigate({ routeName: routeName, params: routeParams });
    }

    if (!userToken){
      this.fetchUserData();
    }else{
      this.props.navigation.dispatch(NavigationActions.navigate({
        routeName:'PhoneAuth',
        action,
      }))
      this.props.navigation.navigate('PhoneAuth')
}

这将控件成功移动到PhoneAuth组件。现在,根据“操作”中传递的内容,我需要进一步导航。但是我无法弄清楚如何从“行动”中提取价值。我尝试了以下操作:

const params = this.props.navigation.state.params;
const routeName = this.props.navigation.state.routeName;
const { navigation } = this.props;
const referrer = navigation.getParam('refereeName', 'NO-ID');
const childState = PhoneAuth.router.getStateForAction();

if (params && routeName){
 return this.props.navigation.navigate(routeName, params);
}

但是在这里,我得到的参数未定义,而routeName作为PhoneAuth,而不是从Deep Link传递的参数。请帮忙。

0 个答案:

没有答案