Skip to content

Commit 2094d86

Browse files
authored
Merge branch 'master' into useList-efficiency
2 parents 4de8e83 + 505b29d commit 2094d86

19 files changed

+967
-1090
lines changed

auth/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ React Firebase Hooks provides a convenience listener for Firebase Auth's auth st
44

55
All hooks can be imported from `react-firebase-hooks/auth`, e.g.
66

7-
```
7+
```js
88
import { useAuthState } from 'react-firebase-hooks/auth';
99
```
1010

@@ -14,7 +14,7 @@ List of Auth hooks:
1414

1515
### useAuthState
1616

17-
```
17+
```js
1818
const [user, loading, error] = useAuthState(auth);
1919
```
2020

auth/useAuthState.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import { auth, User } from 'firebase';
2-
import { useEffect } from 'react';
1+
import firebase from 'firebase/app';
2+
import { useEffect, useMemo } from 'react';
33
import { LoadingHook, useLoadingValue } from '../util';
44

5-
export type AuthStateHook = LoadingHook<User, auth.Error>;
5+
export type AuthStateHook = LoadingHook<firebase.User, firebase.auth.Error>;
66

7-
export default (auth: auth.Auth): AuthStateHook => {
7+
export default (auth: firebase.auth.Auth): AuthStateHook => {
88
const { error, loading, setError, setValue, value } = useLoadingValue<
9-
User,
10-
auth.Error
9+
firebase.User,
10+
firebase.auth.Error
1111
>(() => auth.currentUser);
1212

13-
useEffect(
14-
() => {
15-
const listener = auth.onAuthStateChanged(setValue, setError);
13+
useEffect(() => {
14+
const listener = auth.onAuthStateChanged(setValue, setError);
1615

17-
return () => {
18-
listener();
19-
};
20-
},
21-
[auth]
22-
);
16+
return () => {
17+
listener();
18+
};
19+
}, [auth]);
2320

24-
return [value, loading, error];
21+
const resArray:AuthStateHook = [value, loading, error]
22+
return useMemo<AuthStateHook>(
23+
() => resArray,
24+
resArray,
25+
);
2526
};

database/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ to give a complete lifecycle for loading and listening to the Realtime Database.
88

99
All hooks can be imported from `react-firebase-hooks/database`, e.g.
1010

11-
```
11+
```js
1212
import { useList } from 'react-firebase-hooks/database';
1313
```
1414

@@ -22,7 +22,7 @@ List of Realtime Database hooks:
2222

2323
### useList
2424

25-
```
25+
```js
2626
const [snapshots, loading, error] = useList(reference);
2727
```
2828

@@ -69,7 +69,7 @@ const DatabaseList = () => {
6969

7070
### useListKeys
7171

72-
```
72+
```js
7373
const [keys, loading, error] = useListKeys(reference);
7474
```
7575

@@ -87,8 +87,8 @@ Returns:
8787

8888
### useListVals
8989

90-
```
91-
const [values, loading, error] = useListVals<T>(reference, options);
90+
```js
91+
const [values, loading, error] = useListVals < T > (reference, options);
9292
```
9393

9494
As `useList`, but this hook extracts a typed list of the `firebase.database.DataSnapshot.val()` values, rather than the the
@@ -98,7 +98,8 @@ The `useListVals` hook takes the following parameters:
9898

9999
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
100100
- `options`: (optional) `Object` with the following parameters:
101-
- `keyField`: (optional) `string` field name that should be populated with the `firebase.firestore.QuerySnapshot.id` property in the returned values
101+
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.id` property in the returned values.
102+
- `refField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.ref` property.
102103

103104
Returns:
104105

@@ -108,7 +109,7 @@ Returns:
108109

109110
### useObject
110111

111-
```
112+
```js
112113
const [snapshot, loading, error] = useObject(reference);
113114
```
114115

@@ -146,8 +147,8 @@ const DatabaseValue = () => {
146147

147148
### useObjectVal
148149

149-
```
150-
const [value, loading, error] = useObjectVal<T>(reference, options);
150+
```js
151+
const [value, loading, error] = useObjectVal < T > (reference, options);
151152
```
152153

153154
As `useObject`, but this hook returns the typed contents of `firebase.database.DataSnapshot.val()`, rather than the the
@@ -158,6 +159,7 @@ The `useObjectVal` hook takes the following parameters:
158159
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
159160
- `options`: (optional) `Object` with the following parameters:
160161
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
162+
- `refField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.ref` property.
161163

162164
Returns:
163165

database/helpers/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { database } from 'firebase';
1+
import firebase from 'firebase/app';
22

33
const isObject = (val: any) =>
44
val != null && typeof val === 'object' && Array.isArray(val) === false;
55

66
export const snapshotToData = (
7-
snapshot: database.DataSnapshot,
8-
keyField?: string
7+
snapshot: firebase.database.DataSnapshot,
8+
keyField?: string,
9+
refField?: string
910
) => {
1011
if (!snapshot.exists) {
1112
return undefined;
@@ -16,6 +17,7 @@ export const snapshotToData = (
1617
return {
1718
...val,
1819
...(keyField ? { [keyField]: snapshot.key } : null),
20+
...(refField ? { [refField]: snapshot.ref } : null),
1921
};
2022
}
2123
return val;

database/helpers/useListReducer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import { database, FirebaseError } from 'firebase';
1+
import firebase from 'firebase/app';
22
import { useReducer } from 'react';
33

44
type KeyValueState = {
55
keys?: string[];
6-
values?: database.DataSnapshot[];
6+
values?: firebase.database.DataSnapshot[];
77
};
88

99
type ReducerState = {
10-
error?: FirebaseError;
10+
error?: firebase.FirebaseError;
1111
loading: boolean;
1212
value: KeyValueState;
1313
};
1414

1515
type AddAction = {
1616
type: 'add';
1717
previousKey?: string | null;
18-
snapshot: database.DataSnapshot | null;
18+
snapshot: firebase.database.DataSnapshot | null;
1919
};
2020
type ChangeAction = {
2121
type: 'change';
22-
snapshot: database.DataSnapshot | null;
22+
snapshot: firebase.database.DataSnapshot | null;
2323
};
2424
type EmptyAction = { type: 'empty' };
25-
type ErrorAction = { type: 'error'; error: FirebaseError };
25+
type ErrorAction = { type: 'error'; error: firebase.FirebaseError };
2626
type MoveAction = {
2727
type: 'move';
2828
previousKey?: string | null;
29-
snapshot: database.DataSnapshot | null;
29+
snapshot: firebase.database.DataSnapshot | null;
3030
};
3131
type RemoveAction = {
3232
type: 'remove';
33-
snapshot: database.DataSnapshot | null;
33+
snapshot: firebase.database.DataSnapshot | null;
3434
};
3535
type ResetAction = { type: 'reset' };
3636
type ValueAction = { type: 'value'; snapshots: database.DataSnapshot[] | null };

database/useList.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
1-
import { database, FirebaseError } from 'firebase';
1+
import firebase from 'firebase/app';
22
import { useEffect, useMemo } from 'react';
33
import { snapshotToData } from './helpers';
44
import useListReducer from './helpers/useListReducer';
55
import { LoadingHook, useIsEqualRef } from '../util';
66

7-
export type ListHook = LoadingHook<database.DataSnapshot[], FirebaseError>;
8-
export type ListKeysHook = LoadingHook<string[], FirebaseError>;
9-
export type ListValsHook<T> = LoadingHook<T[], FirebaseError>;
7+
export type ListHook = LoadingHook<
8+
firebase.database.DataSnapshot[],
9+
firebase.FirebaseError
10+
>;
11+
export type ListKeysHook = LoadingHook<string[], firebase.FirebaseError>;
12+
export type ListValsHook<T> = LoadingHook<T[], firebase.FirebaseError>;
1013

11-
export const useList = (query?: database.Query | null): ListHook => {
14+
export const useList = (query?: firebase.database.Query | null): ListHook => {
1215
const [state, dispatch] = useListReducer();
1316

1417
const queryRef = useIsEqualRef(query, () => dispatch({ type: 'reset' }));
1518

1619
useEffect(() => {
17-
const ref: database.Query | null | undefined = queryRef.current;
20+
const ref: firebase.database.Query | null | undefined = queryRef.current;
1821
if (!ref) {
1922
dispatch({ type: 'empty' });
2023
return;
2124
}
2225

2326
const onChildAdded = (
24-
snapshot: database.DataSnapshot | null,
27+
snapshot: firebase.database.DataSnapshot | null,
2528
previousKey?: string | null
2629
) => {
2730
dispatch({ type: 'add', previousKey, snapshot });
2831
};
2932

30-
const onChildChanged = (snapshot: database.DataSnapshot | null) => {
33+
const onChildChanged = (snapshot: firebase.database.DataSnapshot | null) => {
3134
dispatch({ type: 'change', snapshot });
3235
};
3336

3437
const onChildMoved = (
35-
snapshot: database.DataSnapshot | null,
38+
snapshot: firebase.database.DataSnapshot | null,
3639
previousKey?: string | null
3740
) => {
3841
dispatch({ type: 'move', previousKey, snapshot });
3942
};
4043

41-
const onChildRemoved = (snapshot: database.DataSnapshot | null) => {
44+
const onChildRemoved = (snapshot: firebase.database.DataSnapshot | null) => {
4245
dispatch({ type: 'remove', snapshot });
4346
};
4447

45-
const onError = (error: FirebaseError) => {
48+
const onError = (error: firebase.FirebaseError) => {
4649
dispatch({ type: 'error', error });
4750
};
4851

49-
const onValue = (snapshots: database.DataSnapshot[] | null) => {
52+
const onValue = (snapshots: firebase.database.DataSnapshot[] | null) => {
5053
dispatch({ type: 'value', snapshots });
5154
};
5255

5356
let childAddedHandler: ReturnType<typeof ref.on> | undefined;
54-
const children: database.DataSnapshot[] = [];
55-
const onInitialLoad = (snapshot: database.DataSnapshot) => {
57+
const children: firebase.database.DataSnapshot[] = [];
58+
const onInitialLoad = (snapshot: firebase.database.DataSnapshot) => {
5659
let childrenToProcess = Object.keys(snapshot.val()).length;
5760

5861
const onChildAddedWithoutInitialLoad = (
59-
addedChild: database.DataSnapshot,
62+
addedChild: firebase.database.DataSnapshot,
6063
previousKey?: string
6164
) => {
6265
// process the first batch of children all at once
@@ -102,10 +105,11 @@ export const useList = (query?: database.Query | null): ListHook => {
102105
};
103106
}, [dispatch, queryRef]);
104107

105-
return [state.value.values, state.loading, state.error];
108+
const resArray: ListHook = [state.value.values, state.loading, state.error];
109+
return useMemo(() => resArray, resArray);
106110
};
107111

108-
export const useListKeys = (query?: database.Query | null): ListKeysHook => {
112+
export const useListKeys = (query?: firebase.database.Query | null): ListKeysHook => {
109113
const [value, loading, error] = useList(query);
110114
return [
111115
value ? value.map((snapshot) => snapshot.key as string) : undefined,
@@ -115,20 +119,25 @@ export const useListKeys = (query?: database.Query | null): ListKeysHook => {
115119
};
116120

117121
export const useListVals = <T>(
118-
query?: database.Query | null,
122+
query?: firebase.database.Query | null,
119123
options?: {
120124
keyField?: string;
125+
refField?: string;
121126
}
122127
): ListValsHook<T> => {
128+
const keyField = options ? options.keyField : undefined;
129+
const refField = options ? options.refField : undefined;
123130
const [snapshots, loading, error] = useList(query);
124131
const values = useMemo(
125132
() =>
126133
snapshots
127134
? snapshots.map((snapshot) =>
128-
snapshotToData(snapshot, options ? options.keyField : undefined)
135+
snapshotToData(snapshot, options ? options.keyField : undefined, refField)
129136
)
130137
: undefined,
131138
[snapshots, options && options.keyField]
132139
);
133-
return [values, loading, error];
140+
141+
const resArray: ListValsHook<T> = [values, loading, error];
142+
return useMemo(() => resArray, resArray);
134143
};

0 commit comments

Comments
 (0)