Skip to content

Commit f41dbc8

Browse files
authored
Merge pull request #270 from mauriceackel/feature/initial-value
Feature/initial value
2 parents 2d7afbf + fc87a3c commit f41dbc8

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

firestore/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ The `useCollectionData` hook takes the following parameters:
128128
- `options`: (optional) `Object` with the following parameters:
129129
- `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
130130
- `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
131132

132133
Returns:
133134

@@ -154,6 +155,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
154155
- `getOptions`: (optional) `Object` to customise how the collection is loaded
155156
- `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache.
156157
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
158+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
157159

158160
Returns:
159161

@@ -247,6 +249,7 @@ The `useDocumentData` hook takes the following parameters:
247249
- `options`: (optional) `Object` with the following parameters:
248250
- `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
249251
- `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
250253

251254
Returns:
252255

@@ -273,6 +276,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
273276
- `getOptions`: (optional) `Object` to customise how the collection is loaded
274277
- `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache
275278
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
279+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
276280

277281
Returns:
278282

firestore/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export type IDOptions<T> = {
1414
export type Options = {
1515
snapshotListenOptions?: SnapshotListenOptions;
1616
};
17+
export type InitialValueOptions<T> = {
18+
initialValue?: T;
19+
};
1720
export type DataOptions<T> = Options & IDOptions<T>;
1821
export type OnceOptions = {
1922
getOptions?: GetOptions;

firestore/useCollection.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
CollectionOnceHook,
2020
DataOptions,
2121
GetOptions,
22+
InitialValueOptions,
2223
OnceDataOptions,
2324
OnceOptions,
2425
Options,
@@ -113,25 +114,39 @@ export const useCollectionOnce = <T = DocumentData>(
113114

114115
export const useCollectionData = <T = DocumentData>(
115116
query?: Query<T> | null,
116-
options?: DataOptions<T>
117+
options?: DataOptions<T> & InitialValueOptions<T[]>
117118
): CollectionDataHook<T> => {
118119
const snapshotOptions = options?.snapshotOptions;
119120
const [snapshots, loading, error] = useCollection<T>(query, options);
120-
const values = getValuesFromSnapshots<T>(snapshots, snapshotOptions);
121+
122+
const initialValue = options?.initialValue;
123+
const values = getValuesFromSnapshots<T>(
124+
snapshots,
125+
snapshotOptions,
126+
initialValue
127+
);
128+
121129
const resArray: CollectionDataHook<T> = [values, loading, error, snapshots];
122130
return useMemo(() => resArray, resArray);
123131
};
124132

125133
export const useCollectionDataOnce = <T = DocumentData>(
126134
query?: Query<T> | null,
127-
options?: OnceDataOptions<T>
135+
options?: OnceDataOptions<T> & InitialValueOptions<T[]>
128136
): CollectionDataOnceHook<T> => {
129137
const snapshotOptions = options?.snapshotOptions;
130138
const [snapshots, loading, error, loadData] = useCollectionOnce<T>(
131139
query,
132140
options
133141
);
134-
const values = getValuesFromSnapshots<T>(snapshots, snapshotOptions);
142+
143+
const initialValue = options?.initialValue;
144+
const values = getValuesFromSnapshots<T>(
145+
snapshots,
146+
snapshotOptions,
147+
initialValue
148+
);
149+
135150
const resArray: CollectionDataOnceHook<T> = [
136151
values,
137152
loading,
@@ -143,13 +158,17 @@ export const useCollectionDataOnce = <T = DocumentData>(
143158
};
144159

145160
const getValuesFromSnapshots = <T>(
146-
snapshots?: QuerySnapshot<T>,
147-
options?: SnapshotOptions
148-
) => {
149-
return useMemo(() => snapshots?.docs.map((doc) => doc.data(options)) as T[], [
150-
snapshots,
151-
options,
152-
]);
161+
snapshots: QuerySnapshot<T> | undefined,
162+
options?: SnapshotOptions,
163+
initialValue?: T[]
164+
): T[] | undefined => {
165+
return useMemo(
166+
() =>
167+
(snapshots?.docs.map((doc) => doc.data(options)) ?? initialValue) as
168+
| T[]
169+
| undefined,
170+
[snapshots, options]
171+
);
153172
};
154173

155174
const getDocsFnFromGetOptions = (

firestore/useDocument.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
DocumentHook,
1919
DocumentOnceHook,
2020
GetOptions,
21+
InitialValueOptions,
2122
OnceDataOptions,
2223
OnceOptions,
2324
Options,
@@ -117,32 +118,36 @@ export const useDocumentOnce = <T = DocumentData>(
117118

118119
export const useDocumentData = <T = DocumentData>(
119120
docRef?: DocumentReference<T> | null,
120-
options?: DataOptions<T>
121+
options?: DataOptions<T> & InitialValueOptions<T>
121122
): DocumentDataHook<T> => {
122123
const snapshotOptions = options?.snapshotOptions;
123124
const [snapshot, loading, error] = useDocument<T>(docRef, options);
124-
const value = useMemo(() => snapshot?.data(snapshotOptions) as T, [
125-
snapshot,
126-
snapshotOptions,
127-
]);
125+
126+
const initialValue = options?.initialValue;
127+
const value = useMemo(
128+
() => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined,
129+
[snapshot, snapshotOptions, initialValue]
130+
);
128131

129132
const resArray: DocumentDataHook<T> = [value, loading, error, snapshot];
130133
return useMemo(() => resArray, resArray);
131134
};
132135

133136
export const useDocumentDataOnce = <T = DocumentData>(
134137
docRef?: DocumentReference<T> | null,
135-
options?: OnceDataOptions<T>
138+
options?: OnceDataOptions<T> & InitialValueOptions<T>
136139
): DocumentDataOnceHook<T> => {
137140
const snapshotOptions = options?.snapshotOptions;
138141
const [snapshot, loading, error, loadData] = useDocumentOnce<T>(
139142
docRef,
140143
options
141144
);
142-
const value = useMemo(() => snapshot?.data(snapshotOptions) as T, [
143-
snapshot,
144-
snapshotOptions,
145-
]);
145+
146+
const initialValue = options?.initialValue;
147+
const value = useMemo(
148+
() => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined,
149+
[snapshot, snapshotOptions, initialValue]
150+
);
146151

147152
const resArray: DocumentDataOnceHook<T> = [
148153
value,

0 commit comments

Comments
 (0)