gpt4 book ai didi

react-native - React Navigation 5 嵌套屏幕无法返回主屏幕

转载 作者:行者123 更新时间:2023-12-05 01:32:28 24 4
gpt4 key购买 nike

我在弄清楚嵌套的导航器时遇到了问题。我有一个带有这些按钮的 bottomTabNavigator:

Home
Stations
-> StationDetail (StationScreen)
Shows
-> ShowDetail (ShowScreen)

在我的主页选项卡上,我有一个水平滚动条,显示与电台页面相同的电台。

问题:当我使用此代码导航到嵌套导航器从主页导航到站点详细信息页面时,我无法导航回主/根 StationsScreen 选项卡

navigation.navigate('Stations', {
screen: 'Station',
station: props.station,
});

但是,如果我先导航到“站点”选项卡,然后返回主页,然后单击一个站点,它会按预期工作。我该如何解决这个问题?我见过像“重置”这样的东西,但我不确定把它放在哪里。这是我的代码的简化摘录:

AppStack.js:

import React from 'react';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';

import HomeStack from './HomeStack';
import StationStack from './StationStack';
import ShowStack from './ShowStack';

const Tab = createBottomTabNavigator();

const AppStack = () => {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeStack} />
<Tab.Screen name="Stations" component={StationStack} />
<Tab.Screen name="Shows" component={ShowStack} />
</Tab.Navigator>
);
};

export default AppStack;

HomeStack.js

import React from 'react';
import {View} from 'react-native';
import {createStackNavigator} from '@react-navigation/stack';
import TopBarRight from '../components/TopBarRight';
import HomeScreen from '../screens/HomeScreen';
import StationScreen from '../screens/StationScreen';
import {useNavigation} from '@react-navigation/native';

const Top = createStackNavigator();

const HomeStack = () => {
const navigation = useNavigation();
return (
<Top.Navigator>
<Top.Screen name="Welcome" component={HomeScreen} />
</Top.Navigator>
);
};

export default HomeStack;

HomeScreen.js(相关部分)

<SafeAreaView styles={styles.container}>
<StatusBar barStyle="light-content" />
<ScrollView>
<StationScroll />
</ScrollView>
</SafeAreaView>

StationScroll 只加载站徽列表StationBadge.js

const StationBadge = (props: StationProps) => {
const navigation = useNavigation();

const onPress = () => {
navigation.navigate('Stations', {
screen: 'Station',
station: props.station,
});
console.log(`station pressed: ${props.station.location}`);
};

return (
<TouchableOpacity onPress={onPress}>
<View style={styles.container}>
<Image
style={styles.image}
source={{
uri: `https://example.com/img/station-logos/${props.station.callsign}.png`,
}}
/>
<Text numberOfLines={1} style={styles.text}>
{props.station.location}
</Text>
</View>
</TouchableOpacity>
);
};

export default StationBadge;

这里还有 StationStack.js

import React from 'react';
import {View} from 'react-native';
import {createStackNavigator} from '@react-navigation/stack';
import StationsScreen from '../screens/StationsScreen';
import StationScreen from '../screens/StationScreen';
import {StackActions, useNavigation} from '@react-navigation/native';

const SS = createStackNavigator();

const StationStack = () => {
const navigation = useNavigation();

return (
<SS.Navigator>
<SS.Screen name="Stations" component={StationsScreen}/>
<SS.Screen name="Station" component={StationScreen} />
</SS.Navigator>
);
};

export default StationStack;

最佳答案

我应该继续阅读 documentation :

渲染导航器中定义的初始路线

默认情况下,当您在嵌套导航器中导航屏幕时,指定的屏幕用作初始屏幕并且导航器上的初始路由属性被忽略。此行为不同于 React Navigation 4。

如果需要渲染导航器中指定的初始路由,可以通过设置initial: false来禁止使用指定屏幕作为初始屏幕的行为:

navigation.navigate('Stations', {
screen: 'Station',
initial: false,
});

关于react-native - React Navigation 5 嵌套屏幕无法返回主屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65478064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com