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: firestore/README.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,10 @@ List of Cloud Firestore hooks:
29
29
-[useDocumentData](#usedocumentdata)
30
30
-[useDocumentDataOnce](#usedocumentdataonce)
31
31
32
+
Additional functionality:
33
+
34
+
-[Transforming data](#transforming-data)
35
+
32
36
### useCollection
33
37
34
38
```js
@@ -121,6 +125,7 @@ The `useCollectionData` hook takes the following parameters:
121
125
-`refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
122
126
-`snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
123
127
-`snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
128
+
-`transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` for each item in the collection to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
124
129
125
130
Returns:
126
131
@@ -144,6 +149,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
144
149
-`idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
145
150
-`refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
146
151
-`snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
152
+
-`transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` for each item in the collection to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
147
153
148
154
Returns:
149
155
@@ -232,6 +238,7 @@ The `useDocumentData` hook takes the following parameters:
232
238
-`refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
233
239
-`snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
234
240
-`snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
241
+
-`transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
235
242
236
243
Returns:
237
244
@@ -255,9 +262,24 @@ The `useDocumentDataOnce` hook takes the following parameters:
255
262
-`idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
256
263
-`refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
257
264
-`snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
265
+
-`transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
258
266
259
267
Returns:
260
268
261
269
-`value`: `T`, or `undefined` if no query is supplied
262
270
-`loading`: a `boolean` to indicate if the data is still being loaded
263
271
-`error`: Any `Error` returned by Firebase when trying to load the data, or `undefined` if there is no error
272
+
273
+
## Transforming data
274
+
275
+
Firestore 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.
276
+
277
+
Both `useCollectionData` and `useDocumentData` support an optional `transform` function which allows the transformation of the underlying Firestore data into whatever format the application requires.
278
+
279
+
```js
280
+
transform?: (val:any) =>T;
281
+
```
282
+
283
+
The `transform` function is passed a single row of a data, so will be called once when used with `useDocumentData` and multiple times when used with `useCollectionData`.
284
+
285
+
The `transform` function will not receive the `id` or `ref` values referenced in the properties named in the `idField` or `refField` options, nor it is expected to produce them. Either or both, if specified, will be merged afterwards.
0 commit comments