The Ultimate Firebase & React Native Cheat Sheet for Beginners

Devbrite
3 min readAug 23, 2023

--

Firebase and React Native are a powerhouse combo for app development. Firebase provides the backend, while React Native handles the frontend. In this cheat sheet, you’ll get to know the basics and beyond.

Table of Contents:

  1. Setting Up Firebase & React Native.
  2. Authentication.
  3. Real-time Database.
  4. Cloud Firestore.
  5. Storage.
  6. Deploying and Monitoring.

If you want to skip months of development for apps with React Native and Firebase backend, check out Instamobile’s free templates.

1. Setting Up Firebase & React Native

1.1 Initializing a new React Native project:

npx react-native init FirebaseRNApp

1.2 Installing Firebase SDK:

npm install @react-native-firebase/app

1.3 Linking Firebase to your app:

Head over to Firebase Console, create a new project, then add an Android and/or iOS app. Follow the provided instructions for setup.

2. Authentication

Firebase offers email/password, Google, Facebook, Twitter, and more for authentication.

2.1 Email & Password Authentication:

Installation:

npm install @react-native-firebase/auth

Usage:

import auth from '@react-native-firebase/auth';
// Sign up
const signUp = async (email, password) => {
try {
await auth().createUserWithEmailAndPassword(email, password);
} catch (error) {
console.error(error);
}
};
// Sign in
const signIn = async (email, password) => {
try {
await auth().signInWithEmailAndPassword(email, password);
} catch (error) {
console.error(error);
}
};

3. Real-time Database

3.1 Setting up:

npm install @react-native-firebase/database

3.2 Usage:

import database from '@react-native-firebase/database';
// Add data
const addData = async () => {
await database().ref('/users/123').set({name: 'John Doe', age: 30});
};
// Fetch data
const fetchData = async () => {
const user = await database().ref('/users/123').once('value');
console.log(user.val());
};

4. Cloud Firestore

4.1 Setting up:

npm install @react-native-firebase/firestore

4.2 Usage:

import firestore from '@react-native-firebase/firestore';
// Add data
const addData = async () => {
await firestore().collection('users').add({name: 'John Doe', age: 30});
};
// Fetch data
const fetchData = async () => {
const userDocument = await firestore().collection('users').doc('ABC123').get();
console.log(userDocument.data());
};

5. Storage

Store files like images, audios, and videos.

5.1 Setting up:

npm install @react-native-firebase/storage

5.2 Usage:

import storage from '@react-native-firebase/storage';
// Upload file
const uploadFile = async (uri) => {
const reference = storage().ref('myFile.jpg');
await reference.putFile(uri);
};
// Download file
const downloadFile = async () => {
const url = await storage().ref('myFile.jpg').getDownloadURL();
console.log(url);
};

6. Deploying and Monitoring

Firebase provides analytics, crash reports, performance insights, etc., with Firebase’s tools like Crashlytics and Analytics.

6.1 Setting up:

npm install @react-native-firebase/analytics
npm install @react-native-firebase/crashlytics

6.2 Usage:

import analytics from '@react-native-firebase/analytics';
import crashlytics from '@react-native-firebase/crashlytics';
// Log an event
analytics().logEvent('button_click');
// Record a custom error
crashlytics().recordError(new Error('An error occurred!'));

Conclusion: This cheat sheet offers a brief look into integrating Firebase services with React Native. The combination of Firebase and React Native accelerates mobile app development, ensuring you focus more on user experience rather than backend intricacies.

SEO Optimization:

  • Title: Contains target keywords “Firebase”, “React Native”, and “Cheat Sheet”.
  • Subtitles: Clearly divided into actionable sections for improved UX and scannability.
  • Keywords: Incorporated relevant keywords such as “app development”, “authentication”, “real-time database”, “Cloud Firestore”, and more.
  • Links: Internal linking to official Firebase documentation can further improve SERP rankings.
  • Content: Comprehensive, valuable content that addresses user intent.

Note: Real-world deployment might require additional configurations and error-handling mechanisms. Always refer to the official documentation for in-depth understanding and updates.

--

--