反应本机。后退按钮重定向到主页

React native. Back button redirect to the Home page

我有一个包含多个组件的选项卡

const HomePageStack = createStackNavigator();
const HomePageScreen = () => (
    <HomePageStack.Navigator initialRouteName='Главная' screenOptions={{ headerShown: true, headerTransparent: true, headerTintColor: '#fff', headerTitleStyle: { color: 'rgba(0, 0, 0, 0)' }, }}>
        <HomePageStack.Screen name='Главная' component={Home} options={{ title: "Главная" }} style={{ height: '100%' }} />
        <HomePageStack.Screen name='Рецепт' component={Recipe} option={{ title: 'Рецепт' }} style={{ height: '100%' }} />
        <HomePageStack.Screen name='Аккаунт' component={Account} option={{ title: 'Аккаунт' }} style={{ height: '100%' }} />
    </HomePageStack.Navigator>
)

检查时,我是这样走的(使用 navigation.navigate 和数据)

首页 -> 菜谱 -> 账户 -> 菜谱

在我第二次切换到食谱之前,后退按钮工作正常

当我第二次进入 Recipe 并点击返回按钮时,我被转到主页,但我想在 Account

我没有自定义或更改后退按钮的工作方式。

重定向功能(从帐户到配方 2):

const handleRecipe = (_id) => {
    navigation.navigate('Рецепт', { _id })
}

我希望后退按钮按预期工作并将用户重定向到帐户而不是主页。我怀疑问题出在我的重定向功能上。

navigate 弹出回到堆栈中较早的位置,因为 Recipe 屏幕已经存在于那里。要将 Recipe 的另一个实例推送到导航堆栈,您可以改用推送操作。因此,在 Account 屏幕上,您使用推送操作而不是导航操作来避免返回到已经存在的 Recipe 屏幕。有关示例,请参阅 https://reactnavigation.org/docs/stack-actions/#push