Skip to content

Commit 4cb719f

Browse files
authored
Merge pull request #55 from Nickman87/loasing-state-empty-query
Preserve loading state
2 parents bb0f815 + 50d79fe commit 4cb719f

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

firestore/useCollection.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,24 @@ export const useCollection = (
1818
>();
1919
const ref = useIsEqualRef(query, reset);
2020

21-
useEffect(
22-
() => {
23-
if (!ref.current) {
24-
setValue(undefined);
25-
return;
26-
}
27-
const listener =
28-
options && options.snapshotListenOptions
29-
? ref.current.onSnapshot(
30-
options.snapshotListenOptions,
31-
setValue,
32-
setError
33-
)
34-
: ref.current.onSnapshot(setValue, setError);
21+
useEffect(() => {
22+
if (!ref.current) {
23+
reset();
24+
return;
25+
}
26+
const listener =
27+
options && options.snapshotListenOptions
28+
? ref.current.onSnapshot(
29+
options.snapshotListenOptions,
30+
setValue,
31+
setError
32+
)
33+
: ref.current.onSnapshot(setValue, setError);
3534

36-
return () => {
37-
listener();
38-
};
39-
},
40-
[ref.current]
41-
);
35+
return () => {
36+
listener();
37+
};
38+
}, [ref.current]);
4239

4340
return [value, loading, error];
4441
};

firestore/useCollectionOnce.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ export const useCollectionOnce = (
1818
>();
1919
const ref = useIsEqualRef(query, reset);
2020

21-
useEffect(
22-
() => {
23-
if (!ref.current) {
24-
setValue(undefined);
25-
return;
26-
}
27-
ref.current
28-
.get(options ? options.getOptions : undefined)
29-
.then(setValue)
30-
.catch(setError);
31-
},
32-
[ref.current]
33-
);
21+
useEffect(() => {
22+
if (!ref.current) {
23+
reset();
24+
return;
25+
}
26+
ref.current
27+
.get(options ? options.getOptions : undefined)
28+
.then(setValue)
29+
.catch(setError);
30+
}, [ref.current]);
3431

3532
return [value, loading, error];
3633
};

firestore/useDocument.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,24 @@ export const useDocument = (
1818
>();
1919
const ref = useIsEqualRef(docRef, reset);
2020

21-
useEffect(
22-
() => {
23-
if (!ref.current) {
24-
setValue(undefined);
25-
return;
26-
}
27-
const listener =
28-
options && options.snapshotListenOptions
29-
? ref.current.onSnapshot(
30-
options.snapshotListenOptions,
31-
setValue,
32-
setError
33-
)
34-
: ref.current.onSnapshot(setValue, setError);
21+
useEffect(() => {
22+
if (!ref.current) {
23+
reset();
24+
return;
25+
}
26+
const listener =
27+
options && options.snapshotListenOptions
28+
? ref.current.onSnapshot(
29+
options.snapshotListenOptions,
30+
setValue,
31+
setError
32+
)
33+
: ref.current.onSnapshot(setValue, setError);
3534

36-
return () => {
37-
listener();
38-
};
39-
},
40-
[ref.current]
41-
);
35+
return () => {
36+
listener();
37+
};
38+
}, [ref.current]);
4239

4340
return [value, loading, error];
4441
};

firestore/useDocumentOnce.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ export const useDocumentOnce = (
1818
>();
1919
const ref = useIsEqualRef(docRef, reset);
2020

21-
useEffect(
22-
() => {
23-
if (!ref.current) {
24-
setValue(undefined);
25-
return;
26-
}
27-
ref.current
28-
.get(options ? options.getOptions : undefined)
29-
.then(setValue)
30-
.catch(setError);
31-
},
32-
[ref.current]
33-
);
21+
useEffect(() => {
22+
if (!ref.current) {
23+
reset();
24+
return;
25+
}
26+
ref.current
27+
.get(options ? options.getOptions : undefined)
28+
.then(setValue)
29+
.catch(setError);
30+
}, [ref.current]);
3431

3532
return [value, loading, error];
3633
};

0 commit comments

Comments
 (0)