【问题标题】:onPress to navigate on a screen not navigatingonPress 在不导航的屏幕上导航
【发布时间】:2020-07-10 11:02:51
【问题描述】:

我是 React Native 的初学者,当我按下 Navigator 屏幕的 Button 时,我想在屏幕上导航,但它不起作用。

因为当我在TabOneStack.Screen 中按下Button 时没有发生任何事情,我不明白,我想导航到TabTwoScreen。我使用 React Navigation 5.6.1。

    const BottomTab = createBottomTabNavigator<BottomTabParamList>();
    export default function BottomTabNavigator() {
        return (
            <BottomTab.Navigator 
                initialRouteName="TabOne">
                <BottomTab.Screen
                    name="TabOne"
                    component={TabOneNavigator}
                />
            <BottomTab.Screen
                name="TabTwo"
                component={TabTwoNavigator}
                options={{
                    tabBarLabel: 'Autour de moi',
                    tabBarIcon: ({ color }) => <TabBarIcon name="ios-navigate" color={color} />,
                }}
            />
            </BottomTab.Navigator>
        );
    }
    
    const TabOneStack = createStackNavigator<TabOneParamList>();
    function TabOneNavigator() {
    return (
        <TabOneStack.Navigator>
            <TabOneStack.Screen
                name="TabOneScreen"
                component={TabOneScreen}
                options={({ navigation }) => ({
                    headerTitle: 'Rejoindre', headerRight: () => (
                        <Button onPress={() => navigation.navigate('TabTwoScreen')}
                            icon={
                                <Icon
                                    name='ios-log-in'
                                    type='ionicon'
                                    size={15}
                                    color="white"
                                />
                            }
                        />
                    ),
                })}
            />
        </TabOneStack.Navigator>
        );
    }

const TabTwoStack = createStackNavigator<TabTwoParamList>();

function TabTwoNavigator() {
    return (
        <TabTwoStack.Navigator>
            <TabTwoStack.Screen
                name="TabTwoScreen"
                component={TabTwoScreen}
                options={{ headerTitle: 'Autour de moi' }}
            />
        </TabTwoStack.Navigator>
    );
}

为什么当我按下TabOneStack.Screen 中的按钮时没有任何反应?

提前谢谢你

【问题讨论】:

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


    【解决方案1】:

    导航需要用到props,可以试试这个

    options={({ navigation }) => ({
                        headerTitle: 'Rejoindre', headerRight: props => (
                            <Button onPress={() => props.navigation.navigate('TabTwoScreen')}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多