Skip to content

Commit 7888610

Browse files
authored
Merge branch 'master' into memoize_hook_return
2 parents cf815de + 08d35f5 commit 7888610

File tree

17 files changed

+832
-1027
lines changed

17 files changed

+832
-1027
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: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
import { auth, User } from 'firebase';
1+
import firebase from 'firebase/app';
22
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

2421
const resArray:AuthStateHook = [value, loading, error]
2522
return useMemo<AuthStateHook>(

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: 19 additions & 14 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;
@@ -71,7 +74,9 @@ export const useList = (query?: database.Query | null): ListHook => {
7174
);
7275
};
7376

74-
export const useListKeys = (query?: database.Query | null): ListKeysHook => {
77+
export const useListKeys = (
78+
query?: database.Query | null
79+
): ListKeysHook => {
7580
const [snapshots, loading, error] = useList(query);
7681
const values = useMemo(
7782
() => (snapshots ? snapshots.map(snapshot => snapshot.key as string) : undefined),
@@ -90,7 +95,7 @@ export const useListKeys = (query?: database.Query | null): ListKeysHook => {
9095
};
9196

9297
export const useListVals = <T>(
93-
query?: database.Query | null,
98+
query?: firebase.database.Query | null,
9499
options?: {
95100
keyField?: string;
96101
}
@@ -99,7 +104,7 @@ export const useListVals = <T>(
99104
const values = useMemo(
100105
() =>
101106
snapshots
102-
? snapshots.map(snapshot =>
107+
? snapshots.map((snapshot) =>
103108
snapshotToData(snapshot, options ? options.keyField : undefined)
104109
)
105110
: undefined,

database/useObject.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
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

3335
const resArray: ObjectHook = [value, loading, error];
3436
return useMemo(
@@ -38,7 +40,7 @@ export const useObject = (query?: database.Query | null): ObjectHook => {
3840
};
3941

4042
export const useObjectVal = <T>(
41-
query?: database.Query | null,
43+
query?: firebase.database.Query | null,
4244
options?: {
4345
keyField?: string;
4446
}

0 commit comments

Comments
 (0)