Skip to content

Commit 2e22a4c

Browse files
committed
Ensure loading flag is set when reset #6
1 parent e39fc0b commit 2e22a4c

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

auth/useAuthState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export type AuthStateHook = {
88
};
99

1010
export default (auth: auth.Auth): AuthStateHook => {
11-
const { loading, setValue, value } = useLoadingValue<User>(auth.currentUser);
11+
const { loading, setValue, value } = useLoadingValue<User>(
12+
() => auth.currentUser
13+
);
1214

1315
useEffect(
1416
() => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-firebase-hooks",
3-
"version": "1.0.0",
3+
"version": "1.1.0-rc.1",
44
"description": "React Hooks for Firebase",
55
"author": "CS Frequency Limited (https://csfrequency.com)",
66
"license": "Apache-2.0",
@@ -45,6 +45,7 @@
4545
"module": "dist/index.esm.js",
4646
"dependencies": {},
4747
"devDependencies": {
48+
"@types/react": "^16.8.3",
4849
"firebase": "^5.5.6",
4950
"path": "^0.12.7",
5051
"react": "^16.8.0",

util/useLoadingValue.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ type ResetAction = { type: 'reset'; defaultValue?: any };
2020
type ValueAction = { type: 'value'; value: any };
2121
type ReducerAction = ErrorAction | ResetAction | ValueAction;
2222

23+
const defaultState = (defaultValue?: any) => {
24+
return {
25+
loading: defaultValue === undefined || defaultValue === null,
26+
value: defaultValue,
27+
};
28+
};
29+
2330
const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
2431
switch (action.type) {
2532
case 'error':
@@ -29,10 +36,7 @@ const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
2936
loading: false,
3037
};
3138
case 'reset':
32-
return {
33-
...state,
34-
value: action.defaultValue,
35-
};
39+
return defaultState(action.defaultValue);
3640
case 'value':
3741
return {
3842
...state,
@@ -44,13 +48,12 @@ const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
4448
}
4549
};
4650

47-
export default <T>(defaultValue?: T | null): LoadingValue<T> => {
48-
const [state, dispatch] = useReducer(reducer, {
49-
loading: defaultValue === undefined || defaultValue === null,
50-
value: defaultValue,
51-
});
51+
export default <T>(getDefaultValue?: () => T | null): LoadingValue<T> => {
52+
const defaultValue = getDefaultValue ? getDefaultValue() : undefined;
53+
const [state, dispatch] = useReducer(reducer, defaultState(defaultValue));
5254

5355
const reset = () => {
56+
const defaultValue = getDefaultValue ? getDefaultValue() : undefined;
5457
dispatch({ type: 'reset', defaultValue });
5558
};
5659

yarn.lock

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@
126126
version "0.0.38"
127127
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.38.tgz#c1be40aa933723c608820a99a373a16d215a1ca2"
128128

129+
"@types/prop-types@*":
130+
version "15.5.9"
131+
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.9.tgz#f2d14df87b0739041bc53a7d75e3d77d726a3ec0"
132+
integrity sha512-Nha5b+jmBI271jdTMwrHiNXM+DvThjHOfyZtMX9kj/c/LUj2xiLHsG/1L3tJ8DjAoQN48cHwUwtqBotjyXaSdQ==
133+
134+
"@types/react@^16.8.3":
135+
version "16.8.3"
136+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.8.3.tgz#7b67956f682bea30a5a09b3242c0784ff196c848"
137+
integrity sha512-PjPocAxL9SNLjYMP4dfOShW/rj9FDBJGu3JFRt0zEYf77xfihB6fq8zfDpMrV6s82KnAi7F1OEe5OsQX25Ybdw==
138+
dependencies:
139+
"@types/prop-types" "*"
140+
csstype "^2.2.0"
141+
129142
abbrev@1:
130143
version "1.1.1"
131144
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
@@ -257,6 +270,11 @@ core-util-is@~1.0.0:
257270
version "1.0.2"
258271
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
259272

273+
csstype@^2.2.0:
274+
version "2.6.2"
275+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.2.tgz#3043d5e065454579afc7478a18de41909c8a2f01"
276+
integrity sha512-Rl7PvTae0pflc1YtxtKbiSqq20Ts6vpIYOD5WBafl4y123DyHUeLrRdQP66sQW8/6gmX8jrYJLXwNeMqYVJcow==
277+
260278
date-time@^2.1.0:
261279
version "2.1.0"
262280
resolved "https://registry.yarnpkg.com/date-time/-/date-time-2.1.0.tgz#0286d1b4c769633b3ca13e1e62558d2dbdc2eba2"
@@ -873,14 +891,15 @@ rc@^1.2.7:
873891
minimist "^1.2.0"
874892
strip-json-comments "~2.0.1"
875893

876-
react@^16.7.0-alpha.0:
877-
version "16.7.0-alpha.0"
878-
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.0.tgz#e2ed4abe6f268c9b092a1d1e572953684d1783a9"
894+
react@^16.8.0:
895+
version "16.8.1"
896+
resolved "https://registry.yarnpkg.com/react/-/react-16.8.1.tgz#ae11831f6cb2a05d58603a976afc8a558e852c4a"
897+
integrity sha512-wLw5CFGPdo7p/AgteFz7GblI2JPOos0+biSoxf1FPsGxWQZdN/pj6oToJs1crn61DL3Ln7mN86uZ4j74p31ELQ==
879898
dependencies:
880899
loose-envify "^1.1.0"
881900
object-assign "^4.1.1"
882901
prop-types "^15.6.2"
883-
scheduler "^0.11.0-alpha.0"
902+
scheduler "^0.13.1"
884903

885904
readable-stream@^2.0.6:
886905
version "2.3.6"
@@ -1002,9 +1021,10 @@ sax@^1.2.4:
10021021
version "1.2.4"
10031022
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
10041023

1005-
scheduler@^0.11.0-alpha.0:
1006-
version "0.11.0-alpha.0"
1007-
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.11.0-alpha.0.tgz#7b132c726608993471db07866f2d59a52b9e190b"
1024+
scheduler@^0.13.1:
1025+
version "0.13.1"
1026+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.13.1.tgz#1a217df1bfaabaf4f1b92a9127d5d732d85a9591"
1027+
integrity sha512-VJKOkiKIN2/6NOoexuypwSrybx13MY7NSy9RNt8wPvZDMRT1CW6qlpF5jXRToXNHz3uWzbm2elNpZfXfGPqP9A==
10081028
dependencies:
10091029
loose-envify "^1.1.0"
10101030
object-assign "^4.1.1"

0 commit comments

Comments
 (0)