#reactjs #react-native #react-navigation #react-navigation-drawer
Вопрос:
Вот демонстрация моей проблемы: https://snack.expo.dev/xtklvo6eM
Я хочу перейти от ScreenOne -> ScreenTwo -> ScreenThree -> GoBack() к ScreenTwo….
Но вызов navigation.GoBack() на ScreenThree возвращает меня к ScreenOne, а не ScreenTwo.
Может кто-нибудь сказать мне, почему это так?
import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
function ScreenOne({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={() => navigation.navigate('ScreenTwo')}
title="Go to screen two"
/>
</View>
);
}
function ScreenTwo({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.navigate('ScreenThree')} title="Go to screen three" />
</View>
);
}
function ScreenThree({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()} title="Try to go back to screen two.." />
</View>
);
}
const Drawer = createDrawerNavigator();
export default function App() {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="ScreenOne">
<Drawer.Screen name="ScreenOne" component={ScreenOne} />
<Drawer.Screen name="ScreenTwo" component={ScreenTwo} />
<Drawer.Screen name="ScreenThree" component={ScreenThree} />
</Drawer.Navigator>
</NavigationContainer>
);
}
Ответ №1:
backBehavior="history"
https://reactnavigation.org/docs/drawer-navigator#backbehavior