This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
堆
了解如何使用 Expo Router 中的 Stack 导航器。

在屏幕之间导航,在屏幕之间传递参数,创建动态路由,并配置屏幕标题和动画。
堆栈导航器是应用中在路由之间导航的基础方式。在 Android 上,堆叠路由会在当前屏幕上方进行动画。在 iOS 上,堆叠路由会从右侧进行动画。Expo Router 提供了一个 Stack 导航组件,该组件可以创建导航堆栈,并允许你在应用中添加新的路由。
🌐 A stack navigator is the foundational way of navigating between routes in an app. On Android, a stacked route animates on top of the current screen. On iOS, a stacked route animates from the right. Expo Router provides a Stack navigation component that creates a navigation stack and allows you to add new routes in your app.
本指南提供了有关如何在项目中创建 Stack 导航器以及自定义单个路由的选项和标题的信息。
🌐 This guide provides information on how you can create a Stack navigator in your project and customize an individual route's options and header.
开始使用
🌐 Get started
你可以使用基于文件的路由来创建堆栈导航器。下面是一个示例文件结构:
🌐 You can use file-based routing to create a stack navigator. Here's an example file structure:
srcapp_layout.tsxindex.tsxdetails.tsx这个文件结构产生的布局是,index 路由是堆栈中的第一个路由,而当导航时,details 路由会被压入 index 路由之上。
🌐 This file structure produces a layout where the index route is the first route in the stack, and the details route is pushed on top of the index route when navigated.
你可以使用 src/app/_layout.tsx 文件来定义你的应用的 Stack 导航器,并包含这两个路由:
🌐 You can use the src/app/_layout.tsx file to define your app's Stack navigator with these two routes:
屏幕选项和标题配置
🌐 Screen options and header configuration
从 SDK 55 开始,你可以使用基于选项的 API 或新的组合组件 API 配置屏幕选项和标题。这两种 API 在你的项目中都可以互换使用。
🌐 Starting in SDK 55, you can configure screen options and the header using either the options-based API or the new composition components API. Both APIs can be used interchangeably in your project.
静态配置路由选项
🌐 Statically configure route options
你可以在布局组件路由中使用 <Stack.Screen name={routeName} /> 组件来静态配置路由的选项。
🌐 You can use the <Stack.Screen name={routeName} /> component in the layout component route to statically configure a route's options.
配置标题栏
🌐 Configure header bar
你可以通过使用 screenOptions 属性来为 Stack 导航器中的所有路由配置标题栏。这对于在所有路由中设置统一的标题样式非常有用。
🌐 You can configure the header bar for all routes in a Stack navigator by using the screenOptions prop. This is useful for setting a common header style across all routes.
设置原生标题的界面风格
🌐 Set the native header's interface style
信息 此选项仅在 iOS 的 SDK 58 及更高版本 可用。
使用 headerUserInterfaceStyle 可以将屏幕的原生标题设置为 'light' 或 'dark',而不会改变导航主题。例如,详情屏幕可以使用深色标题,而堆栈的其他屏幕仍保持基于主题的样式:
🌐 Use headerUserInterfaceStyle to set a screen's native header to 'light' or 'dark' without changing the navigation theme. For example, a detail screen can use a dark header while the rest of the stack keeps its theme-based style:
你也可以在 screenOptions 中为整个堆栈设置这个选项。Stack.Screen 选项会覆盖该屏幕的设置。如果省略该选项,本地标题将遵循导航主题。本地标题在屏幕可见时不支持更改此选项。这个选项仅影响本地标题。如果需要,可以单独设置标题颜色。
🌐 You can also set this option in screenOptions for the whole stack. A Stack.Screen option overrides it for that screen. When the option is omitted, the native header follows the navigation theme. The native header does not support changes to this option while the screen is visible. This option affects the native header only. Set header colors separately if needed.
动态设置屏幕选项
🌐 Set screen options dynamically
要动态配置路由的选项,你可以使用组合组件或基于选项的 API。
🌐 To configure a route's options dynamically, you can use either the composition components or the options-based API.
重要 屏幕组合 API 处于 alpha 阶段,并在 SDK 55 及更高版本中可用。
可用的标头选项
🌐 Available header options
Stack 导航器支持全面的标题配置选项。以下是所有可用的标题相关选项:
🌐 The Stack navigator supports comprehensive header configuration options. Below are all the header-related options available:
标题按钮
🌐 Header buttons
你可以使用 headerLeft 和 headerRight 选项或 <Stack.Toolbar> 组件向头部添加按钮。这些选项接受在头部渲染的 React 组件。
🌐 You can add buttons to the header by using the headerLeft and headerRight options or <Stack.Toolbar> component. These options accept a React component that renders in the header.
配置 iOS 头部工具栏以支持液态玻璃效果。
重要 屏幕组合 API 处于 alpha 阶段,并在 SDK 55 及更高版本中可用。
其他屏幕选项
🌐 Other screen options
有关所有可用其他屏幕选项(包括动画、手势和其他配置)的完整列表:
🌐 For a complete list of all available other screen options including animations, gestures, and other configurations:
自定义推送行为
🌐 Custom push behavior
默认情况下,Stack 导航器在推送已存在于堆栈中的路由时会移除重复的页面。例如,如果你重复推送同一个页面,第二次推送将被忽略。你可以通过向 <Stack.Screen> 提供自定义的 getId() 函数来更改这种推送行为。
🌐 By default, the Stack navigator removes duplicate screens when pushing a route that is already in the stack. For example, if you push the same screen twice, the second push will be ignored. You can change this push behavior by providing a custom getId() function to the <Stack.Screen>.
例如,以下布局结构中的 index 路由显示了应用中不同用户资料的列表。我们可以将 [details] 路由设为动态路由,以便应用用户可以导航查看某个资料的详细信息。
🌐 For example, the index route in the following layout structure shows a list of different user profiles in the app. Let's make the [details] route a dynamic route so that the app user can navigate to see a profile's details.
srcapp_layout.tsxindex.tsx[details].tsxmatches dynamic paths like '/details1'Stack 导航器每次应用用户导航到不同的个人资料时都会推送一个新屏幕,但会失败。如果你提供一个每次返回新ID的 getId() 函数,Stack 每次应用用户导航到个人资料时都会推送一个新屏幕。
🌐 The Stack navigator will push a new screen every time the app user navigates to a different profile but will fail. If you provide a getId() function that returns a new ID every time, the Stack will push a new screen every time the app user navigates to a profile.
你可以在布局组件路由中使用 <Stack.Screen name="[profile]" getId={}> 组件来修改推送行为:
🌐 You can use the <Stack.Screen name="[profile]" getId={}> component in the layout component route to modify the push behavior:
删除堆栈屏幕
🌐 Removing stack screens
你可以使用不同的操作来关闭和删除堆栈中的一个或多个路由。
🌐 There are different actions you can use to dismiss and remove one or many routes from a stack.
dismiss 操作
🌐 dismiss action
关闭最近堆栈中的最后一个屏幕。如果当前屏幕是堆栈中唯一的路由,它将关闭整个堆栈。
🌐 Dismisses the last screen in the closest stack. If the current screen is the only route in the stack, it will dismiss the entire stack.
你可以选择传递一个正数来关闭指定数量的屏幕。
🌐 You can optionally pass a positive number to dismiss up to that specified number of screens.
Dismiss 与 back 不同,因为它作用于最接近的堆栈,而不是当前的导航器。如果你有嵌套的导航器,调用 dismiss 将会让你返回多个屏幕。
🌐 Dismiss is different from back as it targets the closest stack and not the current navigator. If you have nested navigators, calling dismiss will take you back multiple screens.
dismissTo 操作
🌐 dismissTo action
dismissTo在 Expo Router4.0.8中被添加。它的操作方式类似于 Expo Router v3 中的navigation函数。
在当前的 <Stack /> 中关闭屏幕,直到达到指定的 Href。如果历史记录中没有 Href,当前屏幕将被指定的 Href 替代。
🌐 Dismisses screens in the current <Stack /> until the specified Href is reached. If the Href is absent in the history, the current screen is replaced with the specified Href instead.
例如,考虑 /one、/two、/three 的历史记录,而 /three 是当前的路由。动作 router.dismissTo('/one') 会让历史记录回退两步,而 router.dismissTo('/four') 会将当前的 /three 路由替换为 /four 路由。
🌐 For example, consider the history of /one, /two, /three routes, where /three is the current route. The action router.dismissTo('/one') will cause the history to go back twice, while router.dismissTo('/four') will replace the current /three route with the /four route.
dismissAll 操作
🌐 dismissAll action
dismissAll 操作会关闭最近堆栈中第一个屏幕之上的所有屏幕,返回到那个第一个屏幕。
🌐 The dismissAll action dismisses every screen above the first screen in the closest stack, returning to that first screen.
例如,home 路由是第一个屏幕,而 settings 是最后一个。要从 settings 路由到 home,你需要返回到 details。然而,使用 dismissAll 操作,你可以从 settings 路由到 home,并关闭中间的任意屏幕。
🌐 For example, the home route is the first screen, and the settings is the last. To go from settings to home route you'll have to go back to details. However, using the dismissAll action, you can go from settings to home and dismiss any screen in between.
canDismiss 操作
🌐 canDismiss action
检查是否可以关闭当前屏幕。如果路由位于堆栈中且堆栈历史记录中有多个屏幕,则返回 true。
🌐 To check if it is possible to dismiss the current screen. Returns true if the router is within a stack with more than one screen in the stack's history.
iOS 26 液态玻璃标题
🌐 iOS 26 Liquid Glass headers
从 iOS 26 开始,导航栏标题默认采用系统的“液态玻璃”效果。无法针对单个页面禁用,因此需要通过全局配置来选择退出。
🌐 Starting from iOS 26, navigation headers adopt the system's "Liquid Glass" effect by default. It cannot be disabled per screen, so you need to opt out using a global configuration.
方法1:使用 UIDesignRequiresCompatibility
🌐 Method 1: Use UIDesignRequiresCompatibility
注意:在 Expo Go 中不支持。此方法是一个临时解决方案。从 iOS 27 开始,此选项将被苹果移除,你无法选择退出液态玻璃效果。
创建一个 开发版本 并在 应用配置 中将 UIDesignRequiresCompatibility 属性设置为 true:
🌐 Create a development build and set the UIDesignRequiresCompatibility property to true in app config:
方法二:使用基于 JavaScript 的导航栈
🌐 Method 2: Use JavaScript-based navigation stack
将本地堆栈替换为在 expo-router/js-stack 提供的由 JavaScript 驱动的堆栈。这可以让你完全控制头部 UI,但会牺牲经过高度优化的 iOS 导航视图和控制器的性能优势:
🌐 Swap the native stack for the JavaScript-driven stack shipped at expo-router/js-stack. This gives you full control over the header UI, at the cost of the performance benefits of the highly optimized iOS navigation views and controllers:
expo-router/js-stack 入口点是 SDK 56 替代 @react-navigation/stack 库的方式。有关更多信息,请参阅 SDK 55 到 56 迁移指南。
🌐 The expo-router/js-stack entry point is the SDK 56 replacement for the @react-navigation/stack library. See the SDK 55 to 56 migration guide for more information.
常见问题
🌐 Common problems
大标题在滚动时不会收缩
当使用 headerLargeTitle: true(或 <Stack.Title large>)与 ScrollView 或 FlatList 时,大标题在滚动时可能不会收起。发生这种情况是因为可滚动视图不是屏幕组件的直接第一个子组件。
🌐 When using headerLargeTitle: true (or <Stack.Title large>) with a ScrollView or FlatList, the large title may not collapse on scroll. This happens when the scrollable view is not the direct first child of the screen component.
为了解决此问题,请确保 ScrollView 或 FlatList 是你的屏幕组件渲染的第一个子元素。如果需要一个封装器,请在其上设置 collapsable={false}:
🌐 To fix this, ensure ScrollView or FlatList is the first child rendered by your screen component. If you need a wrapper, set collapsable={false} on it:
如果你需要封装 ScrollView,请在封装器上设置 collapsable={false}:
🌐 If you need to wrap the ScrollView, set collapsable={false} on the wrapper:
在屏幕之间切换时背景会闪白
屏幕切换之间出现的白色闪光通常意味着导航堆栈使用了浅色背景,而你的应用使用的是深色主题。
🌐 A white flash between screen transitions usually means the navigation stack is using a light background while your app uses a dark theme.
要解决此问题,请用 Expo Router 的 <ThemeProvider> 封装你的根布局,并传递适当的主题:
🌐 To fix this, wrap your root layout with Expo Router's <ThemeProvider> and pass the appropriate theme:
对于始终使用深色主题的应用:
🌐 For apps that are always dark-themed: