将 Expo 与现有 React Native 应用结合使用的概述

了解如何将 Expo 工具和服务与现有的 React Native 应用结合使用。


如果你有一个不使用任何 Expo 工具的 React Native 应用,你可能想知道 Expo 可以为你提供什么,为什么你可能想要使用 Expo 工具和服务,以及如何开始。

🌐 If you have a React Native app that doesn't use any Expo tools, you might be wondering what Expo can provide for you, why you might want to use Expo tools and services, and how to get started.

Expo 提供的所有工具和服务都能在任何 React Native 应用中很好地运行

你可以使用 EAS 快速搭建专业的 CI/CD 工作流程,以构建、审核、部署和更新你的应用。 Expo CLI 提供了最优的命令行体验,用于操作 React Native。 Expo SDK 是 React Native 的扩展标准库。它为开发者提供高质量、维护良好的原生库,使用一致的 API 规范,让学习和使用更加容易。

🌐 You can use EAS to get quickly set up with a professional CI/CD workflow for building, reviewing, deploying, and updating your apps. Expo CLI provides the best command-line experience for working with React Native. The Expo SDK is an extended standard library for React Native. It gives developers high-quality, well-maintained native libraries that use consistent API conventions to make them easier to learn and use.

如果你曾经为 React Native 编写过原生模块,你会惊讶地发现,使用 Expo Modules API 提供的符合习惯用法的 Swift 和 Kotlin DSL 构建和维护模块要容易得多。

🌐 If you've ever written a native module for React Native, you'll be surprised how much easier it is to build and maintain modules with the idiomatic Swift and Kotlin DSL provided by the Expo Modules API.

还有更多值得探索的内容,以下链接将帮助你探索可用的选项。

🌐 There's so much more to explore, and the links below will help you to explore the options available to you.

增量采用步骤

🌐 Incremental adoption steps

以下是逐步采用的四个建议阶段。这些阶段通常从快速改变以改善开发者体验开始,逐步过渡到更重要的工作流程和代码库优化。

🌐 Below are four suggested phases of incremental adoption. These phases generally progress from quick changes to improve developer experience, to more significant workflow and codebase optimizations.

只有第一阶段——前提条件——是其他阶段所必需的。按照其说明操作后,你可以直接跳到与你采用 Expo 目标最相关的工具和服务。

🌐 Only the first phase — prerequisites — is required by other phases. After following its instructions, you can skip to the tools and services that are most relevant to your goals in adopting Expo.

先决条件

🌐 Prerequisites

这些第一步是以后采用 Expo 工具和服务所必需的:

🌐 These first steps are required to later adopt Expo tools and services:

安装 Expo 模块

要解锁 Expo 功能,你需要在现有的 React Native 项目中安装 expo 包。本指南提供了自动和手动安装步骤。

使用 Expo CLI

迁移到 Expo CLI 是对 @react-native-community/cli 的直接替代。它与所有 Expo 工具和服务完全兼容。本指南解释了其优势,并提供了安装expo 包后启动开发服务器的编译命令。

React Native 的 Modal 组件

🌐 Quick wins

以下内容有助于改善开发体验并需要配置:

🌐 The following helps improving development experience and requires configuration:

使用 Expo SDK

使用 Expo SDK 提供的众多库之一,Expo SDK 是一个提供对原生 API 访问的丰富库集合。

安装 expo-dev-client

expo-dev-client 为你的调试应用变体提供类似 Expo Go 的应用启动器界面。了解如何在现有的 React Native 项目中安装和配置它。

编写原生模块

使用 Expo 模块 API 使用 Swift 和 Kotlin 编写原生模块。

原生项目升级助手

查看逐文件的差异,了解升级本地项目到下一个 Expo SDK 和 React Native 版本所需进行的所有更改。

新工作流程

🌐 New workflows

一旦你的应用安装了 expo 包,你就可以通过一个命令将应用提交到应用商店,或者更新配置 expo-updates 库来管理应用代码的远程更新:

🌐 Once your app has expo package installed, you can submit your app to app stores with a single command or update configure expo-updates library to manage remote updates to you app's code:

应用分发

只需一个命令即可构建并提交你的应用到应用商店。

安装 expo-updates

了解如何安装和配置 expo-updates 以管理远程更新并启用 PR 预览。

新思维模式

🌐 New mindsets

以下内容有助于提高项目的长期可维护性、原生代码维护和更轻松的升级:

🌐 The following helps with your project's long term maintainability, native code maintenance, and easier upgrades:

采用预构建

了解如何通过根据配置按需生成原生项目来简化维护工作。

Expo 路由

Expo Router 是一个基于文件的路由库,提供了如有序的导航层级、自动深度链接支持等优势。

常见问题

🌐 Common questions

在我现有的 React Native 项目中采用 Expo 需要多长时间?

采用 Expo 不必一次性完成。你可以先从“快速获益”开始,然后再处理更复杂的部分。你还可以根据对你项目最有帮助的功能,选择性地采用不同的功能。

