Skip to content

Commit c520c3f

Browse files
authored
Merge pull request #191 from dylanwatsonsoftware/initial-value
Allow an initial value to be returned by the data hooks
2 parents 16ca17e + 8b56bb8 commit c520c3f

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

.github/workflows/build.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: build
2+
on: push
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- uses: actions/setup-node@v2
9+
with:
10+
node-version: '12'
11+
12+
- name: Cache node modules
13+
uses: actions/cache@v2
14+
env:
15+
cache-name: cache-node-modules
16+
with:
17+
# npm cache files are stored in `~/.npm` on Linux/macOS
18+
path: ~/.npm
19+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
20+
restore-keys: |
21+
${{ runner.os }}-build-${{ env.cache-name }}-
22+
${{ runner.os }}-build-
23+
${{ runner.os }}-
24+
25+
- name: Install
26+
run: npm install
27+
- name: Build
28+
run: npm run build

firestore/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ The `useCollectionData` hook takes the following parameters:
126126
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
127127
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
128128
- `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.
129+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
129130

130131
Returns:
131132

@@ -150,6 +151,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
150151
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
151152
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
152153
- `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.
154+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
153155

154156
Returns:
155157

@@ -239,6 +241,7 @@ The `useDocumentData` hook takes the following parameters:
239241
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
240242
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
241243
- `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.
244+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
242245

243246
Returns:
244247

@@ -263,6 +266,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
263266
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
264267
- `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots
265268
- `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.
269+
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded
266270

267271
Returns:
268272

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)