React Query + Firestore
React Query for Firestore, that you can actually use in production, on every screen
It's that easy.
? This library provides the hooks you need for querying Firestore, that you can actually use in production, on every screen.
⚡️ It aims to be the fastest way to use Firestore in a React app, both from a developer experience and app performance perspective.
? This library is built on top react-query, meaning you get all of its awesome benefits out-of-the-box.
You can now fetch, add, and mutate Firestore data with zero boilerplate.
Features
- Shared state / cache between collection and document queries (instead of Redux??)
- Works with both React and React Native.
- Blazing fast
- Query collection groups
set
,update
, andadd
update your global cache, instantly- TypeScript-ready (see docs)
- Realtime subscriptions (example)
- Prevent memory leaks from Firestore subscriptions
- No more parsing
document.data()
from Firestore requests
...along with the features touted by react-query library:
- Transport and protocol agnostic data fetching
- Fast page navigation
- Revalidation on focus
- Interval polling
- Request deduplication
- Local mutation
- Pagination
- TypeScript ready
- SSR support
- Suspense mode
- Minimal API
⭐️
If you like this library, give it star
Installation
Install firebase:
Set up
In the root of your app, create an instance of firestore and (react query config object) and pass it to the ReactQueryFirestoreProvider.
If you're using next.js
, this goes in your pages/_app.js
file.
App.js
Basic Usage
Assuming you've already completed the setup...
Subscribe to a document
Get a collection
useDocument
accepts a document path
as its first argument here. useCollection
works similarly.
Simple examples
Query a users collection:
Make a complex collection query:
Pass options from react-query to your document query:
Pass options from react-query to your collection query:
Add data to your collection:
Set document data:
Use dynamic fields in a request:
If you pass undefined
as the document key, the request won't send.
Once the key is set to a string, the request will send.
Get list of users who have you in their friends list
Get your favorite song
Query Documents
You'll rely on useDocument
to query documents.
Features
TypeScript Support
Create a model for your typescript
types, and pass it as a generic to useDocument
or useCollection
.
useDocument
The data
item will include your TypeScript model (or undefined
), and will also include an id
string, an exists
boolean, and hasPendingWrites
boolean.
useCollection
The data
item will include your TypeScript model (or undefined
), and will also include an id
string.
Shared global state between documents and collections
A great feature of this library is shared data between documents and collections. Until now, this could only be achieved with something like a verbose Redux set up.
So, what does this mean exactly?
Simply put, any documents pulled from a Firestore request will update the global cache.
To make it clear, let's look at an example.
Imagine you query a user
document from Firestore:
And pretend that this document's data
returns the following:
Remember that isHungry
is false
here ^
Now, let's say you query the users
collection anywhere else in your app:
And pretend that this collection's data
returns the following:
Whoa, isHungry
is now true. But what happens to the original document query? Will we have stale data?
Answer: It will automatically re-render with the new data!
swr-firestore
uses document id
fields to sync any collection queries with existing document queries across your app.
That means that if you somehow fetch the same document twice, the latest version will update everywhere.
License
MIT