Skip to content

Commit 416de01

Browse files
committed
[firestore] Add snapshot to *Data responses to give more control to the developer
1 parent 76985ce commit 416de01

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

firestore/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ Returns:
115115
### useCollectionData
116116

117117
```js
118-
const [values, loading, error] = useCollectionData < T > (query, options);
118+
const [values, loading, error, snapshot] =
119+
useCollectionData < T > (query, options);
119120
```
120121

121122
As `useCollection`, but this hook extracts a typed list of the `firestore.QuerySnapshot.docs` values, rather than the
@@ -133,11 +134,13 @@ Returns:
133134
- `values`: an array of `T`, or `undefined` if no query is supplied
134135
- `loading`: a `boolean` to indicate if the data is still being loaded
135136
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
137+
- `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
136138

137139
### useCollectionDataOnce
138140

139141
```js
140-
const [values, loading, error] = useCollectionDataOnce < T > (query, options);
142+
const [values, loading, error, snapshot] =
143+
useCollectionDataOnce < T > (query, options);
141144
```
142145

143146
As `useCollectionData`, but this hook will only read the current value of the `firestore.Query`.
@@ -155,6 +158,7 @@ Returns:
155158
- `values`: an array of `T`, or `undefined` if no query is supplied
156159
- `loading`: a `boolean` to indicate if the data is still being loaded
157160
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
161+
- `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
158162

159163
### useDocument
160164

@@ -225,7 +229,8 @@ Returns:
225229
### useDocumentData
226230

227231
```js
228-
const [value, loading, error] = useDocumentData < T > (reference, options);
232+
const [value, loading, error, snapshot] =
233+
useDocumentData < T > (reference, options);
229234
```
230235

231236
As `useDocument`, but this hook extracts the typed contents of `firestore.DocumentSnapshot.data()`, rather than the
@@ -243,11 +248,13 @@ Returns:
243248
- `value`: `T`, or `undefined` if no query is supplied
244249
- `loading`: a `boolean` to indicate if the data is still being loaded
245250
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
251+
- `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
246252

247253
### useDocumentDataOnce
248254

249255
```js
250-
const [value, loading, error] = useDocumentDataOnce < T > (reference, options);
256+
const [value, loading, error, snapshot] =
257+
useDocumentDataOnce < T > (reference, options);
251258
```
252259

253260
As `useDocument`, but this hook will only read the current value of the `firestore.DocumentReference`.
@@ -265,6 +272,7 @@ Returns:
265272
- `value`: `T`, or `undefined` if no query is supplied
266273
- `loading`: a `boolean` to indicate if the data is still being loaded
267274
- `error`: Any `firestore.FirestoreError` returned by Firebase when trying to load the data, or `undefined` if there is no error
275+
- `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
268276

269277
## Transforming data
270278

firestore/types.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ export type CollectionHook<T = DocumentData> = LoadingHook<
2727
QuerySnapshot<T>,
2828
FirestoreError
2929
>;
30-
export type CollectionDataHook<T = DocumentData> = LoadingHook<
31-
T[],
32-
FirestoreError
33-
>;
30+
export type CollectionDataHook<T = DocumentData> = [
31+
T[] | undefined,
32+
boolean,
33+
FirestoreError | undefined,
34+
QuerySnapshot<T> | undefined
35+
];
3436

3537
export type DocumentHook<T = DocumentData> = LoadingHook<
3638
DocumentSnapshot<T>,
3739
FirestoreError
3840
>;
39-
export type DocumentDataHook<T = DocumentData> = LoadingHook<T, FirestoreError>;
41+
export type DocumentDataHook<T = DocumentData> = [
42+
T | undefined,
43+
boolean,
44+
FirestoreError | undefined,
45+
DocumentSnapshot<T> | undefined
46+
];

firestore/useCollection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const useCollectionDataInternal = <T = DocumentData>(
125125
[snapshots, snapshotOptions]
126126
);
127127

128-
const resArray: CollectionDataHook<T> = [values, loading, error];
128+
const resArray: CollectionDataHook<T> = [values, loading, error, snapshots];
129129
return useMemo(() => resArray, resArray);
130130
};
131131

firestore/useDocument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const useDocumentDataInternal = <T = DocumentData>(
124124
snapshotOptions,
125125
]);
126126

127-
const resArray: DocumentDataHook<T> = [value, loading, error];
127+
const resArray: DocumentDataHook<T> = [value, loading, error, snapshot];
128128
return useMemo(() => resArray, resArray);
129129
};
130130

0 commit comments

Comments
 (0)