|
| 1 | +# React Firebase Hooks - Realtime Database |
| 2 | + |
| 3 | +React Firebase Hooks provides convenience listeners for lists and values stored within the |
| 4 | +Firebase Realtime Database. The hooks wrap around the `firebase.database().ref().on()` method. |
| 5 | + |
| 6 | +In addition to returning the list or value, the hooks provide an `error` and `loading` property |
| 7 | +to give a complete lifecycle for loading and listening to the Realtime Database. |
| 8 | + |
| 9 | +All hooks can be imported from `react-firebase-hooks/database`, e.g. |
| 10 | + |
| 11 | +``` |
| 12 | +import { useList } from 'react-firebase-hooks/database'; |
| 13 | +``` |
| 14 | + |
| 15 | +List of Realtime Database hooks: |
| 16 | + |
| 17 | +- [useList](#uselistref) |
| 18 | +- [useListKeys](#uselistkeys) |
| 19 | +- [useListVals](#uselistvals) |
| 20 | +- [useObject](#useobjectref) |
| 21 | +- [useObjectVal](#useobjectval) |
| 22 | + |
| 23 | +### useList |
| 24 | + |
| 25 | +``` |
| 26 | +const [snapshots, loading, error] = useList(reference); |
| 27 | +``` |
| 28 | + |
| 29 | +Retrieve and monitor a list value in the Firebase Realtime Database. |
| 30 | + |
| 31 | +The `useList` hook takes the following parameters: |
| 32 | + |
| 33 | +- `reference`: (optional) `firebase.database.Reference` for the data you would like to load |
| 34 | + |
| 35 | +Returns: |
| 36 | + |
| 37 | +- `snapshots`: an array of `firebase.database.DataSnapshot`, or `void` if no reference is supplied |
| 38 | +- `loading`: a `boolean` to indicate if the data is still being loaded |
| 39 | +- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error |
| 40 | + |
| 41 | +#### Full Example |
| 42 | + |
| 43 | +```js |
| 44 | +import { useList } from 'react-firebase-hooks/database'; |
| 45 | + |
| 46 | +const DatabaseList = () => { |
| 47 | + const [snapshots, loading, error] = useList(firebase.database().ref('list')); |
| 48 | + |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + <p> |
| 52 | + {error && <strong>Error: {error}</strong>} |
| 53 | + {loading && <span>List: Loading...</span>} |
| 54 | + {!loading && value && ( |
| 55 | + <React.Fragment> |
| 56 | + <span> |
| 57 | + List:{' '} |
| 58 | + {value.map(v => ( |
| 59 | + <React.Fragment key={v.key}>{v.val()}, </React.Fragment> |
| 60 | + ))} |
| 61 | + </span> |
| 62 | + </React.Fragment> |
| 63 | + )} |
| 64 | + </p> |
| 65 | + </div> |
| 66 | + ); |
| 67 | +}; |
| 68 | +``` |
| 69 | + |
| 70 | +### useListKeys |
| 71 | + |
| 72 | +``` |
| 73 | +const [keys, loading, error] = useListKeys(reference); |
| 74 | +``` |
| 75 | + |
| 76 | +As `useList`, but this hooks extracts the `firebase.database.DataSnapshot.key` values, rather than the the `firebase.database.DataSnapshot`s themselves. |
| 77 | + |
| 78 | +The `useListKeys` hook takes the following parameters: |
| 79 | + |
| 80 | +- `reference`: (optional) `firebase.database.Reference` for the data you would like to load |
| 81 | + |
| 82 | +Returns: |
| 83 | + |
| 84 | +- `keys`: an array of `string`, or `void` if no reference is supplied |
| 85 | +- `loading`: a `boolean` to indicate if the data is still being loaded |
| 86 | +- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error |
| 87 | + |
| 88 | +### useListVals |
| 89 | + |
| 90 | +``` |
| 91 | +const [values, loading, error] = useListVals<T>(reference, options); |
| 92 | +``` |
| 93 | + |
| 94 | +As `useList`, but this hook extracts a typed list of the `firebase.database.DataSnapshot.val()` values, rather than the the |
| 95 | +`firebase.database.DataSnapshot`s themselves. |
| 96 | + |
| 97 | +The `useListVals` hook takes the following parameters: |
| 98 | + |
| 99 | +- `reference`: (optional) `firebase.database.Reference` for the data you would like to load |
| 100 | +- `options`: (optional) `Object` with the following parameters: |
| 101 | + - `keyField`: (optional) `string` field name that should be populated with the `firebase.firestore.QuerySnapshot.id` property in the returned values |
| 102 | + |
| 103 | +Returns: |
| 104 | + |
| 105 | +- `values`: an array of `T`, or `void` if no reference is supplied |
| 106 | +- `loading`: a `boolean` to indicate if the data is still being loaded |
| 107 | +- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error |
| 108 | + |
| 109 | +### useObject |
| 110 | + |
| 111 | +``` |
| 112 | +const [snapshot, loading, error] = useObject(reference); |
| 113 | +``` |
| 114 | + |
| 115 | +Retrieve and monitor an object or primitive value in the Firebase Realtime Database. |
| 116 | + |
| 117 | +The `useObject` hook takes the following parameters: |
| 118 | + |
| 119 | +- `reference`: (optional) `firebase.database.Reference` for the data you would like to load |
| 120 | + |
| 121 | +Returns: |
| 122 | + |
| 123 | +- `snapshot`: a `firebase.database.DataSnapshot`, or `void` if no reference is supplied |
| 124 | +- `loading`: a `boolean` to indicate if the data is still being loaded |
| 125 | +- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error |
| 126 | + |
| 127 | +#### Full Example |
| 128 | + |
| 129 | +```js |
| 130 | +import { useObject } from 'react-firebase-hooks/database'; |
| 131 | + |
| 132 | +const DatabaseValue = () => { |
| 133 | + const [value, loading, error] = useObject(firebase.database().ref('value')); |
| 134 | + |
| 135 | + return ( |
| 136 | + <div> |
| 137 | + <p> |
| 138 | + {error && <strong>Error: {error}</strong>} |
| 139 | + {loading && <span>Value: Loading...</span>} |
| 140 | + {value && <span>Value: {value.val()}</span>} |
| 141 | + </p> |
| 142 | + </div> |
| 143 | + ); |
| 144 | +}; |
| 145 | +``` |
| 146 | + |
| 147 | +### useObjectVal |
| 148 | + |
| 149 | +``` |
| 150 | +const [value, loading, error] = useObjectVal<T>(reference, options); |
| 151 | +``` |
| 152 | + |
| 153 | +As `useObject`, but this hook returns the typed contents of `firebase.database.DataSnapshot.val()`, rather than the the |
| 154 | +`firebase.database.DataSnapshot` itself. |
| 155 | + |
| 156 | +The `useObjectVal` hook takes the following parameters: |
| 157 | + |
| 158 | +- `reference`: (optional) `firebase.database.Reference` for the data you would like to load |
| 159 | +- `options`: (optional) `Object` with the following parameters: |
| 160 | + - `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value. |
| 161 | + |
| 162 | +Returns: |
| 163 | + |
| 164 | +- `value`: a `T`, or `void` if no reference is supplied |
| 165 | +- `loading`: a `boolean` to indicate if the data is still being loaded |
| 166 | +- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error |
0 commit comments