Skip to content

Allow an initial value to be returned by the data hooks #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '12'

- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-

- name: Install
run: npm install
- name: Build
run: npm run build
4 changes: 4 additions & 0 deletions firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ The `useCollectionData` hook takes the following parameters:
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` for each item in the collection to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand All @@ -150,6 +151,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` for each item in the collection to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand Down Expand Up @@ -239,6 +241,7 @@ The `useDocumentData` hook takes the following parameters:
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand All @@ -263,6 +266,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below.
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand Down
3 changes: 3 additions & 0 deletions firestore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type Options = {
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
};
export type DataOptions<T> = Options & IDOptions<T>;
export type InitialValueOptions<T> = {
initialValue?: T;
};
export type OnceOptions = {
getOptions?: firebase.firestore.GetOptions;
};
Expand Down
9 changes: 5 additions & 4 deletions firestore/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OnceOptions,
OnceDataOptions,
Options,
InitialValueOptions,
} from './types';
import { useIsEqualRef, useLoadingValue } from '../util';

Expand All @@ -32,7 +33,7 @@ export const useCollectionData = <
RefField extends string = ''
>(
query?: firebase.firestore.Query | null,
options?: DataOptions<T>
options?: DataOptions<T> & InitialValueOptions<T[]>
): CollectionDataHook<T, IDField, RefField> => {
return useCollectionDataInternal<T, IDField, RefField>(true, query, options);
};
Expand All @@ -43,7 +44,7 @@ export const useCollectionDataOnce = <
RefField extends string = ''
>(
query?: firebase.firestore.Query | null,
options?: OnceDataOptions<T>
options?: OnceDataOptions<T> & InitialValueOptions<T[]>
): CollectionDataHook<T, IDField, RefField> => {
return useCollectionDataInternal<T, IDField, RefField>(false, query, options);
};
Expand Down Expand Up @@ -100,7 +101,7 @@ const useCollectionDataInternal = <
>(
listen: boolean,
query?: firebase.firestore.Query | null,
options?: DataOptions<T> & OnceDataOptions<T>
options?: DataOptions<T> & OnceDataOptions<T> & InitialValueOptions<T[]>
): CollectionDataHook<T, IDField, RefField> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
Expand All @@ -123,7 +124,7 @@ const useCollectionDataInternal = <
transform
)
)
: undefined) as Data<T, IDField, RefField>[],
: options && options.initialValue) as Data<T, IDField, RefField>[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we promote this to v5, I would suggest we change this to options?.initialValue as an easier way of safe navigation

[snapshots, snapshotOptions, idField, refField, transform]
);

Expand Down
9 changes: 5 additions & 4 deletions firestore/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OnceOptions,
OnceDataOptions,
Options,
InitialValueOptions,
} from './types';
import { useIsEqualRef, useLoadingValue } from '../util';

Expand All @@ -32,7 +33,7 @@ export const useDocumentData = <
RefField extends string = ''
>(
docRef?: firebase.firestore.DocumentReference | null,
options?: DataOptions<T>
options?: DataOptions<T> & InitialValueOptions<T>
): DocumentDataHook<T, IDField, RefField> => {
return useDocumentDataInternal<T, IDField, RefField>(true, docRef, options);
};
Expand All @@ -43,7 +44,7 @@ export const useDocumentDataOnce = <
RefField extends string = ''
>(
docRef?: firebase.firestore.DocumentReference | null,
options?: OnceDataOptions<T>
options?: OnceDataOptions<T> & InitialValueOptions<T>
): DocumentDataHook<T, IDField, RefField> => {
return useDocumentDataInternal<T, IDField, RefField>(false, docRef, options);
};
Expand Down Expand Up @@ -100,7 +101,7 @@ const useDocumentDataInternal = <
>(
listen: boolean,
docRef?: firebase.firestore.DocumentReference | null,
options?: DataOptions<T>
options?: DataOptions<T> & InitialValueOptions<T>
): DocumentDataHook<T, IDField, RefField> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
Expand All @@ -121,7 +122,7 @@ const useDocumentDataInternal = <
refField,
transform
)
: undefined) as Data<T, IDField, RefField>,
: options && options.initialValue) as Data<T, IDField, RefField>,
[snapshot, snapshotOptions, idField, refField, transform]
);

Expand Down