Skip to content

Commit 378db89

Browse files
committed
Return arrays rather than objects from all hooks
1 parent e2ebe99 commit 378db89

29 files changed

+624
-715
lines changed

auth/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@
22

33
React Firebase Hooks provides a convenience listener for Firebase Auth's auth state. The hook wraps around the `firebase.auth().onAuthStateChange()` method to ensure that it is always up to date.
44

5+
All hooks can be imported from `react-firebase-hooks/auth`, e.g.
6+
7+
```
8+
import { useAuthState } from 'react-firebase-hooks/auth';
9+
```
10+
511
List of Auth hooks:
612

7-
- [useAuthState](#useauthstateauth)
13+
- [useAuthState](#useauthstate)
814

9-
### `useAuthState(auth)`
15+
### useAuthState
1016

11-
Parameters:
17+
```
18+
const [user, loading, error] = useAuthState(auth);
19+
```
1220

13-
- `auth`: `firebase.auth.Auth`
21+
Returns the `firebase.User` (if logged in), a boolean to indicate whether the the user is still being loaded and any `firebase.FirebaseError` returned by Firebase when trying to load the user.
1422

15-
Returns:
16-
`AuthStateHook` containing:
23+
The `useAuthState` hook takes the following parameters:
1724

18-
- `initialising`: If the listener is still waiting for the user to be loaded
19-
- `user`: The `firebase.User`, or `null`, if no user is logged in
25+
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
2026

21-
#### Example
27+
#### Full Example
2228

2329
```js
2430
import { useAuthState } from 'react-firebase-hooks/auth';
2531

2632
const CurrentUser = () => {
27-
const { initialising, user } = useAuthState(firebase.auth());
33+
const [user, initialising, error] = useAuthState(firebase.auth());
2834
const login = () => {
2935
firebase.auth().signInWithEmailAndPassword('[email protected]', 'password');
3036
};
@@ -39,6 +45,13 @@ const CurrentUser = () => {
3945
</div>
4046
);
4147
}
48+
if (error) {
49+
return (
50+
<div>
51+
<p>Error: {error}>/p>
52+
</div>
53+
)
54+
}
4255
if (user) {
4356
return (
4457
<div>

auth/helpers/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { auth, FirebaseError } from 'firebase';
2+
3+
export const transformError = (error: auth.Error): FirebaseError => {
4+
return {
5+
message: error.message,
6+
stack: '',
7+
name: '',
8+
code: error.code,
9+
};
10+
};

auth/index.js.flow

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// @flow
22
import type { FirebaseUser as User } from 'firebase';
3+
import typeof { FirebaseError } from 'firebase';
34
import type { Auth } from 'firebase/auth';
4-
export type AuthStateHook = {
5-
user?: User,
6-
initialising: boolean,
7-
};
5+
6+
type LoadingHook<T> = [T | void, boolean, FirebaseError | void];
7+
8+
export type AuthStateHook = LoadingHook<User>;
89
declare export function useAuthState(auth: Auth): AuthStateHook;

auth/useAuthState.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { auth, User } from 'firebase';
1+
import { auth, FirebaseError, User } from 'firebase';
22
import { useEffect } from 'react';
3-
import useLoadingValue from '../util/useLoadingValue';
3+
import { transformError } from './helpers';
4+
import { LoadingHook, useLoadingValue } from '../util';
45

5-
export type AuthStateHook = {
6-
user?: firebase.User;
7-
initialising: boolean;
8-
};
6+
export type AuthStateHook = LoadingHook<User, FirebaseError>;
97

108
export default (auth: auth.Auth): AuthStateHook => {
11-
const { loading, setValue, value } = useLoadingValue<User>(
9+
const { error, loading, setError, setValue, value } = useLoadingValue<User>(
1210
() => auth.currentUser
1311
);
1412

1513
useEffect(
1614
() => {
17-
const listener = auth.onAuthStateChanged(setValue);
15+
const listener = auth.onAuthStateChanged(setValue, (error: auth.Error) =>
16+
setError(transformError(error))
17+
);
1818

1919
return () => {
2020
listener();
@@ -23,8 +23,5 @@ export default (auth: auth.Auth): AuthStateHook => {
2323
[auth]
2424
);
2525

26-
return {
27-
initialising: loading,
28-
user: value,
29-
};
26+
return [value, loading, error];
3027
};

database/README.md

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,39 @@ Firebase Realtime Database. The hooks wrap around the `firebase.database().ref()
66
In addition to returning the list or value, the hooks provide an `error` and `loading` property
77
to give a complete lifecycle for loading and listening to the Realtime Database.
88

9+
All hooks can be imported from `react-firebase-hooks/database`, e.g.
10+
11+
```
12+
import { useList } from 'react-firebase-hooks/database';
13+
```
14+
915
List of Realtime Database hooks:
1016

1117
- [useList](#uselistref)
12-
- [useListKeys](#uselistkeystref)
13-
- [useListVals](#uselistvalstref-keyfield)
18+
- [useListKeys](#uselistkeyst)
19+
- [useListVals](#uselistvals)
1420
- [useObject](#useobjectref)
15-
- [useObjectVal](#useobjectvaltref)
21+
- [useObjectVal](#useobjectval)
1622

17-
### `useList(ref)`
23+
### useList
1824

19-
Parameters:
25+
```
26+
const [snapshots, loading, error] = useList(reference);
27+
```
2028

21-
- `ref`: `firebase.database.Reference`
29+
Returns an array of `firebase.database.DataSnapshot` (if a reference is specified), a `boolean` to indicate if the data is still being loaded and any `firebase.FirebaseError` returned by Firebase when trying to load the data.
2230

23-
Returns:
24-
`ListHook` containing
31+
The `useList` hook takes the following parameters:
2532

26-
- `error`: An optional error object returned by Firebase
27-
- `loading`: A `boolean` to indicate if the listener is still being loaded
28-
- `value`: A list of `firebase.database.DataSnapshot`
33+
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
2934

30-
#### Example
35+
#### Full Example
3136

3237
```js
3338
import { useList } from 'react-firebase-hooks/database';
3439

3540
const DatabaseList = () => {
36-
const { error, loading, value } = useList(firebase.database().ref('list'));
41+
const [snapshots, loading, error] = useList(firebase.database().ref('list'));
3742

3843
return (
3944
<div>
@@ -56,59 +61,51 @@ const DatabaseList = () => {
5661
};
5762
```
5863

59-
### `useListKeys<T>(ref)`
64+
### useListKeys
6065

61-
As above, but this hook returns a list of the `DataSnapshot.key` values, rather than the the
62-
`DataSnapshot`s themselves.
66+
```
67+
const [keys, loading, error] = useListKeys(reference);
68+
```
6369

64-
Parameters:
70+
As `useList`, but this hook returns a list of the `firebase.database.DataSnapshot.key` values, rather than the the `firebase.database.DataSnapshot`s themselves.
6571

66-
- `ref`: `firebase.database.Reference`
72+
The `useListKeys` hook takes the following parameters:
6773

68-
Returns:
69-
`ListKeysHook` containing
74+
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
7075

71-
- `error`: An optional error object returned by Firebase
72-
- `loading`: A `boolean` to indicate if the listener is still being loaded
73-
- `value`: A list of `firebase.database.DataSnapshot.key` values
76+
### useListVals
7477

75-
### `useListVals<T>(ref, keyField)`
78+
```
79+
const [values, loading, error] = useListVals<T>(reference, keyField);
80+
```
7681

77-
Similar to `useList`, but this hook returns a typed list of the `DataSnapshot.val()` values, rather than the the
82+
As `useList`, but this hook returns a typed list of the `firebase.database.DataSnapshot.val()` values, rather than the the
7883
`DataSnapshot`s themselves.
7984

80-
Parameters:
81-
82-
- `ref`: `firebase.database.Reference`
83-
- `keyField`: (Optional) Name of field that should be populated with the `DataSnapshot.key` property
84-
85-
Returns:
86-
`ListValsHook` containing
85+
The `useListVals` hook takes the following parameters:
8786

88-
- `error`: An optional error object returned by Firebase
89-
- `loading`: A `boolean` to indicate if the listener is still being loaded
90-
- `value`: A list of `firebase.database.DataSnapshot.val()` values, combined with the optional key field
87+
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
88+
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
9189

92-
### `useObject(ref)`
90+
### useObject
9391

94-
Parameters:
92+
```
93+
const [snapshot, loading, error] = useObject(reference);
94+
```
9595

96-
- `ref`: `firebase.database.Reference`
96+
Returns a `firebase.database.DataSnapshot` (if a reference is specified), a `boolean` to indicate if the data is still being loaded and any `firebase.FirebaseError` returned by Firebase when trying to load the data.
9797

98-
Returns:
99-
`ObjectHook` containing
98+
The `useObject` hook takes the following parameters:
10099

101-
- `error`: An optional error object returned by Firebase
102-
- `loading`: A `boolean` to indicate if the listener is still being loaded
103-
- `value`: A `firebase.database.DataSnapshot`
100+
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
104101

105-
#### Example
102+
#### Full Example
106103

107104
```js
108105
import { useObject } from 'react-firebase-hooks/database';
109106

110107
const DatabaseValue = () => {
111-
const { error, loading, value } = useObject(firebase.database().ref('value'));
108+
const [value, loading, error] = useObject(firebase.database().ref('value'));
112109

113110
return (
114111
<div>
@@ -122,19 +119,16 @@ const DatabaseValue = () => {
122119
};
123120
```
124121

125-
### `useObjectVal<T>(ref)`
122+
### useObjectVal
126123

127-
As above, but this hook returns the typed contents of `DataSnapshot.val()` rather than the
128-
`DataSnapshot` itself.
129-
130-
Parameters:
124+
```
125+
const [value, loading, error] = useObjectVal<T>(reference, keyField);
126+
```
131127

132-
- `ref`: `firebase.database.Reference`
133-
- `keyField`: (Optional) Name of field that should be populated with the `DataSnapshot.key` property
128+
As `useObject`, but this hook returns the typed contents of `DataSnapshot.val()` rather than the
129+
`DataSnapshot` itself.
134130

135-
Returns:
136-
`ObjectValHook` containing
131+
The `useObjectVal` hook takes the following parameters:
137132

138-
- `error`: An optional error object returned by Firebase
139-
- `loading`: A `boolean` to indicate if the listener is still being loaded
140-
- `value`: The contents of `firebase.database.DataSnapshot.val()`, combined with the optional key field
133+
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
134+
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.

0 commit comments

Comments
 (0)