Skip to content

Commit 1e893b1

Browse files
committed
Add support for Firestore snapshotOptions. Closes #78
1 parent 75821e4 commit 1e893b1

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

firestore/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const FirestoreCollection = () => {
7171
{value && (
7272
<span>
7373
Collection:{' '}
74-
{value.docs.map(doc => (
74+
{value.docs.map((doc) => (
7575
<React.Fragment key={doc.id}>
7676
{JSON.stringify(doc.data())},{' '}
7777
</React.Fragment>
@@ -107,7 +107,7 @@ Returns:
107107
### useCollectionData
108108

109109
```js
110-
const [values, loading, error] = useCollectionData<T>(query, options);
110+
const [values, loading, error] = useCollectionData < T > (query, options);
111111
```
112112

113113
As `useCollection`, but this hook extracts a typed list of the `firebase.firestore.QuerySnapshot.docs` values, rather than the
@@ -120,6 +120,7 @@ The `useCollectionData` hook takes the following parameters:
120120
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
121121
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
122122
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
123+
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
123124

124125
Returns:
125126

@@ -130,7 +131,7 @@ Returns:
130131
### useCollectionDataOnce
131132

132133
```js
133-
const [values, loading, error] = useCollectionDataOnce<T>(query, options);
134+
const [values, loading, error] = useCollectionDataOnce < T > (query, options);
134135
```
135136

136137
As `useCollectionData`, but this hook will only read the current value of the `firebase.firestore.Query`.
@@ -142,6 +143,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
142143
- `getOptions`: (optional) `firebase.firestore.GetOptions` to customise how the collection is loaded
143144
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
144145
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
146+
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
145147

146148
Returns:
147149

@@ -216,7 +218,7 @@ Returns:
216218
### useDocumentData
217219

218220
```js
219-
const [value, loading, error] = useDocumentData<T>(reference, options);
221+
const [value, loading, error] = useDocumentData < T > (reference, options);
220222
```
221223

222224
As `useDocument`, but this hook extracts the typed contents of `firebase.firestore.DocumentSnapshot.val()`, rather than the
@@ -229,6 +231,7 @@ The `useDocumentData` hook takes the following parameters:
229231
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
230232
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
231233
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
234+
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
232235

233236
Returns:
234237

@@ -239,7 +242,7 @@ Returns:
239242
### useDocumentDataOnce
240243

241244
```js
242-
const [value, loading, error] = useDocumentDataOnce<T>(reference, options);
245+
const [value, loading, error] = useDocumentDataOnce < T > (reference, options);
243246
```
244247

245248
As `useDocument`, but this hook will only read the current value of the `firebase.firestore.DocumentReference`.
@@ -251,6 +254,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
251254
- `getOptions`: (optional) `firebase.firestore.GetOptions` to customise how the collection is loaded
252255
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
253256
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
257+
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
254258

255259
Returns:
256260

firestore/helpers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import firebase from 'firebase/app';
22

33
export const snapshotToData = (
44
snapshot: firebase.firestore.DocumentSnapshot,
5+
snapshotOptions?: firebase.firestore.SnapshotOptions,
56
idField?: string,
67
refField?: string
78
) => {
@@ -10,7 +11,7 @@ export const snapshotToData = (
1011
}
1112

1213
return {
13-
...snapshot.data(),
14+
...snapshot.data(snapshotOptions),
1415
...(idField ? { [idField]: snapshot.id } : null),
1516
...(refField ? { [refField]: snapshot.ref } : null),
1617
};

firestore/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LoadingHook } from '../util';
44
type IDOptions = {
55
idField?: string;
66
refField?: string;
7+
snapshotOptions?: firebase.firestore.SnapshotOptions;
78
};
89
export type Options = {
910
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;

firestore/useCollection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const useCollectionDataInternal = <
104104
): CollectionDataHook<T, IDField, RefField> => {
105105
const idField = options ? options.idField : undefined;
106106
const refField = options ? options.refField : undefined;
107+
const snapshotOptions = options ? options.snapshotOptions : undefined;
107108
const [snapshots, loading, error] = useCollectionInternal<T>(
108109
listen,
109110
query,
@@ -112,7 +113,9 @@ const useCollectionDataInternal = <
112113
const values = useMemo(
113114
() =>
114115
(snapshots
115-
? snapshots.docs.map((doc) => snapshotToData(doc, idField, refField))
116+
? snapshots.docs.map((doc) =>
117+
snapshotToData(doc, snapshotOptions, idField, refField)
118+
)
116119
: undefined) as Data<T, IDField, RefField>[],
117120
[snapshots, idField, refField]
118121
);

firestore/useDocument.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const useDocumentDataInternal = <
104104
): DocumentDataHook<T, IDField, RefField> => {
105105
const idField = options ? options.idField : undefined;
106106
const refField = options ? options.refField : undefined;
107+
const snapshotOptions = options ? options.snapshotOptions : undefined;
107108
const [snapshot, loading, error] = useDocumentInternal<T>(
108109
listen,
109110
docRef,
@@ -112,7 +113,7 @@ const useDocumentDataInternal = <
112113
const value = useMemo(
113114
() =>
114115
(snapshot
115-
? snapshotToData(snapshot, idField, refField)
116+
? snapshotToData(snapshot, snapshotOptions, idField, refField)
116117
: undefined) as Data<T, IDField, RefField>,
117118
[snapshot, idField, refField]
118119
);

0 commit comments

Comments
 (0)