使用 Firebase
有关入门和使用 Firebase JS SDK 和 React Native Firebase 库的指南。
Firebase 是一个后端即服务 (BaaS) 应用开发平台,提供托管后端服务,例如实时数据库、云存储、身份验证、崩溃报告、分析等。它建立在 Google 的基础设施之上,并可自动扩展。
¥Firebase is a Backend-as-a-Service (BaaS) app development platform that provides hosted backend services such as real-time database, cloud storage, authentication, crash reporting, analytics, and so on. It is built on Google's infrastructure and scales automatically.
你可以通过两种不同的方式在项目中使用 Firebase:
¥There are two different ways you can use Firebase in your projects:
-
¥Using Firebase JS SDK
-
¥Using React Native Firebase
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 开发你的应用。
¥Want to use Firebase services such as Authentication, Firestore, Realtime Database, and Storage in your app and want to develop your app with Expo Go.
-
想要快速开始使用 Firebase 服务。
¥Want a quick start with Firebase services.
-
想要创建适用于 Android、iOS 和 Web 的通用应用。
¥Want to create a universal app for Android, iOS, and the 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
以下小节使用 firebase@9.x.x
。Expo SDK 不会强制或推荐在你的应用中使用任何特定版本的 Firebase。
¥The following sub-sections use firebase@9.x.x
. Expo SDK does not enforce or recommend any specific version of Firebase to use in your app.
如果你在项目中使用旧版本的 firebase 库,则可能需要在 Firebase JS SDK 文档 的帮助下调整代码示例以匹配你正在使用的版本。
¥If you are using an older version of the firebase library in your project, you may have to adapt the code examples to match the version that you are using with the help of the Firebase JS SDK documentation.
1
Install the SDK
After you have created your Expo project, you can install the Firebase JS SDK using the following command:
-
npx expo install firebase
2
Initialize the SDK in your project
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.
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.
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
You do not have to install other plugins or configurations to use Firebase JS SDK.
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.
Troubleshooting tip: If you encounter issues related to authentication persistence with Firebase JS SDK, see the guide for setting up persistence to keep users logged in between reloads.
3
Configure Metro
If you are using Firebase version9.7.x
and above, you need to add the following configuration to a metro.config.js file to make sure that the Firebase JS SDK is bundled correctly.
Expo CLI uses Metro to bundle your JavaScript code and assets, and add support for more file extensions.
Start by generating the template file metro.config.js in your project's root directory using the following command:
-
npx expo customize metro.config.js
Then, update the file with the following configuration:
const { getDefaultConfig } = require('@expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push('cjs');
module.exports = defaultConfig;
Next steps
有关如何在项目中使用身份验证的更多信息,请参阅 Firebase 文档。
有关如何在项目中使用 Firestore 数据库的更多信息,请参阅 Firebase 文档。
有关如何在项目中使用实时数据库的更多信息,请参阅 Firebase 文档。
有关如何使用存储的更多信息,请参阅 Firebase 文档。
通过我们的示例了解如何在 Expo 项目中使用 Firebase Storage。
有关在 Firebase 项目中管理 API 密钥和唯一标识符的详细信息。
有关从 expo-firebase-analytics 或 expo-firebase-recaptcha 包迁移到 React Native Firebase 的更多信息。
Using React Native Firebase
React Native Firebase provides access to native code by wrapping the native SDKs for Android and iOS into a JavaScript API.
Each Firebase service is available as a module that can be added as a dependency to your project. For example, the auth
module provides access to the Firebase Authentication service.
When to use React Native Firebase
You can consider using React Native Firebase when:
- Your app requires access to Firebase services not supported by the Firebase JS SDK, such as Dynamic Links, Crashlytics, and so on. For more information on the additional capabilities offered by the native SDK's, see React Native Firebase documentation.
- You want to use native SDKs in your app.
- You have a bare React Native app with React Native Firebase already configured but are migrating to use Expo SDK.
- You want to use Firebase Analytics in your app.
Migrating from Expo Firebase packages?
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 requires custom native code and cannot be used with Expo Go.
Install and initialize React Native Firebase
1
Install expo-dev-client
Since React Native Firebase requires custom native code, you need to install the expo-dev-client
library in your project.
It also allows configuring any native code required by React Native Firebase using Config plugins without writing native code yourself.
To install expo-dev-client
, run the following command in your project:
-
npx expo install expo-dev-client
2
Install React Native Firebase
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
At this point, you must follow the instructions from React Native Firebase documentation as it covers all the steps required to configure your project with the library.
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
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.
Run project locally?
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.
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 特定模块的更多信息,我们建议你查看他们的文档。