React Native,React 导航,如何使用 Drawer 返回()到上一个屏幕,而不是第一个屏幕?
React Native, reactnavigation, how do I goBack() to the previous screen, not the first screen, using a Drawer?
这是我的问题的演示:https://snack.expo.dev/xtklvo6eM
我想从 ScreenOne -> ScreenTwo -> ScreenThree -> goBack() 转到 ScreenTwo....
但是在 ScreenThree 上调用 navigation.goBack() 会使我回到 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>
);
}
backBehavior="history"
https://reactnavigation.org/docs/drawer-navigator#backbehavior
这是我的问题的演示:https://snack.expo.dev/xtklvo6eM
我想从 ScreenOne -> ScreenTwo -> ScreenThree -> goBack() 转到 ScreenTwo....
但是在 ScreenThree 上调用 navigation.goBack() 会使我回到 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>
);
}
backBehavior="history"
https://reactnavigation.org/docs/drawer-navigator#backbehavior