Skip to content

Commit 54f4c0a

Browse files
committed
[firestore] Consolidate some code
1 parent f41dbc8 commit 54f4c0a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

firestore/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ The `useCollectionData` hook takes the following parameters:
126126

127127
- `query`: (optional) `firestore.Query` for the data you would like to load
128128
- `options`: (optional) `Object` with the following parameters:
129+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
129130
- `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
130131
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
131-
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
132132

133133
Returns:
134134

@@ -154,8 +154,8 @@ The `useCollectionDataOnce` hook takes the following parameters:
154154
- `options`: (optional) `Object` with the following parameters:
155155
- `getOptions`: (optional) `Object` to customise how the collection is loaded
156156
- `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache.
157-
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
158157
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
158+
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
159159

160160
Returns:
161161

@@ -247,9 +247,9 @@ The `useDocumentData` hook takes the following parameters:
247247

248248
- `reference`: (optional) `firestore.DocumentReference` for the data you would like to load
249249
- `options`: (optional) `Object` with the following parameters:
250+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
250251
- `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
251252
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
252-
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
253253

254254
Returns:
255255

@@ -275,8 +275,8 @@ The `useDocumentDataOnce` hook takes the following parameters:
275275
- `options`: (optional) `Object` with the following parameters:
276276
- `getOptions`: (optional) `Object` to customise how the collection is loaded
277277
- `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache
278-
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
279278
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
279+
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
280280

281281
Returns:
282282

firestore/useDocument.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getDocFromCache,
88
getDocFromServer,
99
onSnapshot,
10+
SnapshotOptions,
1011
} from 'firebase/firestore';
1112
import { useEffect, useMemo } from 'react';
1213
import { useLoadingValue } from '../util';
@@ -120,13 +121,12 @@ export const useDocumentData = <T = DocumentData>(
120121
docRef?: DocumentReference<T> | null,
121122
options?: DataOptions<T> & InitialValueOptions<T>
122123
): DocumentDataHook<T> => {
123-
const snapshotOptions = options?.snapshotOptions;
124124
const [snapshot, loading, error] = useDocument<T>(docRef, options);
125125

126-
const initialValue = options?.initialValue;
127-
const value = useMemo(
128-
() => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined,
129-
[snapshot, snapshotOptions, initialValue]
126+
const value = getValueFromSnapshot(
127+
snapshot,
128+
options?.snapshotOptions,
129+
options?.initialValue
130130
);
131131

132132
const resArray: DocumentDataHook<T> = [value, loading, error, snapshot];
@@ -137,16 +137,15 @@ export const useDocumentDataOnce = <T = DocumentData>(
137137
docRef?: DocumentReference<T> | null,
138138
options?: OnceDataOptions<T> & InitialValueOptions<T>
139139
): DocumentDataOnceHook<T> => {
140-
const snapshotOptions = options?.snapshotOptions;
141140
const [snapshot, loading, error, loadData] = useDocumentOnce<T>(
142141
docRef,
143142
options
144143
);
145144

146-
const initialValue = options?.initialValue;
147-
const value = useMemo(
148-
() => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined,
149-
[snapshot, snapshotOptions, initialValue]
145+
const value = getValueFromSnapshot(
146+
snapshot,
147+
options?.snapshotOptions,
148+
options?.initialValue
150149
);
151150

152151
const resArray: DocumentDataOnceHook<T> = [
@@ -172,3 +171,14 @@ const getDocFnFromGetOptions = (
172171
return getDocFromServer;
173172
}
174173
};
174+
175+
const getValueFromSnapshot = <T>(
176+
snapshot: DocumentSnapshot<T> | undefined,
177+
options?: SnapshotOptions,
178+
initialValue?: T
179+
): T | undefined => {
180+
return useMemo(
181+
() => (snapshot?.data(options) ?? initialValue) as T | undefined,
182+
[snapshot, options, initialValue]
183+
);
184+
};

0 commit comments

Comments
 (0)