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

React Navigation 5 Nested Screen can't get back to Primary Screen

我无法理解嵌套的导航器。我有一个带有这些按钮的 bottomTabNavigator:

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

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

问题: 当我使用此代码导航到嵌套导航器从主页导航到站点详细信息页面时,我无法导航回 main/root 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;

希望你一切顺利。 据我了解这个问题,您正在尝试访问与主屏幕不在同一堆栈中的屏幕。 在 HomeStack 上,我建议你添加这行代码:

// HomeStack.js page 
...
...
import StationScreen from '../screens/StationScreen';
...
...
<Top.Navigator initialRouteName="Welcome">
  <Top.Screen name="Welcome" component={HomeScreen} />
  <Top.Screen name="Stations" component={StationsScreen} />
</Top.Navigator>

有了这个,你可以从家里切换到车站,反之,请试试这个,看看它是否有效。

我应该继续阅读 documentation:

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

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

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

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