Skip to content

Commit 8a2583a

Browse files
authored
Merge branch 'master' into feature/hydrate-reference
2 parents 9cdf178 + faad677 commit 8a2583a

File tree

18 files changed

+955
-1080
lines changed

18 files changed

+955
-1080
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: 8 additions & 8 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
@@ -108,7 +108,7 @@ Returns:
108108

109109
### useObject
110110

111-
```
111+
```js
112112
const [snapshot, loading, error] = useObject(reference);
113113
```
114114

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

147147
### useObjectVal
148148

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

153153
As `useObject`, but this hook returns the typed contents of `firebase.database.DataSnapshot.val()`, rather than the the

database/helpers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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,
7+
snapshot: firebase.database.DataSnapshot,
88
keyField?: string
99
) => {
1010
if (!snapshot.exists) {

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' };

database/useList.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
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 ref = useIsEqualRef(query, () => dispatch({ type: 'reset' }));
1518

1619
const onChildAdded = (
17-
snapshot: database.DataSnapshot | null,
20+
snapshot: firebase.database.DataSnapshot | null,
1821
previousKey?: string | null
1922
) => {
2023
dispatch({ type: 'add', previousKey, snapshot });
2124
};
2225

23-
const onChildChanged = (snapshot: database.DataSnapshot | null) => {
26+
const onChildChanged = (snapshot: firebase.database.DataSnapshot | null) => {
2427
dispatch({ type: 'change', snapshot });
2528
};
2629

2730
const onChildMoved = (
28-
snapshot: database.DataSnapshot | null,
31+
snapshot: firebase.database.DataSnapshot | null,
2932
previousKey?: string | null
3033
) => {
3134
dispatch({ type: 'move', previousKey, snapshot });
3235
};
3336

34-
const onChildRemoved = (snapshot: database.DataSnapshot | null) => {
37+
const onChildRemoved = (snapshot: firebase.database.DataSnapshot | null) => {
3538
dispatch({ type: 'remove', snapshot });
3639
};
3740

38-
const onError = (error: FirebaseError) => {
41+
const onError = (error: firebase.FirebaseError) => {
3942
dispatch({ type: 'error', error });
4043
};
4144

@@ -44,7 +47,7 @@ export const useList = (query?: database.Query | null): ListHook => {
4447
};
4548

4649
useEffect(() => {
47-
const query: database.Query | null | undefined = ref.current;
50+
const query: firebase.database.Query | null | undefined = ref.current;
4851
if (!query) {
4952
dispatch({ type: 'empty' });
5053
return;
@@ -64,20 +67,35 @@ export const useList = (query?: database.Query | null): ListHook => {
6467
};
6568
}, [ref.current]);
6669

67-
return [state.value.values, state.loading, state.error];
70+
const resArray: ListHook = [state.value.values, state.loading, state.error];
71+
return useMemo(
72+
() => resArray,
73+
resArray,
74+
);
6875
};
6976

70-
export const useListKeys = (query?: database.Query | null): ListKeysHook => {
71-
const [value, loading, error] = useList(query);
72-
return [
73-
value ? value.map(snapshot => snapshot.key as string) : undefined,
77+
export const useListKeys = (
78+
query?: firebase.database.Query | null
79+
): ListKeysHook => {
80+
const [snapshots, loading, error] = useList(query);
81+
const values = useMemo(
82+
() => (snapshots ? snapshots.map(snapshot => snapshot.key as string) : undefined),
83+
[snapshots]
84+
);
85+
const resArray: ListKeysHook = [
86+
values,
7487
loading,
7588
error,
7689
];
90+
91+
return useMemo(
92+
() => resArray,
93+
resArray,
94+
);
7795
};
7896

7997
export const useListVals = <T>(
80-
query?: database.Query | null,
98+
query?: firebase.database.Query | null,
8199
options?: {
82100
keyField?: string;
83101
}
@@ -86,11 +104,16 @@ export const useListVals = <T>(
86104
const values = useMemo(
87105
() =>
88106
snapshots
89-
? snapshots.map(snapshot =>
107+
? snapshots.map((snapshot) =>
90108
snapshotToData(snapshot, options ? options.keyField : undefined)
91109
)
92110
: undefined,
93111
[snapshots, options && options.keyField]
94112
);
95-
return [values, loading, error];
113+
114+
const resArray: ListValsHook<T> = [values, loading, error];
115+
return useMemo(
116+
() => resArray,
117+
resArray,
118+
);
96119
};

database/useObject.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
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 { LoadingHook, useIsEqualRef, useLoadingValue } from '../util';
55

6-
export type ObjectHook = LoadingHook<database.DataSnapshot, FirebaseError>;
7-
export type ObjectValHook<T> = LoadingHook<T, FirebaseError>;
6+
export type ObjectHook = LoadingHook<
7+
firebase.database.DataSnapshot,
8+
firebase.FirebaseError
9+
>;
10+
export type ObjectValHook<T> = LoadingHook<T, firebase.FirebaseError>;
811

9-
export const useObject = (query?: database.Query | null): ObjectHook => {
12+
export const useObject = (
13+
query?: firebase.database.Query | null
14+
): ObjectHook => {
1015
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
11-
database.DataSnapshot,
12-
FirebaseError
16+
firebase.database.DataSnapshot,
17+
firebase.FirebaseError
1318
>();
1419
const ref = useIsEqualRef(query, reset);
1520

16-
useEffect(
17-
() => {
18-
const query = ref.current;
19-
if (!query) {
20-
setValue(undefined);
21-
return;
22-
}
23-
24-
query.on('value', setValue, setError);
25-
26-
return () => {
27-
query.off('value', setValue);
28-
};
29-
},
30-
[ref.current]
31-
);
21+
useEffect(() => {
22+
const query = ref.current;
23+
if (!query) {
24+
setValue(undefined);
25+
return;
26+
}
27+
28+
query.on('value', setValue, setError);
29+
30+
return () => {
31+
query.off('value', setValue);
32+
};
33+
}, [ref.current]);
3234

33-
return [value, loading, error];
35+
const resArray: ObjectHook = [value, loading, error];
36+
return useMemo(
37+
() => resArray,
38+
resArray,
39+
);
3440
};
3541

3642
export const useObjectVal = <T>(
37-
query?: database.Query | null,
43+
query?: firebase.database.Query | null,
3844
options?: {
3945
keyField?: string;
4046
}
@@ -47,5 +53,10 @@ export const useObjectVal = <T>(
4753
: undefined,
4854
[snapshot, options && options.keyField]
4955
);
50-
return [value, loading, error];
56+
57+
const resArray: ObjectValHook<T> = [value, loading, error];
58+
return useMemo(
59+
() => resArray,
60+
resArray,
61+
);
5162
};

0 commit comments

Comments
 (0)