You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: database/README.md
+60-1Lines changed: 60 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,16 @@ import { useList } from 'react-firebase-hooks/database';
14
14
15
15
List of Realtime Database hooks:
16
16
17
-
-[useList](#uselistref)
17
+
-[useList](#uselist)
18
18
-[useListKeys](#uselistkeys)
19
19
-[useListVals](#uselistvals)
20
20
-[useObject](#useobject)
21
21
-[useObjectVal](#useobjectval)
22
22
23
+
Additional functionality:
24
+
25
+
-[Transforming data](#transforming-data)
26
+
23
27
### useList
24
28
25
29
```js
@@ -100,6 +104,7 @@ The `useListVals` hook takes the following parameters:
100
104
-`options`: (optional) `Object` with the following parameters:
101
105
-`keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.id` property in the returned values.
102
106
-`refField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.ref` property.
107
+
-`transform`: (optional) a function that receives the raw `firebase.database.DataSnapshot.val()` for each item in the list to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
103
108
104
109
Returns:
105
110
@@ -160,9 +165,63 @@ The `useObjectVal` hook takes the following parameters:
160
165
-`options`: (optional) `Object` with the following parameters:
161
166
-`keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
162
167
-`refField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.ref` property.
168
+
-`transform`: (optional) a function that receives the raw `firebase.database.DataSnapshot.val()` for each item in the list to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
163
169
164
170
Returns:
165
171
166
172
-`value`: a `T`, or `undefined` if no reference is supplied
167
173
-`loading`: a `boolean` to indicate if the data is still being loaded
168
174
-`error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `undefined` if there is no error
175
+
176
+
## Transforming data
177
+
178
+
Firebase allows a restricted number of data types in its store. The application, on the other hand, might require converting some of these types into whatever it really needs, `Date` types being a common case.
179
+
180
+
Both `useListVals` and `useObjectVal` support an optional `transform` function which allows the transformation of the underlying Firebase data into whatever format the application requires.
181
+
182
+
```js
183
+
transform?: (val:any) =>T;
184
+
```
185
+
186
+
The `transform` function is passed a single row of a data, so will be called once when used with `useObjectVal` and multiple times, when used with `useListVals`.
187
+
188
+
The `transform` function will not receive the `id` or `ref` values referenced in the properties named in the `keyField` or `refField` options, nor it is expected to produce them. Either or both, if specified, will be merged afterwards.
189
+
190
+
#### Full Example
191
+
192
+
```js
193
+
type SaleType = {
194
+
idSale: string,
195
+
date:Date, // <== it is declared as type Date which Firebase does not support.
0 commit comments