【问题标题】:Reset stack inside a tab on tab double press using React Navigation 5使用 React Navigation 5 在选项卡双击时重置选项卡内的堆栈
【发布时间】:2020-03-03 14:58:27
【问题描述】:

我想知道如果 Tab 被聚焦并按下,如何重置 BottomTabNavigator 内的堆栈。

这是我目前的代码:

const Stack = createStackNavigator<MainStackParams>()
const BottomTab = createBottomTabNavigator<TabNavigatorParams>()
const navigationRef = React.useRef()

> Blockquote

<NavigationContainer ref={navigationRef}>
    <Stack.Navigator mode="modal">
            <Stack.Screen
                name={MainAppRoute.BOTTOM_TAB_NAVIGATOR}
                component={BottomTabNavigator}
            />
            ...
    </Stack.Navigator>
</NavigationContainer>

function BottomTabNavigator() {
    return (
        <BottomTab.Navigator>
            <BottomTab.Screen
                name={TabNavigatorRoute.SOME_STACK}
                component={SomeStack}
                listeners={{tabPress: e => {

                    // TODO: Reset stack in current tab (unsure how to do this)

                }}}
            />
            ...
        </BottomTab.Navigator>
    )
}

在 TODO 中(在代码 sn-p 中)可能应该完成以下操作:

  • 从应用 NavigationContainer 获取 navigationRef
  • 检查选定的 BottomTab 是否获得焦点(以确定双击)
    • e.preventDefault
    • reset SomeStack(不确定如何在 BottomTabNavigator 中获取导航对象的堆栈)

有没有人能够做到这一点? 感谢所有答案:)

【问题讨论】:

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


    【解决方案1】:

    好吧,基本上你有两个选项来管理这个,第一个是检查导航状态,但我发现它只适用于 IOS 设备,android 不做任何事情。

    这段代码从这个 stacknavigator 导航到第一个屏幕

    <Tab.Screen name={`DeviceNavigatorTab`} component={DeviceNavigator} options={{
                tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_home-menu.png')} style={{width: 26, height: 26, tintColor}}/>,
            }} listeners={({ navigation, route }) => ({
                tabPress: e => {
                    if (route.state && route.state.routeNames.length > 0) {
                        navigation.navigate('Device')
                    }
                },
            })} />
    

    另一种解决方案,甚至更好,适用于 android 和 IOS 操作系统

    DeviceNavigatorTab 这是选项卡导航器的屏幕名称,{screen: 'Device'} 是子堆栈导航器中的第一个堆栈屏幕名称,希望对您有所帮助

    <Tab.Screen name={`DeviceNavigatorTab`} component={DeviceNavigator} options={{
                tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_home-menu.png')} style={{width: 26, height: 26, tintColor}}/>,
            }} listeners={({ navigation }) => ({
                tabPress: e => {
                    navigation.navigate('DeviceNavigatorTab', { screen: 'Device' });
                },
            })} />
    

    它实际上并没有重置导航器,它只是转到该选项卡导航器内的指定屏幕。

    【讨论】:

    • 谢谢卡莉。我一直在到处寻找这个(连续两天)。我更喜欢重置,但在我找到重置方法之前,此选项将起作用
    • 我找到了重置堆栈导航器的解决方案,因此您无法返回某个屏幕,这会将堆栈重置为 2 条路线并转到第二个屏幕,您只能从那里回到家,希望它有所帮助 this.props.navigation.reset({ index: 1, routes: [{name: Route.HOME}, { name: Route.VEHICLE_PENALTY_PRINT, params: { fine: data, carNr } }] });跨度>
    猜你喜欢
    • 2020-07-20
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2022-10-09
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多