Skip to content

Commit ab62148

Browse files
committed
[firestore] Add transforming data example
1 parent 809c53c commit ab62148

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

firestore/README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Returns:
136136
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
137137
- `snapshot`: a `firestore.QuerySnapshot`, or `undefined` if no query is supplied. This allows access to the underlying snapshot if needed for any reason, e.g. to view the snapshot metadata
138138

139+
See [Transforming data](#transforming-data) for how to transform data as it leaves Firestore and access the underlying `id` and `ref` fields of the snapshot.
140+
139141
### useCollectionDataOnce
140142

141143
```js
@@ -160,6 +162,8 @@ Returns:
160162
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
161163
- `snapshot`: a `firestore.QuerySnapshot`, or `undefined` if no query is supplied. This allows access to the underlying snapshot if needed for any reason, e.g. to view the snapshot metadata
162164

165+
See [Transforming data](#transforming-data) for how to transform data as it leaves Firestore and access the underlying `id` and `ref` fields of the snapshot.
166+
163167
### useDocument
164168

165169
```js
@@ -251,6 +255,8 @@ Returns:
251255
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
252256
- `snapshot`: a `firestore.DocumentSnapshot`, or `undefined` if no query is supplied. This allows access to the underlying snapshot if needed for any reason, e.g. to view the snapshot metadata
253257

258+
See [Transforming data](#transforming-data) for how to transform data as it leaves Firestore and access the underlying `id` and `ref` fields of the snapshot.
259+
254260
### useDocumentDataOnce
255261

256262
```js
@@ -276,8 +282,42 @@ Returns:
276282
- `snapshot`: a `firestore.DocumentSnapshot`, or `undefined` if no query is supplied. This allows access to the underlying snapshot if needed for any reason, e.g. to view the snapshot metadata
277283
- `reload()`: a function that can be called to trigger a reload of the data
278284

285+
See [Transforming data](#transforming-data) for how to transform data as it leaves Firestore and access the underlying `id` and `ref` fields of the snapshot.
286+
279287
## Transforming data
280288

281-
Firestore allows a restricted number of data types in its store, which may not be flexible enough for your application. As of Firebase 9, there is a built in FirestoreDataConverter which allows you to transform data as it leaves the Firestore database. This is described here: https://firebase.google.com/docs/reference/js/firestore_.firestoredataconverter
289+
Firestore allows a restricted number of data types in its store, which may not be flexible enough for your application. As of Firebase 9, there is a built in FirestoreDataConverter which allows you to transform data as it leaves the Firestore database, as well as access the `id` and `ref` fields of the underlying snapshot. This is described here: https://firebase.google.com/docs/reference/js/firestore_.firestoredataconverter
290+
291+
> NOTE: This replaces the `transform`, `idField` and `refField` options that were available in `react-firebase-hooks` v4 and earlier.
292+
293+
### Example
282294

283-
> This has replaced the `transform`, `idField` and `refField` options that were available in `react-firebase-hooks` v4 and earlier.
295+
```js
296+
type Post = {
297+
author: string,
298+
id: string,
299+
ref: DocumentReference<DocumentData>,
300+
title: string,
301+
};
302+
303+
const postConverter: FirestoreDataConverter<Post> = {
304+
toFirestore(post: WithFieldValue<Post>): DocumentData {
305+
return { author: post.author, title: post.title };
306+
},
307+
fromFirestore(
308+
snapshot: QueryDocumentSnapshot,
309+
options: SnapshotOptions
310+
): Post {
311+
const data = snapshot.data(options);
312+
return {
313+
author: data.author,
314+
id: snapshot.id,
315+
ref: snapshot.ref,
316+
title: data.title,
317+
};
318+
},
319+
};
320+
321+
const ref = collection(firestore, 'posts').withConverter(postConverter);
322+
const [data, loading, error] = useCollectionData(ref);
323+
```

0 commit comments

Comments
 (0)