如果 React Native 中的 React Navigation 库提供的堆栈导航器组件不存在 initialRouteName 道具会发生什么
What happens if the initialRouteName prop doesn't exist for a stack navigator component provided by the React Navigation library in React Native
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
时,它的作用与您未指定道具时相同:它会转到第一个屏幕(路线)。
代码中可以看到(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
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
时,它的作用与您未指定道具时相同:它会转到第一个屏幕(路线)。
代码中可以看到(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