React Navigation 5将参数传递给嵌套导航器中的屏幕

时间:2020-02-12 00:00:07

标签: react-native react-navigation react-navigation-bottom-tab react-navigation-v5

我们正在将React Navigation 4中的应用迁移到React Navigation 5。 该项目具有一个BottomTabNavigator,该BottomTabNavigator的其中一条路线是带有三个标签的嵌套TopTabNavigator。按下时,BottomTabNavigator中的一个选项卡导航到“排序”屏幕,您在其中选择要作为排序依据的排序参数,然后导航到“ Pending”路线,添加所选的排序参数,该路由应到达TopTabNavigator内部的三个“列表”屏幕之一。 目前,我正在这些屏幕中接收route.params为null,我认为这是因为中间有另一个导航器(topTabNavigator)。

BottomTabNavigator

    <BottomTabsNavigator.Navigator
      tabBar={props => <CustomTabBar {...props} />}
      initialRouteName="Pending"
    >
      <BottomTabsNavigator.Screen name="Pending" component={createTopTabsNavigator} />
      <BottomTabsNavigator.Screen name="Sort" component={SortScreen} />
      <BottomTabsNavigator.Screen name="Tab 3" component={Fragment} />
      <BottomTabsNavigator.Screen name="Tab 4" component={Fragment} /> 
      <BottomTabsNavigator.Screen name="Tab 5" component={Fragment} />
      <BottomTabsNavigator.Screen name="Filter" component={Fragment} />
    </BottomTabsNavigator.Navigator>

SortScreen标签:

    handleOnPress(item: { text: string; value: string; }): void {
        const {navigation} = this.props;

        const param = {orderBy: item.value};
        navigation.navigate('Pending', param);
    }

    render(){
        return(
            <View>
                <FlatList
                    data={orderOptions}
                    renderItem={({ item }) => (
                        <ListItem style={{ borderBottomWidth: 0 }} onPress={() => this.handleOnPress(item)}>
                            <Body>
                                <Text>{item.text}</Text>
                            </Body>
                        </ListItem>
                    )}
                    keyExtractor={item => item.text}
                />
            </View>
        );
    }

createTopTabsNavigator:

    <TopTabsNavigator.Navigator>
      <TopTabsNavigator.Screen name="List1" component={Screen1} />
      <TopTabsNavigator.Screen name="List2" component={Screen2} />
      <TopTabsNavigator.Screen name="List3" component={Screen3} />
    </TopTabsNavigator.Navigator>

非常感谢有此问题的人

1 个答案:

答案 0 :(得分:5)

navigation.navigate('Root', {
  screen: 'Settings',
  params: { user: 'jane' },
});

https://reactnavigation.org/docs/en/nesting-navigators.html#navigating-to-a-screen-in-a-nested-navigator