🌐 Adopting Expo doesn't have to be done in one step. You can start with the quick wins and then move on to more complex parts. You can also pick and choose which features you want to adopt based on what is most helpful for your project.

在我的 React Native 应用中使用 Expo 我会获得什么好处?

在现有的 React Native 应用中采用 Expo 工具可以帮助你使用 Expo SDK 更快地开发,通过 CNG 简化原生代码的维护和升级,使用 EAS Update 更快地部署,等等。

🌐 Adopting Expo tools in your existing React Native app can help you develop faster with the Expo SDK, streamline native code maintenance and upgrades with CNG, deploy faster with EAS Update, and more.

谁在使用 Expo?

Expo 被全球顶尖公司使用,这些公司服务数百万终端用户。更多信息,请参见我们的 Expo 展示

🌐 Expo is used by top companies worldwide that serve millions of end users. For more information, see our Expo showcase.

采用 Expo 会对我的应用大小产生什么影响?

expo 包占用空间很小,因为它仅包含每个应用所需的最小模块集,这些模块与自动链接基础设施和其他内置的 Expo SDK 库一起使用。有关如何确定应用实际大小的更多信息,请参见 了解应用大小

🌐 The expo package has a small footprint since it only includes a minimal set of modules required in every app with autolinking infrastructure and other Expo SDK libraries that are built-in. For more information on how to determine the actual size of your app, see Understanding app size.

为什么 React Native 推荐使用 Expo?

大多数 React Native 开发者在构建应用时都会解决一些常见问题,例如实现导航、访问原生 API、升级到新版本等。这需要使用一套特定的工具和库来构建和维护你的应用——这也意味着你正在创建自己的框架。

🌐 Most React Native developers solve common problems when building an app, such as implementing navigation, accessing Native APIs, upgrading to new versions, and more. This requires using a specific set of tools and libraries to build and maintain your app — which means you are creating your own framework.

Expo 通过提供一套基本工具来解决这些问题,并帮助你(开发者)专注于构建应用。它还提供工具来加快开发中的迭代速度。更多信息,请参见 为什么 React Native 建议使用框架

🌐 Expo solves these problems by providing a set of primitives and helping you (the developer) to focus on building your app. It also offers tools to iterate faster in development. For more information, see Why React Native recommends using a framework.

我需要删除我的原生项目才能使用 Expo 吗?

默认情况下,用 create-expo-app 创建的 Expo 项目使用 持续本地生成 (CNG),并且不包含 androidios 本地目录。如果你在现有的 React Native 应用中逐步采用 Expo,则不必删除这些目录。你可以使用 npx expo run:[android|ios] 作为 @react-native-community/cli 提供的命令的替代方案,在本地编译应用并保持本地项目的配置。

🌐 By default, Expo projects created with create-expo-app use Continuous Native Generation (CNG) and do not contain android and ios native directories. If you incrementally adopt Expo in your existing React Native app, you don't have to remove these directories. You can use npx expo run:[android|ios] as an alternative to commands offered by @react-native-community/cli to compile your app locally and keep the configuration of your native projects.

我使用 CodePush。我可以继续在 Expo 中使用它吗?

CodePush 将于 2025 年 3 月退役,并且与 React Native 的新架构不兼容,因此从长远来看,我们建议切换到 EAS Update 来管理应用代码的远程更新。然而,你今天就可以在启用了 CodePush 的应用中开始使用 Expo 工具,包括 Expo SDK、Expo CLI、EAS Build 等。

🌐 CodePush will be retired in March 2025 and is incompatible with React Native's New Architecture, so, in the long run, we recommend switching to EAS Update to manage remote updates to your app's code. However, you can start using Expo tools in your CodePush enabled app today, including the Expo SDK, Expo CLI, EAS Build, and more.

我必须用 EAS 构建吗?

Expo 应用服务 (EAS) 是针对 Expo 和 React Native 应用深度集成的云服务,提供构建、测试和部署应用的工具。

虽然我们建议使用 EAS 以便与团队成员顺畅协作并快速分发,但你也可以在本地、在 CI 上或使用你喜欢的任何其他方式编译你的应用。

🌐 Although we recommend using EAS for a smooth collaboration with your teammates and fast distribution, you can compile your app locally, on your CI, or any other way you prefer.

我可以在我的代码中安装第三方本地库吗?

是的,你可以安装和使用需要本地项目(androidios)配置的第三方库,或者提供带有 开发构建配置插件 的库。更多信息请参见 使用第三方库

🌐 Yes, you can install and use third-party libraries that require native projects (android and ios) configuration or provides a config plugin with development builds. See Using Third-Party libraries for more information.

我使用的是 React Navigation。我必须使用 Expo Router 吗?

你可以在项目中继续使用任何导航库。然而,我们建议使用 Expo Router,以享受这里描述的所有优势 described here

🌐 You can continue using any navigation library in your project. However, we recommend using Expo Router for all the benefits described here.