Skip to content

Commit 7ac6439

Browse files
feat: Allow an initialValue to be passed as an option on data methods
1 parent 16ca17e commit 7ac6439

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

firestore/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export type Options = {
1111
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
1212
};
1313
export type DataOptions<T> = Options & IDOptions<T>;
14+
export type InitialValueOptions<T> = {
15+
initialValue?: T;
16+
};
1417
export type OnceOptions = {
1518
getOptions?: firebase.firestore.GetOptions;
1619
};

firestore/useCollection.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
OnceOptions,
1010
OnceDataOptions,
1111
Options,
12+
InitialValueOptions,
1213
} from './types';
1314
import { useIsEqualRef, useLoadingValue } from '../util';
1415

@@ -32,7 +33,7 @@ export const useCollectionData = <
3233
RefField extends string = ''
3334
>(
3435
query?: firebase.firestore.Query | null,
35-
options?: DataOptions<T>
36+
options?: DataOptions<T> & InitialValueOptions<T[]>
3637
): CollectionDataHook<T, IDField, RefField> => {
3738
return useCollectionDataInternal<T, IDField, RefField>(true, query, options);
3839
};
@@ -43,7 +44,7 @@ export const useCollectionDataOnce = <
4344
RefField extends string = ''
4445
>(
4546
query?: firebase.firestore.Query | null,
46-
options?: OnceDataOptions<T>
47+
options?: OnceDataOptions<T> & InitialValueOptions<T[]>
4748
): CollectionDataHook<T, IDField, RefField> => {
4849
return useCollectionDataInternal<T, IDField, RefField>(false, query, options);
4950
};
@@ -100,7 +101,7 @@ const useCollectionDataInternal = <
100101
>(
101102
listen: boolean,
102103
query?: firebase.firestore.Query | null,
103-
options?: DataOptions<T> & OnceDataOptions<T>
104+
options?: DataOptions<T> & OnceDataOptions<T> & InitialValueOptions<T[]>
104105
): CollectionDataHook<T, IDField, RefField> => {
105106
const idField = options ? options.idField : undefined;
106107
const refField = options ? options.refField : undefined;
@@ -123,7 +124,7 @@ const useCollectionDataInternal = <
123124
transform
124125
)
125126
)
126-
: undefined) as Data<T, IDField, RefField>[],
127+
: options && options.initialValue) as Data<T, IDField, RefField>[],
127128
[snapshots, snapshotOptions, idField, refField, transform]
128129
);
129130

firestore/useDocument.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
OnceOptions,
1010
OnceDataOptions,
1111
Options,
12+
InitialValueOptions,
1213
} from './types';
1314
import { useIsEqualRef, useLoadingValue } from '../util';
1415

@@ -32,7 +33,7 @@ export const useDocumentData = <
3233
RefField extends string = ''
3334
>(
3435
docRef?: firebase.firestore.DocumentReference | null,
35-
options?: DataOptions<T>
36+
options?: DataOptions<T> & InitialValueOptions<T>
3637
): DocumentDataHook<T, IDField, RefField> => {
3738
return useDocumentDataInternal<T, IDField, RefField>(true, docRef, options);
3839
};
@@ -43,7 +44,7 @@ export const useDocumentDataOnce = <
4344
RefField extends string = ''
4445
>(
4546
docRef?: firebase.firestore.DocumentReference | null,
46-
options?: OnceDataOptions<T>
47+
options?: OnceDataOptions<T> & InitialValueOptions<T>
4748
): DocumentDataHook<T, IDField, RefField> => {
4849
return useDocumentDataInternal<T, IDField, RefField>(false, docRef, options);
4950
};
@@ -100,7 +101,7 @@ const useDocumentDataInternal = <
100101
>(
101102
listen: boolean,
102103
docRef?: firebase.firestore.DocumentReference | null,
103-
options?: DataOptions<T>
104+
options?: DataOptions<T> & InitialValueOptions<T>
104105
): DocumentDataHook<T, IDField, RefField> => {
105106
const idField = options ? options.idField : undefined;
106107
const refField = options ? options.refField : undefined;
@@ -121,7 +122,7 @@ const useDocumentDataInternal = <
121122
refField,
122123
transform
123124
)
124-
: undefined) as Data<T, IDField, RefField>,
125+
: options && options.initialValue) as Data<T, IDField, RefField>,
125126
[snapshot, snapshotOptions, idField, refField, transform]
126127
);
127128

0 commit comments

Comments
 (0)