使用 Firebase
有关入门和使用 Firebase JS SDK 和 React Native Firebase 库的指南。
Firebase 是一个后端即服务(BaaS)应用开发平台,提供托管的后端服务,例如实时数据库、云存储、身份验证、崩溃报告、分析等。它建立在谷歌的基础设施上,并能够自动扩展。
你可以通过两种不同的方式在项目中使用 Firebase:
🌐 There are two different ways you can use Firebase in your projects:
React Native 同时支持 JS SDK 和原生 SDK。以下各节将指导你何时使用哪种 SDK,以及在 Expo 项目中使用 Firebase 所需的所有配置步骤。
🌐 React Native supports both the JS SDK and the native SDK. The following sections will guide you through when to use which SDK and all the configuration steps required to use Firebase in your Expo projects.
先决条件
🌐 Prerequisites
在继续之前,请确保你已经创建了一个新的 Firebase 项目,或使用 Firebase 控制台 拥有现有项目。
🌐 Before proceeding, make sure that you have created a new Firebase project or have an existing one using the Firebase console.
使用 Firebase JS SDK
🌐 Using Firebase JS SDK
Firebase JS SDK 是一个 JavaScript 库,允许你在项目中与 Firebase 服务进行交互。它支持在 React Native 应用中使用 身份验证、Firestore、实时数据库 和 存储 等服务。
🌐 The Firebase JS SDK is a JavaScript library that allows you to interact with Firebase services in your project. It supports services such as Authentication, Firestore, Realtime Database, and Storage in a React Native app.
何时使用 Firebase JS SDK
🌐 When to use Firebase JS SDK
当你满足以下条件时,可以考虑使用 Firebase JS SDK:
🌐 You can consider using the Firebase JS SDK when you:
- 想在你的应用中使用 Firebase 服务,如身份验证、Firestore、实时数据库和存储,并希望通过 Expo Go 开发你的应用。
- 想要快速开始使用 Firebase 服务。
- 想要创建适用于 Android、iOS 和 Web 的通用应用。
注意事项
🌐 Caveats
Firebase JS SDK 并不支持移动应用的所有服务。其中一些服务包括 Analytics、Dynamic Links 和 Crashlytics。如果你想使用这些服务,请参阅 React Native Firebase 部分。
🌐 Firebase JS SDK does not support all services for mobile apps. Some of these services are Analytics, Dynamic Links and Crashlytics. See the React Native Firebase section if you want to use these services.
安装并初始化 Firebase JS SDK
🌐 Install and initialize Firebase JS SDK
警告 Expo SDK 53 及更高版本仅支持
firebase@^12.0.0。在此版本之前的版本会导致 ES 模块解析错误。
1
安装 SDK
🌐 Install the SDK
在你创建了你的 Expo 项目 后,你可以使用以下命令安装 Firebase JS SDK:
🌐 After you have created your Expo project, you can install the Firebase JS SDK using the following command:
- npx expo install firebase2
在你的项目中初始化 SDK
🌐 Initialize the SDK in your project
要在你的 Expo 项目中初始化 Firebase 实例,你必须创建一个配置对象,并将其传递给从 firebase/app 模块导入的 initializeApp() 方法。
🌐 To initialize the Firebase instance in your Expo project, you must create a config object and pass it to the initializeApp() method imported from the firebase/app module.
配置对象需要一个 API 密钥和其他唯一标识符。要获取这些值,你需要在 Firebase 项目中注册一个 Web 应用。你可以在 Firebase 文档 中找到这些说明。
🌐 The config object requires an API key and other unique identifiers. To obtain these values, you will have to register a web app in your Firebase project. You can find these instructions in the Firebase documentation.
在获取 API 密钥和其他标识符后,你可以通过在项目的根目录或存放配置文件的其他目录中创建一个新的 firebaseConfig.js 文件,然后粘贴以下代码片段。
🌐 After you have the API key and other identifiers, you can paste the following code snippet by creating a new firebaseConfig.js file in your project's root directory or any other directory where you keep the configuration files.
import { initializeApp } from 'firebase/app'; // Optionally import the services that you want to use // import {...} from 'firebase/auth'; // import {...} from 'firebase/database'; // import {...} from 'firebase/firestore'; // import {...} from 'firebase/functions'; // import {...} from 'firebase/storage'; // Initialize Firebase const firebaseConfig = { apiKey: 'api-key', authDomain: 'project-id.firebaseapp.com', databaseURL: 'https://project-id.firebaseio.com', projectId: 'project-id', storageBucket: 'project-id.appspot.com', messagingSenderId: 'sender-id', appId: 'app-id', measurementId: 'G-measurement-id', }; const app = initializeApp(firebaseConfig); // For more information on how to access Firebase in your project, // see the Firebase documentation: https://firebase.google.com/docs/web/setup#access-firebase
使用 Firebase JS SDK 无需安装其他插件或进行额外配置。
🌐 You do not have to install other plugins or configurations to use Firebase JS SDK.
Firebase 版本 9 及以上提供了模块化 API。你可以直接从 firebase 包中导入你想使用的任何服务。例如,如果你想在项目中使用身份验证服务,你可以从 firebase/auth 包中导入 auth 模块。
🌐 Firebase version 9 and above provide a modular API. You can directly import any service you want to use from the firebase package. For example, if you want to use an authentication service in your project, you can import the auth module from the firebase/auth package.
信息 故障排除提示: 如果你遇到与 Firebase JS SDK 身份验证持久性相关的问题,请参阅设置持久性以在页面重新加载之间保持用户登录指南。
下一步
🌐 Next steps
有关如何在项目中使用身份验证的更多信息,请参阅 Firebase 文档。
有关如何在项目中使用 Firestore 数据库的更多信息,请参阅 Firebase 文档。
有关如何在项目中使用实时数据库的更多信息,请参阅 Firebase 文档。
有关如何使用存储的更多信息,请参阅 Firebase 文档。
通过我们的示例,了解如何在 Expo 项目中使用 Firebase Storage。
有关在 Firebase 项目中管理 API 密钥和唯一标识符的更多信息。
有关从 expo-firebase-analytics 或 expo-firebase-recaptcha 包迁移到 React Native Firebase 的更多信息。
使用 React Native Firebase
🌐 Using React Native Firebase
React Native Firebase 通过将 Android 和 iOS 的原生 SDK 封装成 JavaScript API,提供对原生代码的访问。每个 Firebase 服务都作为一个模块提供,可以作为依赖添加到你的项目中。例如,auth 模块提供对 Firebase 身份验证服务的访问。
何时使用 React Native Firebase
🌐 When to use React Native Firebase
当你在以下情况下可以考虑使用 React Native Firebase 时:
🌐 You can consider using React Native Firebase when:
- 你的应用需要访问 Firebase JS SDK 不支持的 Firebase 服务,例如 动态链接、Crashlytics 等。有关原生 SDK 提供的额外功能的更多信息,请参阅 React Native Firebase 文档。
- 你想在应用中使用原生 SDK。
- 你有一个已经配置了 React Native Firebase 的基础 React Native 应用,但正在迁移以使用 Expo SDK。
- 你想在你的应用中使用 Firebase Analytics。
从 Expo Firebase 包迁移吗?
如果你的项目之前使用了 expo-firebase-analytics 和 expo-firebase-recaptcha 包,你可以迁移到 React Native Firebase 库。更多信息,请参见 Firebase 迁移指南。
🌐 If your project has been previously using expo-firebase-analytics and expo-firebase-recaptcha packages, you can migrate to the React Native Firebase library. For more information, see Firebase migration guide.
注意事项
🌐 Caveats
React Native Firebase 需要自定义原生代码,不能与 Expo Go 一起使用。
🌐 React Native Firebase requires custom native code and cannot be used with Expo Go.
安装并初始化 React Native Firebase
🌐 Install and initialize React Native Firebase
1
安装 expo-dev-client
🌐 Install expo-dev-client
由于 React Native Firebase 需要自定义原生代码,你需要在项目中安装 expo-dev-client 库。它允许使用 Config 插件 配置 React Native Firebase 所需的任何原生代码,而无需自己编写原生代码。
🌐 Since React Native Firebase requires custom native code, you need to install the expo-dev-client library in your project.
It allows configuring any native code required by React Native Firebase using Config plugins without writing native code yourself.
要在你的项目中安装 expo-dev-client,请运行以下命令:
🌐 To install expo-dev-client, run the following command in your project:
- npx expo install expo-dev-client2
安装 React Native Firebase
🌐 Install React Native Firebase
要使用 React Native Firebase,必须安装 @react-native-firebase/app 模块。该模块为所有其他模块提供核心功能。同时,它还通过配置插件在你的项目中添加自定义的本地代码。你可以使用以下命令进行安装:
🌐 To use React Native Firebase, it is necessary to install the @react-native-firebase/app module. This module provides the core functionality for all other modules.
It also adds custom native code in your project using a config plugin. You can install it using the following command:
- npx expo install @react-native-firebase/app此时,你必须遵循 React Native Firebase 文档 中的说明,因为它涵盖了配置你的项目所需的所有步骤。
在你的项目中配置好 React Native Firebase 库后,请返回本指南,了解下一步如何运行你的项目。
🌐 Once you have configured the React Native Firebase library in your project, come back to this guide to learn how to run your project in the next step.
3
运行项目
🌐 Run the project
如果你正在使用 EAS Build,你可以在设备上创建并安装开发构建。在创建开发构建之前,你不需要在本地运行项目。 有关创建开发构建的更多信息,请参阅安装开发构建部分。
🌐 If you are using EAS Build, you can create and install a development build on your devices. You do not need to run the project locally before creating a development build. For more information on creating a development build, see the section on installing a development build.
本地运行项目?
如果你想在本地运行该项目,你需要在你的电脑上安装并配置好 Android Studio 和 Xcode。更多信息请参见 本地应用开发 指南。
🌐 If you want to run the project locally, you need both Android Studio and Xcode installed and configured on your machine. See Local app development guide for more information.
如果某个特定的 React Native Firebase 模块需要自定义的原生配置步骤,你必须将其作为 plugin 添加到 应用配置 文件中。然后,要在本地运行项目,请在运行 npx expo run 命令之前执行 npx expo prebuild --clean 命令以应用原生更改。
🌐 If a particular React Native Firebase module requires custom native configuration steps, you must add it as a plugin to app config file. Then, to run the project locally, run the npx expo prebuild --clean command to apply the native changes before the npx expo run commands.
下一步
🌐 Next steps
配置 React Native Firebase 库后,你可以在 Expo 项目中使用它提供的任何模块。
🌐 After configuring React Native Firebase library, you can use any module it provides in your Expo project.
有关安装和使用 React Native Firebase 中某个模块的更多信息,我们建议你查阅他们的文档。