reactfire
Hooks, Context Providers, and Components that make it easy to interact with Firebase.
Top Related Projects
Firebase Javascript SDK
🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
React Hooks for Firebase.
🔥 A collection of Firebase plugins for Flutter apps.
Quick Overview
ReactFire is an official library that provides React components and hooks for easy integration with Firebase services. It simplifies the process of using Firebase in React applications, offering a declarative approach to handle authentication, real-time database operations, and other Firebase features.
Pros
- Seamless integration with React and Firebase
- Provides hooks and components for easy state management
- Supports real-time updates out of the box
- Reduces boilerplate code for common Firebase operations
Cons
- Limited documentation and examples for advanced use cases
- Dependency on Firebase ecosystem may limit flexibility
- Learning curve for developers new to Firebase concepts
- May introduce unnecessary complexity for simple applications
Code Examples
- Using the
useSigninCheck
hook for authentication:
import { useSigninCheck } from 'reactfire';
function AuthenticationStatus() {
const { status, data: signInCheckResult } = useSigninCheck();
if (status === 'loading') {
return <span>Loading...</span>;
}
if (signInCheckResult.signedIn === true) {
return <span>You're signed in!</span>;
} else {
return <span>You're not signed in.</span>;
}
}
- Using the
useFirestore
hook to fetch data:
import { useFirestore, useFirestoreCollectionData } from 'reactfire';
function TodoList() {
const firestore = useFirestore();
const todosCollection = firestore.collection('todos');
const { status, data: todos } = useFirestoreCollectionData(todosCollection);
if (status === 'loading') {
return <span>Loading...</span>;
}
return (
<ul>
{todos.map((todo) => (
<li key={todo.id}>{todo.text}</li>
))}
</ul>
);
}
- Using the
FirebaseAppProvider
component to initialize Firebase:
import { FirebaseAppProvider } from 'reactfire';
const firebaseConfig = {
// Your Firebase configuration object
};
function App() {
return (
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
{/* Your app components */}
</FirebaseAppProvider>
);
}
Getting Started
-
Install ReactFire and Firebase:
npm install reactfire firebase
-
Wrap your app with
FirebaseAppProvider
:import { FirebaseAppProvider } from 'reactfire'; import { initializeApp } from 'firebase/app'; const firebaseConfig = { // Your Firebase configuration }; const app = initializeApp(firebaseConfig); function App() { return ( <FirebaseAppProvider firebaseApp={app}> {/* Your app components */} </FirebaseAppProvider> ); }
-
Use ReactFire hooks in your components:
import { useFirestore, useFirestoreCollectionData } from 'reactfire'; function MyComponent() { const firestore = useFirestore(); const { status, data } = useFirestoreCollectionData(firestore.collection('items')); // Render your component using the data }
Competitor Comparisons
Firebase Javascript SDK
Pros of firebase-js-sdk
- More comprehensive and flexible, covering all Firebase services
- Can be used with any JavaScript framework or vanilla JS
- Regularly updated with new Firebase features and improvements
Cons of firebase-js-sdk
- Steeper learning curve for React developers
- Requires more boilerplate code for React integration
- Lacks React-specific optimizations and hooks
Code Comparison
reactfire:
import { useFirestore, useFirestoreCollectionData } from 'reactfire';
function TodoList() {
const firestore = useFirestore();
const todosCollection = firestore.collection('todos');
const { data: todos } = useFirestoreCollectionData(todosCollection);
return <ul>{todos.map(todo => <li key={todo.id}>{todo.text}</li>)}</ul>;
}
firebase-js-sdk:
import { useEffect, useState } from 'react';
import { getFirestore, collection, onSnapshot } from 'firebase/firestore';
function TodoList() {
const [todos, setTodos] = useState([]);
useEffect(() => {
const db = getFirestore();
const unsubscribe = onSnapshot(collection(db, 'todos'), snapshot => {
setTodos(snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })));
});
return () => unsubscribe();
}, []);
return <ul>{todos.map(todo => <li key={todo.id}>{todo.text}</li>)}</ul>;
}
🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
Pros of react-native-firebase
- Specifically designed for React Native, offering native Firebase integration
- Comprehensive support for various Firebase services, including Analytics and Crashlytics
- Active community and frequent updates
Cons of react-native-firebase
- Limited to React Native development, not suitable for web-based React projects
- Steeper learning curve due to more complex setup and configuration
- Larger bundle size compared to reactfire
Code Comparison
reactfire:
import { useFirestore, useFirestoreCollectionData } from 'reactfire';
function App() {
const firestore = useFirestore();
const usersCollection = firestore.collection('users');
const { data: users } = useFirestoreCollectionData(usersCollection);
}
react-native-firebase:
import firestore from '@react-native-firebase/firestore';
function App() {
const [users, setUsers] = useState([]);
useEffect(() => {
const subscriber = firestore().collection('users').onSnapshot(querySnapshot => {
setUsers(querySnapshot.docs.map(doc => doc.data()));
});
return () => subscriber();
}, []);
}
Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
Pros of react-redux-firebase
- Integrates seamlessly with Redux, allowing for centralized state management
- Provides more extensive features for complex applications, including authentication helpers and population of nested data
- Offers a wider range of hooks and HOCs for easier integration with React components
Cons of react-redux-firebase
- Steeper learning curve due to Redux integration and more complex API
- Potentially heavier bundle size due to additional dependencies
- May be overkill for simpler applications that don't require Redux
Code Comparison
reactfire:
import { useFirestoreCollection } from 'reactfire';
function TodoList() {
const todos = useFirestoreCollection(firestore.collection('todos'));
return <ul>{todos.map(todo => <li key={todo.id}>{todo.text}</li>)}</ul>;
}
react-redux-firebase:
import { useFirestoreConnect } from 'react-redux-firebase';
import { useSelector } from 'react-redux';
function TodoList() {
useFirestoreConnect([{ collection: 'todos' }]);
const todos = useSelector(state => state.firestore.ordered.todos);
return <ul>{todos.map(todo => <li key={todo.id}>{todo.text}</li>)}</ul>;
}
Both libraries provide hooks for easy integration with Firebase, but react-redux-firebase requires Redux setup and uses a different approach to connect and retrieve data.
React Hooks for Firebase.
Pros of react-firebase-hooks
- Simpler API with fewer abstractions, making it easier to understand and use
- More focused on core Firebase functionality, potentially leading to a smaller bundle size
- Actively maintained with regular updates and community support
Cons of react-firebase-hooks
- Less comprehensive coverage of Firebase features compared to reactfire
- Lacks some advanced features and optimizations present in reactfire
- May require more manual setup and configuration for certain use cases
Code Comparison
reactfire:
import { useFirestoreCollectionData } from 'reactfire';
function MyComponent() {
const { status, data } = useFirestoreCollectionData(myCollectionRef);
return status === 'loading' ? 'Loading...' : <ul>{data.map(/* ... */)}</ul>;
}
react-firebase-hooks:
import { useCollection } from 'react-firebase-hooks/firestore';
function MyComponent() {
const [value, loading, error] = useCollection(myCollectionRef);
return loading ? 'Loading...' : <ul>{value.docs.map(/* ... */)}</ul>;
}
Both libraries provide hooks for interacting with Firebase, but reactfire offers a more abstracted approach with additional features, while react-firebase-hooks provides a simpler, more direct interface to Firebase functionality. The choice between them depends on the specific needs of your project and your preferred level of abstraction.
🔥 A collection of Firebase plugins for Flutter apps.
Pros of FlutterFire
- More comprehensive Firebase integration, covering a wider range of Firebase services
- Better documentation and more extensive examples for Flutter developers
- Actively maintained by the official Firebase team, ensuring faster updates and better support
Cons of FlutterFire
- Steeper learning curve due to more complex API and extensive features
- Potentially larger app size due to the inclusion of multiple Firebase services
- May require more setup and configuration compared to ReactFire
Code Comparison
ReactFire:
import { useFirestore, useFirestoreCollectionData } from 'reactfire';
function App() {
const firestore = useFirestore();
const usersCollection = firestore.collection('users');
const { data: users } = useFirestoreCollectionData(usersCollection);
}
FlutterFire:
import 'package:cloud_firestore/cloud_firestore.dart';
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
// Handle snapshot data
},
);
}
}
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ReactFire
Hooks, Context Providers, and Components that make it easy to interact with Firebase.
What is ReactFire?
- Easy realtime updates for your function components - Hooks
like
useUser
anduseFirestoreCollection
let you easily subscribe to auth state, realtime data, and all other Firebase SDK events. Plus, they automatically unsubscribe when your component unmounts. - Access Firebase libraries from any component - Need the Firestore SDK?
useFirestore
. Remote Config?useRemoteConfig
. - Safely configure Firebase libraries - Libraries like Firestore and Remote Config require settings like
enablePersistence
to be set before any data fetches are made. This can be tough to support in React's world of re-renders. ReactFire gives youuseInitFirestore
anduseInitRemoteConfig
hooks that guarantee they're set before anything else.
Install
# npm
npm install --save firebase reactfire
# or
# yarn
yarn add firebase reactfire
Depending on your targeted platforms you may need to install polyfills. The most commonly needed will be globalThis and Proxy.
Docs
Example use
Check out the live version on StackBlitz!
import React from 'react';
import { render } from 'react-dom';
import { doc, getFirestore } from 'firebase/firestore';
import { FirebaseAppProvider, FirestoreProvider, useFirestoreDocData, useFirestore, useFirebaseApp } from 'reactfire';
const firebaseConfig = {
/* Add in your config object from the Firebase console */
};
function BurritoTaste() {
// access the Firestore library
const burritoRef = doc(useFirestore(), 'tryreactfire', 'burrito');
// subscribe to a document for realtime updates. just one line!
const { status, data } = useFirestoreDocData(burritoRef);
// check the loading status
if (status === 'loading') {
return <p>Fetching burrito flavor...</p>;
}
return <p>The burrito is {data.yummy ? 'good' : 'bad'}!</p>;
}
function App() {
const firestoreInstance = getFirestore(useFirebaseApp());
return (
<FirestoreProvider sdk={firestoreInstance}>
<h1>ð¯</h1>
<BurritoTaste />
</FirestoreProvider>
);
}
render(
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
<App />
</FirebaseAppProvider>,
document.getElementById('root')
);
Status
This repository is maintained by Googlers but is not a supported Firebase product. Issues here are answered by maintainers and other community members on GitHub on a best-effort basis.
Extra Experimental concurrent mode features
These features are marked as extra experimental because they use experimental React features that will not be stable until sometime after React 18 is released.
- Loading states handled by
<Suspense>
- ReactFire's hooks throw promises that Suspense can catch. Let React handle loading states for you. - Automatically instrument your
Suspense
load times - Need to automatically instrument yourSuspense
load times with RUM? Use<SuspenseWithPerf />
.
Enable concurrent mode features by following the concurrent mode setup guide and then setting the suspense
prop in FirebaseAppProvider
:
<FirebaseAppProvider firebaseConfig={firebaseConfig} suspense={true}>
See concurrent mode code samples in example/withSuspense
Top Related Projects
Firebase Javascript SDK
🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
React Hooks for Firebase.
🔥 A collection of Firebase plugins for Flutter apps.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot