Skip to content

Commit d8f2e4a

Browse files
authored
Obey the Rules of Hooks (#367)
1 parent 4d723a8 commit d8f2e4a

File tree

12 files changed

+62
-101
lines changed

12 files changed

+62
-101
lines changed

docs/reference/interfaces/auth.authcheckprops.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,30 @@
88

99
### Properties
1010

11-
- [auth](auth.authcheckprops.md#auth)
1211
- [children](auth.authcheckprops.md#children)
1312
- [fallback](auth.authcheckprops.md#fallback)
1413
- [requiredClaims](auth.authcheckprops.md#requiredclaims)
1514

1615
## Properties
1716

18-
### auth
19-
20-
`Optional` **auth**: Auth
21-
22-
Defined in: [src/auth.tsx:61](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L61)
23-
24-
___
25-
2617
### children
2718

2819
**children**: ReactNode
2920

30-
Defined in: [src/auth.tsx:63](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L63)
21+
Defined in: [src/auth.tsx:54](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L54)
3122

3223
___
3324

3425
### fallback
3526

3627
**fallback**: ReactNode
3728

38-
Defined in: [src/auth.tsx:62](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L62)
29+
Defined in: [src/auth.tsx:53](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L53)
3930

4031
___
4132

4233
### requiredClaims
4334

4435
`Optional` **requiredClaims**: Object
4536

46-
Defined in: [src/auth.tsx:64](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L64)
37+
Defined in: [src/auth.tsx:55](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L55)

docs/reference/interfaces/auth.claimscheckprops.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919

2020
**children**: ReactNode
2121

22-
Defined in: [src/auth.tsx:70](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L70)
22+
Defined in: [src/auth.tsx:61](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L61)
2323

2424
___
2525

2626
### fallback
2727

2828
**fallback**: ReactNode
2929

30-
Defined in: [src/auth.tsx:69](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L69)
30+
Defined in: [src/auth.tsx:60](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L60)
3131

3232
___
3333

@@ -37,12 +37,12 @@ ___
3737

3838
#### Type declaration:
3939

40-
Defined in: [src/auth.tsx:71](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L71)
40+
Defined in: [src/auth.tsx:62](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L62)
4141

4242
___
4343

4444
### user
4545

4646
**user**: User
4747

48-
Defined in: [src/auth.tsx:68](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L68)
48+
Defined in: [src/auth.tsx:59](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L59)

docs/reference/modules/auth.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
**Returns:** JSX.Element
3333

34-
Defined in: [src/auth.tsx:97](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L97)
34+
Defined in: [src/auth.tsx:88](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L88)
3535

3636
___
3737

@@ -47,24 +47,24 @@ ___
4747

4848
**Returns:** *Element*
4949

50-
Defined in: [src/auth.tsx:74](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L74)
50+
Defined in: [src/auth.tsx:65](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L65)
5151

5252
___
5353

5454
### preloadUser
5555

56-
**preloadUser**(`options?`: { `firebaseApp?`: firebase.app.App }): *Promise*<User\>
56+
**preloadUser**(`options`: { `firebaseApp`: firebase.app.App }): *Promise*<User\>
5757

5858
#### Parameters:
5959

6060
| Name | Type |
6161
| :------ | :------ |
62-
| `options?` | *object* |
63-
| `options.firebaseApp?` | firebase.app.App |
62+
| `options` | *object* |
63+
| `options.firebaseApp` | firebase.app.App |
6464

6565
**Returns:** *Promise*<User\>
6666

67-
Defined in: [src/auth.tsx:8](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L8)
67+
Defined in: [src/auth.tsx:7](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L7)
6868

6969
___
7070

@@ -82,13 +82,13 @@ ___
8282

8383
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<firebase.auth.IdTokenResult\>
8484

85-
Defined in: [src/auth.tsx:45](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L45)
85+
Defined in: [src/auth.tsx:37](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L37)
8686

8787
___
8888

8989
### useUser
9090

91-
**useUser**<T\>(`auth?`: firebase.auth.Auth, `options?`: [*ReactFireOptions*](../interfaces/index.reactfireoptions.md)<T\>): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<firebase.User\>
91+
**useUser**<T\>(`options?`: [*ReactFireOptions*](../interfaces/index.reactfireoptions.md)<T\>): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<firebase.User\>
9292

9393
Subscribe to Firebase auth state changes, including token refresh
9494

@@ -100,11 +100,10 @@ Subscribe to Firebase auth state changes, including token refresh
100100

101101
#### Parameters:
102102

103-
| Name | Type | Description |
104-
| :------ | :------ | :------ |
105-
| `auth?` | firebase.auth.Auth | the [firebase.auth](https://firebase.google.com/docs/reference/js/firebase.auth) object |
106-
| `options?` | [*ReactFireOptions*](../interfaces/index.reactfireoptions.md)<T\> | |
103+
| Name | Type |
104+
| :------ | :------ |
105+
| `options?` | [*ReactFireOptions*](../interfaces/index.reactfireoptions.md)<T\> |
107106

108107
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<firebase.User\>
109108

110-
Defined in: [src/auth.tsx:24](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L24)
109+
Defined in: [src/auth.tsx:21](https://github.com/FirebaseExtended/reactfire/blob/main/src/auth.tsx#L21)

docs/reference/modules/firestore.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818

1919
### preloadFirestoreDoc
2020

21-
**preloadFirestoreDoc**(`refProvider`: (`firestore`: firebase.firestore.Firestore) => firebase.firestore.DocumentReference, `options?`: { `firebaseApp?`: firebase.app.App }): *Promise*<[*SuspenseSubject*](../classes/suspensesubject.suspensesubject-1.md)<DocumentSnapshot\>\>
21+
**preloadFirestoreDoc**(`refProvider`: (`firestore`: firebase.firestore.Firestore) => firebase.firestore.DocumentReference, `options`: { `firebaseApp`: firebase.app.App }): *Promise*<[*SuspenseSubject*](../classes/suspensesubject.suspensesubject-1.md)<DocumentSnapshot\>\>
2222

2323
#### Parameters:
2424

2525
| Name | Type |
2626
| :------ | :------ |
2727
| `refProvider` | (`firestore`: firebase.firestore.Firestore) => firebase.firestore.DocumentReference |
28-
| `options?` | *object* |
29-
| `options.firebaseApp?` | firebase.app.App |
28+
| `options` | *object* |
29+
| `options.firebaseApp` | firebase.app.App |
3030

3131
**Returns:** *Promise*<[*SuspenseSubject*](../classes/suspensesubject.suspensesubject-1.md)<DocumentSnapshot\>\>
3232

33-
Defined in: [src/firestore.tsx:29](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L29)
33+
Defined in: [src/firestore.tsx:28](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L28)
3434

3535
___
3636

@@ -55,7 +55,7 @@ Subscribe to a Firestore collection
5555

5656
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T *extends* {} ? T[] : firebase.firestore.QuerySnapshot\>
5757

58-
Defined in: [src/firestore.tsx:110](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L110)
58+
Defined in: [src/firestore.tsx:108](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L108)
5959

6060
___
6161

@@ -80,7 +80,7 @@ Subscribe to a Firestore collection and unwrap the snapshot.
8080

8181
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T[]\>
8282

83-
Defined in: [src/firestore.tsx:126](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L126)
83+
Defined in: [src/firestore.tsx:124](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L124)
8484

8585
___
8686

@@ -105,7 +105,7 @@ Suscribe to Firestore Document changes
105105

106106
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T *extends* {} ? T : firebase.firestore.DocumentSnapshot\>
107107

108-
Defined in: [src/firestore.tsx:48](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L48)
108+
Defined in: [src/firestore.tsx:46](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L46)
109109

110110
___
111111

@@ -130,7 +130,7 @@ Suscribe to Firestore Document changes
130130

131131
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T\>
132132

133-
Defined in: [src/firestore.tsx:80](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L80)
133+
Defined in: [src/firestore.tsx:78](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L78)
134134

135135
___
136136

@@ -155,7 +155,7 @@ Get a firestore document and don't subscribe to changes
155155

156156
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T\>
157157

158-
Defined in: [src/firestore.tsx:95](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L95)
158+
Defined in: [src/firestore.tsx:93](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L93)
159159

160160
___
161161

@@ -180,4 +180,4 @@ Get a firestore document and don't subscribe to changes
180180

181181
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T *extends* {} ? T : firebase.firestore.DocumentSnapshot\>
182182

183-
Defined in: [src/firestore.tsx:64](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L64)
183+
Defined in: [src/firestore.tsx:62](https://github.com/FirebaseExtended/reactfire/blob/main/src/firestore.tsx#L62)

docs/reference/modules/remote_config.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### useRemoteConfigAll
1818

19-
**useRemoteConfigAll**(`key`: *string*, `remoteConfig?`: RemoteConfig): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<[*AllParameters*](remote_config_getvalue.md#allparameters)\>
19+
**useRemoteConfigAll**(`key`: *string*): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<[*AllParameters*](remote_config_getvalue.md#allparameters)\>
2020

2121
Convience method similar to useRemoteConfigValue. Returns allRemote Config parameters.
2222

@@ -25,17 +25,16 @@ Convience method similar to useRemoteConfigValue. Returns allRemote Config param
2525
| Name | Type | Description |
2626
| :------ | :------ | :------ |
2727
| `key` | *string* | The parameter key in Remote Config |
28-
| `remoteConfig?` | RemoteConfig | Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
2928

3029
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<[*AllParameters*](remote_config_getvalue.md#allparameters)\>
3130

32-
Defined in: [src/remote-config/index.tsx:79](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L79)
31+
Defined in: [src/remote-config/index.tsx:78](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L78)
3332

3433
___
3534

3635
### useRemoteConfigBoolean
3736

38-
**useRemoteConfigBoolean**(`key`: *string*, `remoteConfig?`: RemoteConfig): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<boolean\>
37+
**useRemoteConfigBoolean**(`key`: *string*): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<boolean\>
3938

4039
Convience method similar to useRemoteConfigValue. Returns a `boolean` from a Remote Config parameter.
4140

@@ -44,17 +43,16 @@ Convience method similar to useRemoteConfigValue. Returns a `boolean` from a Rem
4443
| Name | Type | Description |
4544
| :------ | :------ | :------ |
4645
| `key` | *string* | The parameter key in Remote Config |
47-
| `remoteConfig?` | RemoteConfig | Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
4846

4947
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<boolean\>
5048

51-
Defined in: [src/remote-config/index.tsx:70](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L70)
49+
Defined in: [src/remote-config/index.tsx:69](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L69)
5250

5351
___
5452

5553
### useRemoteConfigNumber
5654

57-
**useRemoteConfigNumber**(`key`: *string*, `remoteConfig?`: RemoteConfig): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<number\>
55+
**useRemoteConfigNumber**(`key`: *string*): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<number\>
5856

5957
Convience method similar to useRemoteConfigValue. Returns a `number` from a Remote Config parameter.
6058

@@ -63,17 +61,16 @@ Convience method similar to useRemoteConfigValue. Returns a `number` from a Remo
6361
| Name | Type | Description |
6462
| :------ | :------ | :------ |
6563
| `key` | *string* | The parameter key in Remote Config |
66-
| `remoteConfig?` | RemoteConfig | Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
6764

6865
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<number\>
6966

70-
Defined in: [src/remote-config/index.tsx:61](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L61)
67+
Defined in: [src/remote-config/index.tsx:60](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L60)
7168

7269
___
7370

7471
### useRemoteConfigString
7572

76-
**useRemoteConfigString**(`key`: *string*, `remoteConfig?`: RemoteConfig): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<string\>
73+
**useRemoteConfigString**(`key`: *string*): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<string\>
7774

7875
Convience method similar to useRemoteConfigValue. Returns a `string` from a Remote Config parameter.
7976

@@ -82,17 +79,16 @@ Convience method similar to useRemoteConfigValue. Returns a `string` from a Remo
8279
| Name | Type | Description |
8380
| :------ | :------ | :------ |
8481
| `key` | *string* | The parameter key in Remote Config |
85-
| `remoteConfig?` | RemoteConfig | Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
8682

8783
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<string\>
8884

89-
Defined in: [src/remote-config/index.tsx:52](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L52)
85+
Defined in: [src/remote-config/index.tsx:51](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L51)
9086

9187
___
9288

9389
### useRemoteConfigValue
9490

95-
**useRemoteConfigValue**(`key`: *string*, `remoteConfig?`: RemoteConfig): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<RemoteConfigValue\>
91+
**useRemoteConfigValue**(`key`: *string*): [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<RemoteConfigValue\>
9692

9793
Accepts a key and optionally a Remote Config instance. Returns a
9894
Remote Config Value.
@@ -102,8 +98,7 @@ Remote Config Value.
10298
| Name | Type | Description |
10399
| :------ | :------ | :------ |
104100
| `key` | *string* | The parameter key in Remote Config |
105-
| `remoteConfig?` | RemoteConfig | Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
106101

107102
**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<RemoteConfigValue\>
108103

109-
Defined in: [src/remote-config/index.tsx:43](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L43)
104+
Defined in: [src/remote-config/index.tsx:42](https://github.com/FirebaseExtended/reactfire/blob/main/src/remote-config/index.tsx#L42)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"eslint": {
3636
"rules": {
37-
"react-hooks/rules-of-hooks": "warn"
37+
"react-hooks/rules-of-hooks": "error"
3838
}
3939
},
4040
"prettier": {

src/auth.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import * as React from 'react';
33
import { user } from 'rxfire/auth';
44
import { preloadAuth, preloadObservable, ReactFireOptions, useAuth, useObservable, ObservableStatus } from './';
55
import { from } from 'rxjs';
6-
import { useFirebaseApp } from './firebaseApp';
76

8-
export function preloadUser(options?: { firebaseApp?: firebase.app.App }) {
9-
// TODO: Find an alternative that doesn't break the rules of hooks (conditional hook call)
10-
const firebaseApp = options?.firebaseApp || useFirebaseApp();
7+
export function preloadUser(options: { firebaseApp: firebase.app.App }) {
8+
const firebaseApp = options.firebaseApp;
119

1210
return preloadAuth({ firebaseApp }).then(auth => {
1311
const result = preloadObservable(user(auth()), `auth:user:${firebaseApp.name}`);
@@ -18,16 +16,10 @@ export function preloadUser(options?: { firebaseApp?: firebase.app.App }) {
1816
/**
1917
* Subscribe to Firebase auth state changes, including token refresh
2018
*
21-
* @param auth - the [firebase.auth](https://firebase.google.com/docs/reference/js/firebase.auth) object
2219
* @param options
2320
*/
24-
export function useUser<T = unknown>(auth?: firebase.auth.Auth, options?: ReactFireOptions<T>): ObservableStatus<firebase.User> {
25-
// TODO: Find an alternative that doesn't break the rules of hooks (conditional hook call)
26-
auth = auth || useAuth();
27-
28-
if (!auth) {
29-
throw new Error('firebase.auth not found');
30-
}
21+
export function useUser<T = unknown>(options?: ReactFireOptions<T>): ObservableStatus<firebase.User> {
22+
const auth = useAuth();
3123

3224
const observableId = `auth:user:${auth.app.name}`;
3325
const observable$ = user(auth);
@@ -58,7 +50,6 @@ export function useIdTokenResult(
5850
}
5951

6052
export interface AuthCheckProps {
61-
auth?: firebase.auth.Auth;
6253
fallback: React.ReactNode;
6354
children: React.ReactNode;
6455
requiredClaims?: Object;
@@ -94,8 +85,8 @@ export function ClaimsCheck({ user, fallback, children, requiredClaims }: Claims
9485
}
9586
}
9687

97-
export function AuthCheck({ auth, fallback, children, requiredClaims }: AuthCheckProps): JSX.Element {
98-
const { data: user } = useUser<firebase.User>(auth);
88+
export function AuthCheck({ fallback, children, requiredClaims }: AuthCheckProps): JSX.Element {
89+
const { data: user } = useUser<firebase.User>();
9990

10091
if (user) {
10192
return requiredClaims ? (

0 commit comments

Comments
 (0)