Skip to main content
Version: 7.x

屏幕

屏幕表示导航器中的路由。屏幕的配置包含路由的组件、选项、事件监听器等。

¥A screen represents routes in a navigator. A screen's configuration contains the component for the route, options, event listeners, etc.

可以在导航器配置中的 screens 键下定义屏幕:

¥Screens can be defined under the screens key in the navigator configuration:

const MyStack = createNativeStackNavigator({
screens: {
Home: HomeScreen,
Profile: ProfileScreen,
},
});

配置

¥Configuration

名称

¥Name

用于屏幕的名称。

¥The name to use for the screen.

screens 对象中的键用作名称:

¥The key in the screens object is used as the name:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
},
},
});

该名称用于导航到屏幕:

¥This name is used to navigate to the screen:

navigation.navigate('Profile');

它还用于 route 中的 name 属性。

¥It is also used for the name property in the route.

虽然它受支持,但我们建议避免在屏幕名称中使用空格或特殊字符并保持其简单。

¥While it is supported, we recommend avoiding spaces or special characters in screen names and keeping them simple.

选项

¥Options

选项用于配置屏幕在导航器中的显示方式。它接受一个对象或返回一个对象的函数:

¥Options are used to configure how the screen gets presented in the navigator. It accepts either an object or a function returning an object:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
options: {
title: 'Awesome app',
},
},
},
});

当你传递一个函数时,它将接收 routenavigationtheme 作为参数:

¥When you pass a function, it'll receive the route, navigation and theme as arguments:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
options: ({ route, navigation, theme }) => ({
title: route.params.userId,
}),
},
},
});

有关更多详细信息和示例,请参阅 屏幕选项

¥See Options for screens for more details and examples.

初始参数

¥Initial params

初始参数用作屏幕的默认参数。如果屏幕用作 initialRouteName,它将包含 initialParams 的参数。如果你导航到新屏幕,传递的参数将与初始参数浅层合并。

¥Initial params are used as the default params for the screen. If a screen is used as initialRouteName, it'll contain the params from initialParams. If you navigate to a new screen, the params passed are shallow merged with the initial params.

const Stack = createNativeStackNavigator({
screens: {
Details: {
screen: DetailsScreen,
initialParams: { itemId: 42 },
},
},
});

ID

屏幕可以有一个 ID 来唯一地标识它。当你想要确保具有相同 ID 的屏幕不会在堆栈中多次出现时,这很有用。

¥A screen can have an ID to identify it uniquely. This is useful when you want to ensure that the screen with the same ID doesn't appear multiple times in the stack.

这可以通过指定 getId 回调来完成。它接收一个带有路由参数的对象:

¥This can be done by specifying the getId callback. It receives an object with the route params:

const Stack = createStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
getId: ({ params }) => params.userId,
},
},
});

在上面的例子中,params.userId 用作 Profile 屏幕和 getId 的 ID。这会改变导航的工作方式,以确保具有相同 ID 的屏幕在堆栈中仅出现一次。

¥In the above example, params.userId is used as an ID for the Profile screen with getId. This changes how the navigation works to ensure that the screen with the same ID appears only once in the stack.

假设你有一个带有历史记录 Home > Profile (userId: bob) > Settings 的堆栈,请考虑以下场景:

¥Let's say you have a stack with the history Home > Profile (userId: bob) > Settings, consider following scenarios:

  • 你调用 navigate(Profile, { userId: 'bob' }):结果屏幕将是 Home > Settings > Profile (userId: bob),因为现有的 Profile 屏幕与 ID 匹配。

    ¥You call navigate(Profile, { userId: 'bob' }): The resulting screens will be Home > Settings > Profile (userId: bob) since the existing Profile screen matches the ID.

  • 你调用 navigate(Profile, { userId: 'alice' }):结果屏幕将是 Home > Profile (userId: bob) > Settings > Profile (userId: alice),因为它将添加一个新的 Profile 屏幕,因为没有找到匹配的屏幕。

    ¥You call navigate(Profile, { userId: 'alice' }): The resulting screens will be Home > Profile (userId: bob) > Settings > Profile (userId: alice) since it'll add a new Profile screen as no matching screen was found.

如果在选项卡或抽屉式导航器中指定了 getId,则当 ID 更改时屏幕将重新安装。

¥If getId is specified in a tab or drawer navigator, the screen will remount if the ID changes.

警告

如果你使用 @react-navigation/native-stack,它无法与 getId 回调一起正常工作。因此,建议在这种情况下避免使用它。

¥If you're using @react-navigation/native-stack, it doesn't work correctly with the getId callback. So it's recommended to avoid using it in that case.

组件

¥Component

每个屏幕都必须指定要为该路由渲染的组件。

¥Each screen must specify a component to render for that route.

它可以在屏幕配置中的 screen 属性下传递:

¥It can be passed under the screen property in the screen configuration:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
},
},
});

布局

¥Layout

布局是屏幕的封装器。它可以更轻松地为屏幕提供诸如错误边界和悬念回退之类的内容,或者用额外的 UI 封装屏幕。

¥A layout is a wrapper around the screen. It makes it easier to provide things such as an error boundary and suspense fallback for a screen, or wrap the screen with additional UI.

它需要一个返回 React 元素的函数:

¥It takes a function that returns a React element:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
layout: ({ children }) => (
<ErrorBoundary>
<React.Suspense
fallback={
<View style={styles.fallback}>
<Text style={styles.text}>Loading…</Text>
</View>
}
>
{children}
</React.Suspense>
</ErrorBoundary>
),
},
},
});

¥Navigation key

导航键是此屏幕的可选键。这不需要是唯一的。如果键发生更改,则具有此名称的现有屏幕将被删除(如果在堆栈导航器中使用)或重置(如果在选项卡或抽屉导航器中使用)。

¥A navigation key is an optional key for this screen. This doesn't need to be unique. If the key changes, existing screens with this name will be removed (if used in a stack navigator) or reset (if used in a tab or drawer navigator).

当我们希望在条件发生变化时删除或重置某些屏幕时,这将很有用:

¥This can be useful when we have some screens that we want to be removed or reset when the condition changes:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
navigationKey: 'user',
},
},
});

对于静态 API,我们建议对每个屏幕使用 groups 而不是 navigationKey,因为你可以使用 if 属性动态添加或删除组。

¥For the static API, we recommend using the groups instead of the navigationKey for each screen as you can dynamically add or remove groups with the if property.

事件监听器

¥Event listeners

事件监听器可用于订阅为屏幕发出的各种事件。详细信息请参见 Screen 上的 listeners 属性

¥Event listeners can be used to subscribe to various events emitted for the screen. See listeners prop on Screen for more details.