• Stars
    star
    367
  • Rank 112,791 (Top 3 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React Query hooks for managing asynchronous operations with Firebase. Supports Authentication, Analytics, Firestore & Realtime Database.

React Query Firebase

A set of React Query hooks integrating with Firebase.

Installation โ€ข Documentation โ€ข License


React Query Firebase provides a set of easy to use hooks for handling asynchronous tasks with Firebase in your React application.

Why should I use React Query Firebase?

  • Backed by React Query - Unlike other solutions, hooks are built on top of React Query which takes care of complex challenges such as caching, automatic refetching, realtime data subscriptions, pagination & infinite queries, mutations, SSR Support, data selectors, side effect handlers and more. You also get DevTool support out of the box!
  • Un-opinionated - You provide the Query Keys, Configuration & Firebase instances, allowing for full control over how your data is integrated and cached. You can also roll it alongside any existing Firebase usage.
  • Performant & Efficient - Whether your queries are one-off or realtime, the library is designed to be performant and efficient. Data fetching is handled via Queries and Query Keys, meaning components can share data throughout your application without needless database reads.
  • Mutations - Sign a user in, delete a document, run a transaction, log an event... React Query Firebase takes care of that for you via Mutations, allowing you to focus on your application and not managing complex local loading & error states.
  • Fully Typed - The library is built with and has full compatibility with TypeScript.

Note: The library supports the Firebase JS SDK v9 - learn more about it here!

Example

As an example, let's use a Firestore hooks to fetch a document & run a transaction whilst easily handling asynchronous state.

import {
  useFirestoreDocument,
  useFirestoreTransaction,
} from "@react-query-firebase/firestore";
import { doc } from "firebase/firestore";
import { firestore } from "./config/firebase";

type Product = {
  name: string;
  price: number;
};

function ProductPage({ id }: { id: string }) {
  // Create a Firestore document reference
  const ref = doc(firestore, "products", id);

  // Query a Firestore document using useQuery
  const product = useFirestoreDocument<Product>(
    ["product", id],
    ref,
    {
      // Subscribe to realtime changes
      subscribe: true,
      // Include metadata changes in the updates
      includeMetadataChanges: true,
    },
    {
      // Optionally handle side effects with React Query hook options
      onSuccess(snapshot) {
        console.log("Successfully fetched product ID: ", snapshot.id);
      },
    }
  );

  // Run a Firestore transaction as Mutation using useMutation
  const like = useFirestoreTransaction(
    auth,
    async (tsx) => {
      const record = await tsx.get(ref);
      tsx.update(ref, {
        likes: record.data().likes + 1,
      });
    },
    {
      onError(error) {
        console.error("Failed to like product!", error);
      },
    }
  );

  if (product.isLoading) {
    return <div>Loading...</div>;
  }

  if (product.isError) {
    return <div>Failed to fetch product: {product.error.message}</div>;
  }

  const snapshot = product.data; // DocumentSnapshot<Product>

  return (
    <div>
      <h1>{snapshot.data().name}</h1>
      <button disabled={like.isLoading} onClick={() => like.mutate()}>
        Like Product!
      </button>
      {like.isError && <p>Failed to like product: {like.error.message}</p>}
    </div>
  );
}

Installation

If you haven't done so already, install react, react-query & firebase (v9):

npm i --save react react-query firebase

Before using this library, ensure React Query is setup on your project by following the Installation guide.

Next install one of the React Query Firebase packages, e.g:

npm i --save @react-query-firebase/firestore

See below for a full list of available packages.

Packages

License


Built and maintained by Invertase.

More Repositories

1

react-native-firebase

๐Ÿ”ฅ A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
JavaScript
11,434
star
2

rdash-angular

AngularJS implementation of the RDash admin dashboard theme
HTML
4,691
star
3

react-native-material-design

React Native UI Components for Material Design
JavaScript
3,155
star
4

notifee

โš›๏ธ A feature rich notifications library for React Native.
TypeScript
1,712
star
5

react-native-apple-authentication

A React Native library providing support for Apple Authentication on iOS and Android.
JavaScript
1,338
star
6

react-native-firebase-starter

DEPRECATED: For RNFB v5 only.
JavaScript
1,170
star
7

melos

๐ŸŒ‹ A tool for managing Dart projects with multiple packages. With IntelliJ and Vscode IDE support. Supports automated versioning, changelogs & publishing via Conventional Commits.
Dart
1,034
star
8

react-native-google-mobile-ads

React Native Google Mobile Ads enables you to monetize your app with AdMob.
TypeScript
609
star
9

react-native-material-design-demo

An Android app to demonstrate react-native-material-design
JavaScript
494
star
10

react-native-notifee

Moved to https://github.com/invertase/notifee
TypeScript
467
star
11

docs.page

Instant Open Source docs with zero configuration.
TypeScript
424
star
12

stripe-firebase-extensions

Repository of Firebase Extensions built by Stripe.
TypeScript
418
star
13

firestore-ios-sdk-frameworks

โšก Precompiled Firestore iOS SDKs extracted from the Firebase iOS SDK repository release downloads for faster build times.
Ruby
402
star
14

denque

The fastest javascript implementation of a double-ended queue. Used by the official Redis, MongoDB, MariaDB & MySQL libraries for Node.js and many other libraries.
JavaScript
347
star
15

zapp.run

Zapp! A free online sandbox environment for building Flutter applications in the browser.
Dart
309
star
16

spec

โœ… A streamlined testing framework for Dart & Flutter.
Dart
303
star
17

dart_edge

Run Dart on the Edge - supporting Vercel & Cloudflare Workers (more coming soon).
Dart
302
star
18

dart_custom_lint

๐Ÿ’ก Easily write powerful Dart & Flutter lint rules for your projects or for users of your packages.
Dart
269
star
19

angular-toasty

A simple standalone AngularJS module with extensive features that provides growl-style alerts and messages for your app!
CSS
255
star
20

react-native-firebase-docs

DEPRECATED: For RNFB v5 only.
189
star
21

github-action-dart-analyzer

A GitHub action to run Dart analyzer with annotation support.
TypeScript
168
star
22

flutterfire_cli

A CLI to help with using FlutterFire in your Flutter applications.
Dart
160
star
23

react-native-firebase-authentication-example

An example React Native Firebase application integrating authentication.
TypeScript
138
star
24

conference-app

Dart
116
star
25

dart_firebase_admin

๐Ÿ”ฅ A Firebase Admin SDK for Dart.
Dart
103
star
26

jet

โœˆ๏ธ Test your React Native modules e2e; mock-free and native testing code free.
JavaScript
98
star
27

globe

๐ŸŒŽ The global deployment platform for Dart & Flutter applications.
Dart
98
star
28

react-native-gradle-plugin

A gradle plugin for React Native Android that simplifies and standardises build configurations (such as dependency versioning) for Projects & React Native modules.
Groovy
62
star
29

dart_docker

๐Ÿ‹ A Dart client for the Docker API via local unix socket.
Dart
41
star
30

flutter_desktop_webview_auth

WebView OAuth flows for desktop flutter apps
C++
31
star
31

dart_firebase_apis

๐Ÿ”ฅ Generated Firebase APIs for Dart. Additionally Includes private and unlisted APIs.
Dart
29
star
32

firebase-extensions

A collection of Firebase Extensions built by Invertase.
TypeScript
29
star
33

puppeteer-pool

Flexible Puppeteer Browser instance resource pooling.
JavaScript
28
star
34

deeps

Performant utilities to manage deeply nested objects. get, set, flatten, diff etc.
JavaScript
26
star
35

nodejs-paddle-sdk

A fully typed Node.js library for integration with Paddle.
TypeScript
26
star
36

samples

Collection of samples from our projects and tutorials.
Dart
20
star
37

dart-cli-utilities

โš’๏ธ A monorepo containing a collection of packages that provide useful functionality for building CLI applications in Dart.
Dart
20
star
38

cluster-key-slot

Generates CRC hashes for strings - for use by Node Redis clients to determine key slots.
JavaScript
17
star
39

react-native-firebase-workshop-app

Showcasing React Native + Firebase
TypeScript
13
star
40

sails-firebase-auth

A SailsJS v1 JSON starter api with Firebase token authentication.
JavaScript
10
star
41

crc16

Native node addon to calculate CRC16 values.
C++
10
star
42

firebase-firestore-fields

Convert Firebase Cloud Firestore fields into a usable JavaScript format
JavaScript
10
star
43

react-native-auto-link-example

Init a React Native project with auto-linking ๐Ÿ”ฅ
Objective-C
10
star
44

react-native-template

Generic template for Invertase React Native projects
Objective-C
10
star
45

remix-firebase-storage-file-handler

An upload handler for Remix using Firebase Storage
TypeScript
8
star
46

a2a

Async await to Array -> `const [error, value] = await A2A(fooPromise());`
JavaScript
7
star
47

nodejs-google-java-format

Node.js repackaging of Google's native `google-java-format` tool.
JavaScript
7
star
48

dart_globe_examples

Repository containing various Dart examples for Globe.
6
star
49

nx-dart

A Nx plugin, that adds support for developing Dart and Flutter packages in a Nx workspace.
TypeScript
6
star
50

babel-preset-react-native-syntax

๐Ÿ“ฆBabel preset providing syntax only plugins used by babel-preset-react-native - supports React Native ^0.56.0.
JavaScript
5
star
51

firebase-relationship

A simple Firebase Realtime Database relationship manager.
JavaScript
5
star
52

firebase-extension-utilities

TypeScript
4
star
53

sails-ioredis

Redis adapter for waterline with sentinel and cluster support support (ioredis)
JavaScript
4
star
54

tutorials

๐ŸŽ“ A collection of open-source tutorials by us and the open-source community covering topics such as React Native and Firebase.
4
star
55

balanced-employee-ip-agreement

An English law and Open Source friendly Balanced Employee IP Agreement - providing a balanced approach to assigning control of intellectual property (IP) created by UK company employees. See releases for PDF/MD files
4
star
56

flutterfire_ui

Dart
3
star
57

redis-writable

Fast conversion of commands and args to redis protocol writables.
JavaScript
3
star
58

wtfork

Subscribe to and publish events between parent and child node processes using the standard node event emitter api or call parent methods directly from the child process and vice versa.
JavaScript
3
star
59

firebase-functions-python-old

[WIP] Firebase SDK for Cloud Functions and Python.
Python
3
star
60

llm-gcp

Python
3
star
61

extensions-quickstart-js

Firebase Extensions quick start examples
TypeScript
3
star
62

firebase_performance_flutter_example

Testing
Dart
2
star
63

meta

General guidelines and documentation for the Invertase GitHub org.
1
star
64

ios-sdk-10-issues

iOS SDK 10 Firebase Storage issues
Objective-C
1
star
65

react-custom-event

React hook for window CustomEvent
JavaScript
1
star
66

docker-react-native-firebase-android

Dockerfile
1
star
67

oss-discord-community

Nothing to see here.
1
star
68

.github

Default community health files for Invertase.
1
star