This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo
Expo 和相关包的一组常用方法和类型。
安装
🌐 Installation
应用接口
🌐 API
import * as Expo from 'expo';
expo/fetch API
expo/fetch 提供了一个 符合 WinterCG 标准的 Fetch API,在网页和移动环境中都能稳定工作,确保在 Expo 应用中实现标准化和跨平台的 fetch 体验。
注意: 在 Android 和 iOS 上,
expo/fetch也作为全局fetch安装。这意味着任何对fetch(...)的调用,如果不从expo/fetch导入,都使用相同的符合 WinterCG 的实现。要保持 React Native 内置的fetch为全局,请在你的 环境变量 中设置EXPO_PUBLIC_USE_RN_FETCH=1。无论此标志如何,从expo/fetch的命名导入仍然可以正常工作。
API 编码
🌐 Encoding APIs
TextEncoder 和 TextDecoder 是内置的 API,用于以各种字符编码对文本进行编码和解码。它们在所有平台上都可用。有关 Web 和 Node.js 的支持情况,请参阅 浏览器和服务器运行时支持。
TextEncoder API,以及从 SDK 58 和 React Native 0.88 开始的 TextDecoder API,都是由 Hermes 提供的。可以查看 Hermes GitHub 仓库中的 TextEncoder.cpp 和 TextDecoder.cpp 源代码。TextDecoder 支持 UTF-8、UTF-16 以及像 windows-1252(也叫做 latin1)这样的传统单字节编码。如果你使用自定义的 JavaScript 运行时,它必须在 Expo 初始化之前提供 TextDecoder。
🌐 The TextEncoder API and, starting with SDK 58 and React Native 0.88, the TextDecoder API are provided by Hermes. See the source code for TextEncoder.cpp and TextDecoder.cpp in the Hermes GitHub repository. TextDecoder supports UTF-8, UTF-16, and legacy single-byte encodings such as windows-1252 (also known as latin1). If you use a custom JavaScript runtime, it must provide TextDecoder before Expo initializes.
这些 API 的流式等效版本 TextEncoderStream 和 TextDecoderStream 在所有平台上也可用。它们允许你以流的方式进行文本的编码和解码,这对于处理大量数据而不需要一次性将其全部加载到内存中非常有用。
🌐 The stream equivalents of these APIs, TextEncoderStream and TextDecoderStream, are also available on all platforms. They allow you to encode and decode text in a streaming manner, which is useful for processing large amounts of data without loading it all into memory at once.
Streams API
在本地平台上提供对标准网络流的全球支持,以匹配网页和服务器平台的行为。有关特定的网页和 Node.js 支持,请参阅 浏览器和服务器运行时支持。EAS Hosting 服务器运行时也包括对标准网络流 API 的支持。
🌐 Global support for standard web streams is available on native platforms to match the behavior of web and server platforms. Refer to the browser and server runtime support for specific web and Node.js support. EAS Hosting server runtime also includes support for the standard web streams API.
在全球范围内访问 ReadableStream、WritableStream 和 TransformStream 类。
🌐 Globally access ReadableStream, WritableStream, and TransformStream classes.
URL接口
🌐 URL API
URL 在所有平台上提供标准 API。
在本地平台上,内置的 URL 和 URLSearchParams 实现替代了 react-native 中的填充。有关 Web 和 Node.js 的支持情况,请参阅 浏览器和服务器运行时支持。
🌐 On native platforms, built-in URL and URLSearchParams implementations replace the shims in react-native. Refer to the browser and server runtime support for web and Node.js.
Expo 内置的 URL 支持尝试完全符合 规范。唯一缺失的例外是本地平台目前不支持主机名中的 非 ASCII 字符。
🌐 Expo's built-in URL support attempts to be fully spec compliant. The only missing exception is that native platforms do not currently support non-ASCII characters in the hostname.
structuredClone
structuredClone 是一个内置函数,允许你创建一个值的深拷贝,包括像 Map、Set 和 ArrayBuffer 这样的复杂对象。它在所有平台上都可用。
const original = { name: 'Expo', date: new Date() }; const clone = structuredClone(original); console.log(clone); // { name: 'Expo', date: Date }
ArrayBuffer 和 TypedArray 的 transfer 选项尚未实现。
🌐 The transfer option for ArrayBuffer and TypedArrays is not implemented.
移除任何针对 structuredClone 的自定义 polyfill,以防止体积膨胀。有关 结构化克隆算法 的更多信息,请参考标准文档。
🌐 Remove any custom polyfills for structuredClone to prevent bloat. Refer to the standard documentation for more information on the structured clone algorithm.
No API data file found, sorry!
常见问题
🌐 Common questions
关于在你的项目中使用 expo 包的一些常见问题。
🌐 Some common questions about using the expo package in your project.
rootRegisterComponent 为现有的 React Native 项目设置
如果你正在手动管理你的 React Native 项目的原生目录(android 和 ios),你需要按照下面的说明来设置 registerRootComponent 功能。这对于 Expo 模块的正常工作是必要的。
🌐 If you are managing your React Native project's native directories (android and ios) manually, you need to follow the instructions below to set up the registerRootComponent function. This is necessary for the Expo modules to work correctly.
安卓
更新 android/app/src/main/your-package/MainActivity.java 文件,在 getMainComponentName 函数中使用名称 main。
🌐 Update the android/app/src/main/your-package/MainActivity.java file to use the name main in the getMainComponentName function.
iOS
更新 iOS 的 ios/your-project/AppDelegate.(m|mm|swift) 文件,在 application:didFinishLaunchingWithOptions: 函数的 createRootViewWithBridge:bridge moduleName:@"main" initialProperties:initProps 行中使用 moduleName main。
🌐 Update the iOS ios/your-project/AppDelegate.(m|mm|swift) file to use the moduleName main in the createRootViewWithBridge:bridge moduleName:@"main" initialProperties:initProps line of the application:didFinishLaunchingWithOptions: function.
如果我想给我的主应用文件起一个不是 App.js 或 app/_layout.tsx 的名字怎么办?
对于不使用 Expo Router 的项目,你可以在 package.json 中将 "main" 设置为项目中的任意文件。如果你这么做,那么你需要使用 registerRootComponent。如果你使用自定义入口文件,export default 不会将此组件设为应用的根组件。
例如,假设你想让 src/main.jsx 成为你应用的入口文件——也许你不喜欢在项目根目录中有 JavaScript 文件。首先,在 package.json 中设置如下:
🌐 For example, let's say you want to make src/main.jsx the entry file for your app — maybe you don't like having JavaScript files in the project root. First, set this in package.json:
然后,在 src/main.jsx 中,确保你调用 registerRootComponent 并传入你想在应用根部渲染的组件:
🌐 Then, in src/main.jsx, make sure you call registerRootComponent and pass in the component you want to render at the root of the app:
对于使用 Expo Router 的项目,你可以按照 Expo Router 安装指南 中的步骤创建自定义入口点。要在你的 Expo Router 项目中使用顶层 src 目录,请参阅 src 目录参考 了解更多信息。