React Firebase Hooks provides convenience listeners for lists and values stored within the
Firebase Realtime Database. The hooks wrap around the firebase.database().ref().on()
method.
In addition to returning the list or value, the hooks provide an error
and loading
property
to give a complete lifecycle for loading and listening to the Realtime Database.
All hooks can be imported from react-firebase-hooks/database
, e.g.
import { useList } from 'react-firebase-hooks/database';
List of Realtime Database hooks:
const [snapshots, loading, error] = useList(reference);
Retrieve and monitor a list value in the Firebase Realtime Database.
The useList
hook takes the following parameters:
reference
: (optional)firebase.database.Reference
for the data you would like to load
Returns:
snapshots
: an array offirebase.database.DataSnapshot
, orundefined
if no reference is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: Anyfirebase.FirebaseError
returned by Firebase when trying to load the data, orundefined
if there is no error
import { useList } from 'react-firebase-hooks/database';
const DatabaseList = () => {
const [snapshots, loading, error] = useList(firebase.database().ref('list'));
return (
<div>
<p>
{error && <strong>Error: {error}</strong>}
{loading && <span>List: Loading...</span>}
{!loading && snapshots && (
<React.Fragment>
<span>
List:{' '}
{snapshots.map((v) => (
<React.Fragment key={v.key}>{v.val()}, </React.Fragment>
))}
</span>
</React.Fragment>
)}
</p>
</div>
);
};
const [keys, loading, error] = useListKeys(reference);
As useList
, but this hooks extracts the firebase.database.DataSnapshot.key
values, rather than the the firebase.database.DataSnapshot
s themselves.
The useListKeys
hook takes the following parameters:
reference
: (optional)firebase.database.Reference
for the data you would like to load
Returns:
keys
: an array ofstring
, orundefined
if no reference is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: Anyfirebase.FirebaseError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [values, loading, error] = useListVals<T>(reference, options);
As useList
, but this hook extracts a typed list of the firebase.database.DataSnapshot.val()
values, rather than the the
firebase.database.DataSnapshot
s themselves.
The useListVals
hook takes the following parameters:
reference
: (optional)firebase.database.Reference
for the data you would like to loadoptions
: (optional)Object
with the following parameters:keyField
: (optional)string
field name that should be populated with thefirebase.firestore.QuerySnapshot.id
property in the returned values
Returns:
values
: an array ofT
, orundefined
if no reference is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: Anyfirebase.FirebaseError
returned by Firebase when trying to load the data, orundefined
if there is no error
const [snapshot, loading, error] = useObject(reference);
Retrieve and monitor an object or primitive value in the Firebase Realtime Database.
The useObject
hook takes the following parameters:
reference
: (optional)firebase.database.Reference
for the data you would like to load
Returns:
snapshot
: afirebase.database.DataSnapshot
, orundefined
if no reference is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: Anyfirebase.FirebaseError
returned by Firebase when trying to load the data, orundefined
if there is no error
import { useObject } from 'react-firebase-hooks/database';
const DatabaseValue = () => {
const [value, loading, error] = useObject(firebase.database().ref('value'));
return (
<div>
<p>
{error && <strong>Error: {error}</strong>}
{loading && <span>Value: Loading...</span>}
{value && <span>Value: {value.val()}</span>}
</p>
</div>
);
};
const [value, loading, error] = useObjectVal<T>(reference, options);
As useObject
, but this hook returns the typed contents of firebase.database.DataSnapshot.val()
, rather than the the
firebase.database.DataSnapshot
itself.
The useObjectVal
hook takes the following parameters:
reference
: (optional)firebase.database.Reference
for the data you would like to loadoptions
: (optional)Object
with the following parameters:keyField
: (optional)string
field name that should be populated with thefirebase.database.DataSnapshot.key
property in the returned value.
Returns:
value
: aT
, orundefined
if no reference is suppliedloading
: aboolean
to indicate if the data is still being loadederror
: Anyfirebase.FirebaseError
returned by Firebase when trying to load the data, orundefined
if there is no error