我正在使用 React Navigation v5 开发 React Native 应用程序,但我无法获得我想要的行为。
这是我想要的所有行为:
我应该如何处理这些要求?我面临着多重挑战 - 例如,当我从 B 导航到 C-2,将 C 和 C-2 作为目标屏幕传递时,标题后退按钮无法按预期工作。它带我回到 B 而不是 C-1。
这是代码...
创建两个堆栈:
const HomeStack = createStackNavigator();
const CStack = createStackNavigator();
创建父堆栈:
class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<NavigationContainer>
<HomeStack.Navigator
initialRouteName="B"
screenOptions={{
cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
}}>
<HomeStack.Screen name="A">
{props => (
<View>
<Text>A content</Text>
</View>
)}
</HomeStack.Screen>
<HomeStack.Screen name="B">
{props => (
<View>
<Button
title="Navigate to C"
onPress={() => {
// props.navigation.navigate('C')
// OR
// props.navigation.navigate('C', {screen: 'C-2'});
}}
/>
</View>
)}
</HomeStack.Screen>
<HomeStack.Screen name="C">
{props => <C {...props} />}
</HomeStack.Screen>
</HomeStack.Navigator>
</NavigationContainer>
);
}
}
创建嵌套堆栈:
class C extends Component {
constructor(props) {
super(props);
}
render() {
return (
<CStack.Navigator
initialRouteName="C-1"
screenOptions={{
headerShown: false,
cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
}}>
<CStack.Screen name="C-1">
{props => (
<View>
<Text>C-1</Text>
</View>
)}
</CStack.Screen>
<CStack.Screen name="C-2">
{props => (
<View>
<Text>C-2</Text>
<Button
title="Close C navigator"
onPress={() => props.navigation.navigate('B')}
/>
</View>
)}
</CStack.Screen>
<CStack.Screen name="C-3">
{props => (
<View>
<Text>C-3</Text>
</View>
)}
</CStack.Screen>
</CStack.Navigator>
);
}
}
感谢您的帮助!