React Firebase Hooks provides convenience listeners for Collections and Documents stored with
Cloud Firestore. The hooks wrap around the firebase.firestore.collection().onSnapshot()
and firebase.firestore().doc().onSnapshot()
methods.
In addition to returning the snapshot value, the hooks provide an error
and loading
property
to give a complete lifecycle for loading and listening to Cloud Firestore.
There are 2 variants of each hook:
useX
which subscribes to the underlying Collection or Document and listens for changesuseXOnce
which reads the current value of the Collection or Document
All hooks can be imported from react-firebase-hooks/firestore
, e.g.
import { useCollection } from 'react-firebase-hooks/firestore';
List of Cloud Firestore hooks:
- useCollection
- useCollectionOnce
- useCollectionData
- useCollectionDataOnce
- useDocument
- useDocumentOnce
- useDocumentData
- useDocumentDataOnce
const [snapshot, loading, error] = useCollection(query, options);
Retrieve and monitor a collection value in Cloud Firestore.
Returns a firebase.firestore.QuerySnapshot
(if a query is specified), a boolean
to indicate if the data is still being loaded and any Error
returned by Firebase when trying to load the data.
The useCollection
hook takes the following parameters:
query
: (optional)firebase.firestore.Query
for the data you would like to loadoptions
: (optional)Object
with the following parameters:snapshotListenOptions
: (optional)firebase.firestore.SnapshotListenOptions
to customise how the query is loaded
Returns:
snapshot
: afirebase.firestore.QuerySnapshot
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
import { useCollection } from 'react-firebase-hooks/firestore';
const FirestoreCollection = () => {
const [value, loading, error] = useCollection(
firebase.firestore().collection('hooks'),
{
snapshotListenOptions: { includeMetadataChanges: true },
}
);
return (
<div>
<p>
{error && <strong>Error: {JSON.stringify(error)}</strong>}
{loading && <span>Collection: Loading...</span>}
{value && (
<span>
Collection:{' '}
{value.docs.map(doc => (
<React.Fragment key={doc.id}>
{JSON.stringify(doc.data())},{' '}
</React.Fragment>
))}
</span>
)}
</p>
</div>
);
};
const [snapshot, loading, error] = useCollectionOnce(query, options);
Retrieve the current value of the firebase.firestore.Query
.
The useCollectionOnce
hook takes the following parameters:
query
: (optional)firebase.firestore.Query
for the data you would like to loadoptions
: (optional)Object
with the following parameters:getOptions
: (optional)firebase.firestore.GetOptions
to customise how the collection is loaded
Returns:
snapshot
: afirebase.firestore.QuerySnapshot
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [values, loading, error] = useCollectionData<T>(query, options);
As useCollection
, but this hook extracts a typed list of the firebase.firestore.QuerySnapshot.docs
values, rather than the
firebase.firestore.QuerySnapshot
itself.
The useCollectionData
hook takes the following parameters:
query
: (optional)firebase.firestore.Query
for the data you would like to loadoptions
: (optional)Object
with the following parameters:idField
: (optional) name of the field that should be populated with thefirebase.firestore.QuerySnapshot.id
property.snapshotListenOptions
: (optional)firebase.firestore.SnapshotListenOptions
to customise how the collection is loaded
Returns:
values
: an array ofT
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [values, loading, error] = useCollectionDataOnce<T>(query, options);
As useCollectionData
, but this hook will only read the current value of the firebase.firestore.Query
.
The useCollectionDataOnce
hook takes the following parameters:
query
: (optional)firebase.firestore.Query
for the data you would like to loadoptions
: (optional)Object
with the following parameters:getOptions
: (optional)firebase.firestore.GetOptions
to customise how the collection is loadedidField
: (optional) name of the field that should be populated with thefirebase.firestore.QuerySnapshot.id
property.
Returns:
values
: an array ofT
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [snapshot, loading, error] = useDocument(reference, options);
Retrieve and monitor a document value in Cloud Firestore.
The useDocument
hook takes the following parameters:
reference
: (optional)firebase.firestore.DocumentReference
for the data you would like to loadoptions
: (optional)Object
with the following parameters:snapshotListenOptions
: (optional)firebase.firestore.SnapshotListenOptions
to customise how the query is loaded
Returns:
snapshot
: afirebase.firestore.DocumentSnapshot
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
import { useDocument } from 'react-firebase-hooks/firestore';
const FirestoreDocument = () => {
const [value, loading, error] = useDocument(
firebase.firestore().doc('hooks/nBShXiRGFAhuiPfBaGpt'),
{
snapshotListenOptions: { includeMetadataChanges: true },
}
);
return (
<div>
<p>
{error && <strong>Error: {JSON.stringify(error)}</strong>}
{loading && <span>Document: Loading...</span>}
{value && <span>Document: {JSON.stringify(value.data())}</span>}
</p>
</div>
);
};
const [snapshot, loading, error] = useDocumentOnce(reference, options);
Retrieve the current value of the firebase.firestore.DocumentReference
.
The useDocumentOnce
hook takes the following parameters:
reference
: (optional)firebase.firestore.DocumentReference
for the data you would like to loadoptions
: (optional)Object
with the following parameters:getOptions
: (optional)firebase.firestore.GetOptions
to customise how the collection is loaded
Returns:
snapshot
: afirebase.firestore.DocumentSnapshot
, orundefined
if no reference is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [value, loading, error] = useDocumentData<T>(reference, options);
As useDocument
, but this hook extracts the typed contents of firebase.firestore.DocumentSnapshot.val()
, rather than the
firebase.firestore.DocumentSnapshot
itself.
The useDocumentData
hook takes the following parameters:
reference
: (optional)firebase.firestore.DocumentReference
for the data you would like to loadoptions
: (optional)Object
with the following parameters:idField
: (optional) name of the field that should be populated with thefirebase.firestore.DocumentSnapshot.id
property.snapshotListenOptions
: (optional)firebase.firestore.SnapshotListenOptions
to customise how the collection is loaded
Returns:
value
:T
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [value, loading, error] = useDocumentDataOnce<T>(reference, options);
As useDocument
, but this hook will only read the current value of the firebase.firestore.DocumentReference
.
The useDocumentDataOnce
hook takes the following parameters:
reference
: (optional)firebase.firestore.DocumentReference
for the data you would like to loadoptions
: (optional)Object
with the following parameters:getOptions
: (optional)firebase.firestore.GetOptions
to customise how the collection is loadedidField
: (optional) name of the field that should be populated with thefirebase.firestore.DocumentSnapshot.id
property.
Returns:
value
:T
, orundefined
if no query is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: AnyError
returned by Firebase when trying to load the data, orundefined
if there is no error