reactjs - 如果 React Native 中的 React Navigation 库提供的堆栈导航器组件不存在 initialRouteName Prop 会发生什么

标签 reactjs react-native react-navigation react-navigation-stack react-navigation-v5

const App = () => (
  <SafeAreaProvider>
    <NavigationContainer theme={MyTheme} linking={linking}>
      <Stack.Navigator
        initialRouteName="Root"
        screenOptions={{ gestureEnabled: false }}
      >
        <Stack.Screen
          name="NotRoot"
          component={NotRoot}
          options={verticalConfig}
        />
      </Stack.Navigator>
    </NavigationContainer>
  </SafeAreaProvider>
);

在此代码片段中,Root 是一个不存在的 Stack.Screen 名称,它不存在于项目源代码中的任何文件中。所以,我想知道“Root”是否有点像默认值?

最佳答案

Root 不是默认值。当您指定一个不存在的 initialRouteName 时,它的作用与您未指定 prop 时相同:它会转到第一个屏幕(路线)。

你可以在代码中看到它(GitHub),这里是相关部分:

const initialRouteName =
  options.initialRouteName !== undefined &&
  routeNames.includes(options.initialRouteName)
    ? options.initialRouteName // if initialRouteName is defined and exists
    : routeNames[0]; // otherwise, go to the first one

关于reactjs - 如果 React Native 中的 React Navigation 库提供的堆栈导航器组件不存在 initialRouteName Prop 会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63200145/

相关文章:

reactjs - 函数组件中的 React/ReactNative 静态属性

javascript - react 导航中的动态标题

meteor - 为什么应该将 React 与 Meteor 一起使用?

css - 如何通过 NPM 分发 CSS

javascript - React - 无法设置未定义的属性

react-native - 无法使用远程 redux devtools 连接 React Native 应用程序

javascript - 如何在 typescript 中使用抽象类

ios - 为什么 React Native/Objective-C 提示我用错误数量的参数调用我的方法?

reactjs - 在 React Native 中使用 Expo-Route 设置所有 View 的背景颜色

javascript - react 导航 v5 : How to get route params of the parent navigator inside the child screen