From 8dad1f23febfacc7cb37663523552eeb66ffc715 Mon Sep 17 00:00:00 2001 From: Dylan Watson Date: Tue, 21 Dec 2021 23:22:26 +0800 Subject: [PATCH 1/2] Add documentation for the initialValue option --- firestore/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firestore/README.md b/firestore/README.md index 61d6585..3dbdfa4 100644 --- a/firestore/README.md +++ b/firestore/README.md @@ -128,6 +128,7 @@ The `useCollectionData` hook takes the following parameters: - `options`: (optional) `Object` with the following parameters: - `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded - `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: @@ -154,6 +155,7 @@ The `useCollectionDataOnce` hook takes the following parameters: - `getOptions`: (optional) `Object` to customise how the collection is loaded - `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache. - `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: @@ -247,6 +249,7 @@ The `useDocumentData` hook takes the following parameters: - `options`: (optional) `Object` with the following parameters: - `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded - `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: @@ -273,6 +276,7 @@ The `useDocumentDataOnce` hook takes the following parameters: - `getOptions`: (optional) `Object` to customise how the collection is loaded - `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cach - `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: From fc87a3c4b43c8fd78413c2980f8a463ae7cffe01 Mon Sep 17 00:00:00 2001 From: mauriceackel Date: Mon, 10 Oct 2022 13:29:36 +0200 Subject: [PATCH 2/2] feat: Allow an initialValue to be passed as an option on data methods --- firestore/types.ts | 3 +++ firestore/useCollection.ts | 41 ++++++++++++++++++++++++++++---------- firestore/useDocument.ts | 25 +++++++++++++---------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/firestore/types.ts b/firestore/types.ts index 3fbb911..e28ab41 100644 --- a/firestore/types.ts +++ b/firestore/types.ts @@ -14,6 +14,9 @@ export type IDOptions = { export type Options = { snapshotListenOptions?: SnapshotListenOptions; }; +export type InitialValueOptions = { + initialValue?: T; +}; export type DataOptions = Options & IDOptions; export type OnceOptions = { getOptions?: GetOptions; diff --git a/firestore/useCollection.ts b/firestore/useCollection.ts index b4e72cd..8ced102 100644 --- a/firestore/useCollection.ts +++ b/firestore/useCollection.ts @@ -19,6 +19,7 @@ import { CollectionOnceHook, DataOptions, GetOptions, + InitialValueOptions, OnceDataOptions, OnceOptions, Options, @@ -113,25 +114,39 @@ export const useCollectionOnce = ( export const useCollectionData = ( query?: Query | null, - options?: DataOptions + options?: DataOptions & InitialValueOptions ): CollectionDataHook => { const snapshotOptions = options?.snapshotOptions; const [snapshots, loading, error] = useCollection(query, options); - const values = getValuesFromSnapshots(snapshots, snapshotOptions); + + const initialValue = options?.initialValue; + const values = getValuesFromSnapshots( + snapshots, + snapshotOptions, + initialValue + ); + const resArray: CollectionDataHook = [values, loading, error, snapshots]; return useMemo(() => resArray, resArray); }; export const useCollectionDataOnce = ( query?: Query | null, - options?: OnceDataOptions + options?: OnceDataOptions & InitialValueOptions ): CollectionDataOnceHook => { const snapshotOptions = options?.snapshotOptions; const [snapshots, loading, error, loadData] = useCollectionOnce( query, options ); - const values = getValuesFromSnapshots(snapshots, snapshotOptions); + + const initialValue = options?.initialValue; + const values = getValuesFromSnapshots( + snapshots, + snapshotOptions, + initialValue + ); + const resArray: CollectionDataOnceHook = [ values, loading, @@ -143,13 +158,17 @@ export const useCollectionDataOnce = ( }; const getValuesFromSnapshots = ( - snapshots?: QuerySnapshot, - options?: SnapshotOptions -) => { - return useMemo(() => snapshots?.docs.map((doc) => doc.data(options)) as T[], [ - snapshots, - options, - ]); + snapshots: QuerySnapshot | undefined, + options?: SnapshotOptions, + initialValue?: T[] +): T[] | undefined => { + return useMemo( + () => + (snapshots?.docs.map((doc) => doc.data(options)) ?? initialValue) as + | T[] + | undefined, + [snapshots, options] + ); }; const getDocsFnFromGetOptions = ( diff --git a/firestore/useDocument.ts b/firestore/useDocument.ts index 7077ba7..1dfa7b9 100644 --- a/firestore/useDocument.ts +++ b/firestore/useDocument.ts @@ -18,6 +18,7 @@ import { DocumentHook, DocumentOnceHook, GetOptions, + InitialValueOptions, OnceDataOptions, OnceOptions, Options, @@ -117,14 +118,16 @@ export const useDocumentOnce = ( export const useDocumentData = ( docRef?: DocumentReference | null, - options?: DataOptions + options?: DataOptions & InitialValueOptions ): DocumentDataHook => { const snapshotOptions = options?.snapshotOptions; const [snapshot, loading, error] = useDocument(docRef, options); - const value = useMemo(() => snapshot?.data(snapshotOptions) as T, [ - snapshot, - snapshotOptions, - ]); + + const initialValue = options?.initialValue; + const value = useMemo( + () => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined, + [snapshot, snapshotOptions, initialValue] + ); const resArray: DocumentDataHook = [value, loading, error, snapshot]; return useMemo(() => resArray, resArray); @@ -132,17 +135,19 @@ export const useDocumentData = ( export const useDocumentDataOnce = ( docRef?: DocumentReference | null, - options?: OnceDataOptions + options?: OnceDataOptions & InitialValueOptions ): DocumentDataOnceHook => { const snapshotOptions = options?.snapshotOptions; const [snapshot, loading, error, loadData] = useDocumentOnce( docRef, options ); - const value = useMemo(() => snapshot?.data(snapshotOptions) as T, [ - snapshot, - snapshotOptions, - ]); + + const initialValue = options?.initialValue; + const value = useMemo( + () => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined, + [snapshot, snapshotOptions, initialValue] + ); const resArray: DocumentDataOnceHook = [ value,