React本机导航参数不适用于Switch导航器

时间:2019-06-25 18:36:30

标签: react-native react-native-navigation

我在代码中使用react native导航和redux。 下面是我的导航结构

const MainTabNavigator = createBottomTabNavigator({
    Home,
    Tenants,
    WorkOrders: {
      screen: WorkOrders,
      navigationOptions: {
        title: 'Work Orders',
        headerTitle: 'Work Orders'
      }
    }
}, {
  navigationOptions: ({ navigation }) => {
      const { routeName } = navigation.state.routes[navigation.state.index];
      return {
        headerTitle: routeName
      };
  }
});

const MainStackNavigator = createStackNavigator({
    MainTabNavigator
}, {
  defaultNavigationOptions: ({ navigation }) => {
      return {
        headerLeft: (
          <Icon
            style={{ paddingLeft: 10 }}
            onPress={() => navigation.openDrawer()}
            name="md-menu"
            size={30}
          />
        )
      };
    }
});

const AppDrawerNavigator = createDrawerNavigator({
  Menu: {
    screen: MainStackNavigator
  }
});

const AppSwitchNavigator = createSwitchNavigator({
  Login: { screen: Login },
  Main: { screen: AppDrawerNavigator }
});

然后,我有一个操作文件,该文件从“登录”导航到主页。

async
...
console.debug(response.data);  // Here the console shows value 10
NavigationService.navigate('Main', { userID: response.data });

在HomeScreen页面中,我尝试查看参数值:

const { navigation } = this.props;
const userID = navigation.getParam('userID', '0');
console.debug(userID); // HERE THE VALUE IS 0, BUT SHOULD BE 10

我做错了什么?谢谢

1 个答案:

答案 0 :(得分:1)

您无法使用SwitchNavigator传递参数,这在文档中显然是有意的:

  

默认情况下,它不处理后退动作,并且将路由重置为   离开时的默认状态。

如果您确实需要在屏幕之间共享对象,则应使用redux而不是react-navigation