|
1 | 1 | > [!IMPORTANT]
|
2 |
| -> This project is currently undergoing a overhaul, to support TanStack v5 - this includes a package restructuring, and enables support for other web frameworks! |
| 2 | +> This project is currently a work in progress. Please check back soon for updates! |
3 | 3 |
|
4 |
| -<h1 align="center">React Query Firebase</h1> |
| 4 | +<h1 align="center">TanStack Query Firebase</h1> |
5 | 5 | <p align="center">
|
6 |
| - <span>A set of <a href="https://react-query.tanstack.com">React Query</a> hooks integrating with <a href="https://firebase.google.com/">Firebase</a>.</span> |
| 6 | + <span>A set of <a href="https://tanstack.com/query/latest">TanStack Query</a> hooks integrating with <a href="https://firebase.google.com/">Firebase</a>.</span> |
7 | 7 | </p>
|
8 | 8 | <p align="center">
|
9 | 9 | <span><a href="#installation">Installation</a> •
|
10 |
| - <a href="https://react-query-firebase.invertase.dev/"> Documentation</a> • |
| 10 | + <a href="https://invertase.docs.page/tanstack-query-firebase"> Documentation</a> • |
11 | 11 | <a href="/LICENSE.md">License</a></span>
|
12 | 12 | </p>
|
13 | 13 | <br />
|
14 | 14 |
|
15 |
| -React Query Firebase provides a set of easy to use hooks for handling asynchronous tasks with Firebase in your React application. |
16 |
| - |
17 |
| -## Why should I use React Query Firebase? |
18 |
| - |
19 |
| -- **Backed by React Query** - Unlike other solutions, hooks are built on top of [React Query](https://react-query.tanstack.com) which takes care of complex challenges |
20 |
| - 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](https://react-query.tanstack.com/devtools) |
21 |
| - support out of the box! |
22 |
| -- **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. |
23 |
| -- **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](https://react-query.tanstack.com/guides/queries) and |
24 |
| - [Query Keys](https://react-query.tanstack.com/guides/query-keys), meaning components can share data throughout your application without needless database reads. |
25 |
| -- **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](https://react-query.tanstack.com/guides/mutations), allowing you to focus |
26 |
| - on your application and not managing complex local loading & error states. |
27 |
| -- **Fully Typed** - The library is built with and has full compatibility with TypeScript. |
28 |
| - |
29 |
| -> **Note**: The library supports the Firebase JS SDK v9 - [learn more about it here](https://firebase.googleblog.com/2021/08/the-new-firebase-js-sdk-now-ga.html)! |
30 |
| -
|
31 |
| -## Example |
32 |
| - |
33 |
| -As an example, let's use a Firestore hooks to fetch a document & run a transaction whilst easily handling asynchronous state. |
34 |
| - |
35 |
| -```tsx |
36 |
| -import { |
37 |
| - useFirestoreDocument, |
38 |
| - useFirestoreTransaction, |
39 |
| -} from "@react-query-firebase/firestore"; |
40 |
| -import { doc } from "firebase/firestore"; |
41 |
| -import { firestore } from "./config/firebase"; |
42 |
| - |
43 |
| -type Product = { |
44 |
| - name: string; |
45 |
| - price: number; |
46 |
| -}; |
47 |
| - |
48 |
| -function ProductPage({ id }: { id: string }) { |
49 |
| - // Create a Firestore document reference |
50 |
| - const ref = doc(firestore, "products", id); |
51 |
| - |
52 |
| - // Query a Firestore document using useQuery |
53 |
| - const product = useFirestoreDocument<Product>( |
54 |
| - ["product", id], |
55 |
| - ref, |
56 |
| - { |
57 |
| - // Subscribe to realtime changes |
58 |
| - subscribe: true, |
59 |
| - // Include metadata changes in the updates |
60 |
| - includeMetadataChanges: true, |
61 |
| - }, |
62 |
| - { |
63 |
| - // Optionally handle side effects with React Query hook options |
64 |
| - onSuccess(snapshot) { |
65 |
| - console.log("Successfully fetched product ID: ", snapshot.id); |
66 |
| - }, |
67 |
| - } |
68 |
| - ); |
69 |
| - |
70 |
| - // Run a Firestore transaction as Mutation using useMutation |
71 |
| - const like = useFirestoreTransaction( |
72 |
| - auth, |
73 |
| - async (tsx) => { |
74 |
| - const record = await tsx.get(ref); |
75 |
| - tsx.update(ref, { |
76 |
| - likes: record.data().likes + 1, |
77 |
| - }); |
78 |
| - }, |
79 |
| - { |
80 |
| - onError(error) { |
81 |
| - console.error("Failed to like product!", error); |
82 |
| - }, |
83 |
| - } |
84 |
| - ); |
85 |
| - |
86 |
| - if (product.isLoading) { |
87 |
| - return <div>Loading...</div>; |
88 |
| - } |
89 |
| - |
90 |
| - if (product.isError) { |
91 |
| - return <div>Failed to fetch product: {product.error.message}</div>; |
92 |
| - } |
93 |
| - |
94 |
| - const snapshot = product.data; // DocumentSnapshot<Product> |
95 |
| - |
96 |
| - return ( |
97 |
| - <div> |
98 |
| - <h1>{snapshot.data().name}</h1> |
99 |
| - <button disabled={like.isLoading} onClick={() => like.mutate()}> |
100 |
| - Like Product! |
101 |
| - </button> |
102 |
| - {like.isError && <p>Failed to like product: {like.error.message}</p>} |
103 |
| - </div> |
104 |
| - ); |
105 |
| -} |
106 |
| -``` |
| 15 | +TanStack Query Firebase provides a set of hooks for handling asynchronous tasks with Firebase in your applications. |
107 | 16 |
|
108 |
| -## Installation |
| 17 | +> Looking for React Query Firebase? Check out the [old branch](https://github.com/invertase/tanstack-query-firebase/tree/react-query-firebase). |
109 | 18 |
|
110 |
| -If you haven't done so already, install `react`, `react-query` & `firebase` (v9): |
| 19 | +## Why use this library? |
111 | 20 |
|
112 |
| -```bash |
113 |
| -npm i --save react react-query firebase |
114 |
| -``` |
| 21 | +When managing Firebase’s asynchronous API calls within your application, state synchronization can become cumbersome in most applications. You will commonly find yourself handling loading states, error states, and data synchronization manually. |
| 22 | + |
| 23 | +This library provides a hands-off approach to these problems, by leveraging the popular [TanStack Query](https://tanstack.com/query/latest) project. Out of the box, you get: |
115 | 24 |
|
116 |
| -Before using this library, ensure React Query is setup on your project by following the [Installation](https://react-query.tanstack.com/quick-start) guide. |
| 25 | +- **Automatic Caching**: Avoid redundant Firebase calls with built-in caching. |
| 26 | +- **Out-of-the-box Synchronization**: TanStack Query keeps your UI in sync with the Firebase backend effortlessly. |
| 27 | +- **Background Updates**: Fetch and sync data seamlessly in the background without interrupting the user experience. |
| 28 | +- **Error Handling & Retries**: Get automatic retries on failed Firebase calls, with robust error handling baked in. |
| 29 | +- **Dev Tools for Debugging**: Leverage the React Query Devtools to gain insights into your data-fetching logic and Firebase interactions. |
117 | 30 |
|
118 |
| -Next install one of the React Query Firebase packages, e.g: |
| 31 | +By combining Firebase with TanStack Query, you can make your app more resilient, performant, and scalable, all while writing less code. |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +This project expects you have `firebase` installed as a peer dependency. If you haven't done so already, install `firebase`: |
119 | 36 |
|
120 | 37 | ```bash
|
121 |
| -npm i --save @react-query-firebase/firestore |
| 38 | +npm i --save firebase |
122 | 39 | ```
|
123 | 40 |
|
124 |
| -See below for a full list of available packages. |
| 41 | +Next, install specific packages for your framework of choice: |
| 42 | + |
| 43 | +### React |
125 | 44 |
|
126 |
| -## Packages |
| 45 | +``` |
| 46 | +npm i --save @tanstack/react-query @tanstack-query-firebase/react |
| 47 | +``` |
127 | 48 |
|
128 |
| -- [`@react-query-firebase/analytics`](https://react-query-firebase.invertase.dev/analytics) |
129 |
| -- [`@react-query-firebase/auth`](https://react-query-firebase.invertase.dev/auth) |
130 |
| -- [`@react-query-firebase/database`](https://react-query-firebase.invertase.dev/database) |
131 |
| -- [`@react-query-firebase/firestore`](https://react-query-firebase.invertase.dev/firestore) |
132 |
| -- [`@react-query-firebase/functions`](https://react-query-firebase.invertase.dev/functions) |
| 49 | +See the [Documentation](https://invertase.docs.page/tanstack-query-firebase/react) for more information on how to use the library. |
| 50 | + |
| 51 | +## Status |
| 52 | + |
| 53 | +The status of the following Firebase services and frameworks are as follows: |
| 54 | + |
| 55 | +- ✅ Ready for use |
| 56 | +- 🟠 Work in progress |
| 57 | +- () Not yet started |
| 58 | + |
| 59 | +| Module | React | Vue | Solid | Angular | Svelte | |
| 60 | +|----------------|--:-:--|--:-:--|--:-:--|--:-:----|--:-:---| |
| 61 | +| analytics | | | | | | |
| 62 | +| app-check | | | | | | |
| 63 | +| auth | 🟠 | | | | | |
| 64 | +| database | | | | | | |
| 65 | +| data-connect | ✅ | | | | | |
| 66 | +| firestore | 🟠 | | | | | |
| 67 | +| firestore/lite | | | | | | |
| 68 | +| functions | | | | | | |
| 69 | +| installations | | | | | | |
| 70 | +| messaging | | | | | | |
| 71 | +| performance | | | | | | |
| 72 | +| remote-config | | | | | | |
| 73 | +| vertexai | | | | | | |
133 | 74 |
|
134 | 75 | ## License
|
135 | 76 |
|
|
0 commit comments