Expo 路由简介

Expo Router 是一个开源路由库,适用于使用 Expo 构建的 Universal React Native 应用。


Expo Router 是一个用于 React Native 和 Web 应用的基于文件的路由。它允许你管理应用中屏幕之间的导航,允许用户在多个平台(Android、iOS 和 Web)上使用相同的组件,在应用 UI 的不同部分之间无缝移动。

¥Expo Router is a file-based router for React Native and web applications. It allows you to manage navigation between screens in your app, allowing users to move seamlessly between different parts of your app's UI, using the same components on multiple platforms (Android, iOS, and web).

它将最好的文件系统路由概念从网络引入到通用应用中 - 允许你的路由在每个平台上工作。将文件添加到应用目录后,该文件会自动成为导航中的路由。

¥It brings the best file-system routing concepts from the web to a universal application — allowing your routing to work across every platform. When a file is added to the app directory, the file automatically becomes a route in your navigation.

特性

¥Features

  • 本国的:Expo Router 导航建立在我们强大的 React 导航套件 之上,默认情况下是真正的原生和平台优化。

    ¥Native: Built on top of our powerful React Navigation suite, Expo Router navigation is truly native and platform-optimized by default.

  • 可分享:应用中的每个屏幕都可以自动深度链接。使应用中的任何路由都可以通过链接共享。

    ¥Shareable: Every screen in your app is automatically deep linkable. Making any route in your app shareable with links.

  • 离线优先:应用会被缓存并首先离线运行,并在你发布新版本时自动更新。无需网络连接或服务器即可处理所有传入的原生 URL。

    ¥Offline-first: Apps are cached and run offline-first, with automatic updates when you publish a new version. Handles all incoming native URLs without a network connection or server.

  • 优化:通过生产中的惰性评估和开发中的延迟打包,自动优化路由。

    ¥Optimized: Routes are automatically optimized with lazy-evaluation in production, and deferred bundling in development.

  • 迭代:跨 Android、iOS 和 Web 的通用快速刷新,以及打包器中的工件记忆,可让你大规模快速前进。

    ¥Iteration: Universal Fast Refresh across Android, iOS, and web, along with artifact memoization in the bundler to keep you moving fast at scale.

  • 普遍的:Android、iOS 和 Web 共享统一的导航结构,能够在路由级别下拉至特定于平台的 API。

    ¥Universal: Android, iOS, and web share a unified navigation structure, with the ability to drop-down to platform-specific APIs at the route level.

  • 可发现:Expo Router 支持在 Web 上构建时静态渲染以及与原生的通用链接。这意味着你的应用内容可以被搜索引擎索引。

    ¥Discoverable: Expo Router enables build-time static rendering on web, and universal linking to native. Meaning your app content can be indexed by search engines.

使用不同的导航库

¥Using a different navigation library

你可以在 Expo 项目中使用任何其他导航库,例如 React 导航。但是,如果你正在构建新应用,我们建议使用 Expo Router 来实现上述所有功能。对于其他导航库,你可能必须为其中一些功能实现自己的策略,例如可共享链接或在同一项目中处理 Web 和原生导航。

¥You can use any other navigation library, like React Navigation, in your Expo project. However, if you are building a new app, we recommend using Expo Router for all the features described above. With other navigation libraries, you might have to implement your own strategies for some of these features, such as shareable links or handling web and native navigation in the same project.

如果你希望使用 Wix 的 React Native Navigation,它在 Expo Go 中不可用,并且尚不兼容 expo-dev-client。我们建议使用 React Navigation 中的 createNativeStackNavigator 来使用 Android 和 iOS 原生导航 API。

¥If you are looking to use React Native Navigation by Wix, it is not available in Expo Go and is not yet compatible with expo-dev-client. We recommend using createNativeStackNavigator from React Navigation to use Android and iOS native navigation APIs.

常见问题

¥Common questions

Expo Router versus Expo versus React Native CLI

Historically, React Native has been non prescriptive about how apps should be built which is similar to using React without a modern web framework. Expo Router is an opinionated framework for React Native, similar to how Remix and Next.js are opinionated frameworks for web-only React.

Expo Router is designed to bring the best architectural patterns to everyone, to ensure React Native is leveraged to its fullest. For example, Expo Router's Async Routes feature enables lazy bundling for everyone. Previously, lazy bundling was only used at Meta to build the Facebook app.

Can I use Expo Router in my existing React Native app?

Yes, Expo Router is the framework for universal React Native apps. Due to the deep connection between the router and the bundler, Expo Router is only available in Expo CLI projects with Metro. Luckily, you can use Expo CLI in any React Native project too!

What are the benefits of file-based routing?
  • The file system is a well-known and well-understood concept. The simpler mental model makes it easier to educate new team members and scale your application.
  • The fastest way to onboard new users is by having them open a universal link that opens the app or website to the correct screen depending on if they have the app installed or not. This technique is so advanced that it's usually only available to large companies that can afford to make and maintain the parity between platforms. But with Expo's file-based routing, you can have this feature out of the box!
  • Refactoring is easier to do because you can move files around without having to update any imports or routing components.
  • Expo Router has the ability to statically type routes automatically. This ensures you can only link to valid routes and that you can't link to a route that doesn't exist. Typed Routes also improve refactoring as you'll get type errors if links are broken.
  • Async Routes (bundle splitting) improve development speed, especially in larger projects. They also make upgrades easier as errors are isolated to a single route, meaning you can incrementally update or refactor your app page-by-page rather than all at once (traditional React Native).
  • Deep links always work, for every page. This makes it possible to share links to any content in the app, which is great for promoting your app, collecting bug reports, E2E testing, automating screenshots, and so on.
  • Expo Head uses automatic links to enable deep-native integration. Features like Quick Notes, Handoff, Siri context, and universal links only require configuration setup, no code changes. This enables perfect vertical integration with the entire ecosystem of smart devices that a user has, leading to the types of user experiences that are only possible with universal apps (web ⇄ native).
  • Expo Router has the ability to statically render each page automatically on the web, enabling real SEO and full discoverability of your app's content. This is only possible because of the file-based convention.
  • Expo CLI can infer a lot of information about your application when it follows a known convention. For example, we could implement automatic bundle splitting per route, or automatically generate a sitemap for your website. This is impossible when your app only has a single entry point.
  • Re-engagement features like notifications and home screen widgets are easier to integrate as you can simply intercept the launch and deep link, with query parameters, anywhere in the app.
  • Like on the web, analytics and error reporting can easily be configured to automatically include the route name, which is useful for debugging and understanding user behavior.
Why should I use Expo Router over React Navigation?

Expo Router and React Navigation are both libraries from the Expo team. We built Expo Router on top of React Navigation to enable the benefits of file-based routing. Expo Router is a superset of React Navigation, meaning you can use any React Navigation components and APIs with Expo Router.

If file-based routing isn't right for your project, you can drop down to React Navigation and set up routes, types, and links manually.

How do I server-render my Expo Router website?

Basic static rendering (SSG) is supported in Expo Router. Server-side rendering currently requires custom infrastructure to set up.

下一步

¥Next steps

快速开始

了解如何快速开始使用 Expo Router。

手动安装

有关如何开始并将 Expo Router 添加到现有应用的详细说明。

示例应用

请参阅 GitHub 上示例应用的源代码。