react-query-firebase 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -11,7 +11,7 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
11
11
|
/**
|
|
12
12
|
* A reference to a collection.
|
|
13
13
|
*/
|
|
14
|
-
reference
|
|
14
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
15
15
|
/**
|
|
16
16
|
* Additional path segments that will be applied relative
|
|
17
17
|
* to the first argument.
|
|
@@ -21,7 +21,16 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
21
21
|
* A callback to be called if the listen fails or is
|
|
22
22
|
* cancelled. No further callbacks will occur.
|
|
23
23
|
*/
|
|
24
|
-
onError
|
|
24
|
+
onError?: (error: FirebaseError) => unknown;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @inline
|
|
28
|
+
*/
|
|
29
|
+
export type UseGetRealtimeDocDataResult<AppModelType> = {
|
|
30
|
+
data: AppModelType | null;
|
|
31
|
+
isError: boolean;
|
|
32
|
+
error: FirebaseError | null;
|
|
33
|
+
isFetching: boolean;
|
|
25
34
|
};
|
|
26
35
|
/**
|
|
27
36
|
* A hook to get realtime updates to a firestore document.
|
|
@@ -30,19 +39,19 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
30
39
|
*
|
|
31
40
|
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
32
41
|
*
|
|
33
|
-
* @returns {AppModelType
|
|
42
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
34
43
|
*
|
|
35
44
|
* @example
|
|
36
45
|
* ```jsx
|
|
37
46
|
* const firebaseConfig = {};
|
|
38
47
|
* export const MyComponent = () => {
|
|
39
|
-
* const
|
|
48
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
40
49
|
* return (
|
|
41
50
|
* <div>
|
|
42
|
-
* {JSON.stringify(
|
|
51
|
+
* {JSON.stringify(result)}
|
|
43
52
|
* </div>
|
|
44
53
|
* );
|
|
45
54
|
* };
|
|
46
55
|
* ```
|
|
47
56
|
*/
|
|
48
|
-
export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => AppModelType
|
|
57
|
+
export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => UseGetRealtimeDocDataResult<AppModelType>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { onSnapshot } from "firebase/firestore";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
|
3
3
|
import { useDocReference } from "./useDocReference";
|
|
4
4
|
/**
|
|
5
5
|
* A hook to get realtime updates to a firestore document.
|
|
@@ -8,16 +8,16 @@ import { useDocReference } from "./useDocReference";
|
|
|
8
8
|
*
|
|
9
9
|
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
10
10
|
*
|
|
11
|
-
* @returns {AppModelType
|
|
11
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```jsx
|
|
15
15
|
* const firebaseConfig = {};
|
|
16
16
|
* export const MyComponent = () => {
|
|
17
|
-
* const
|
|
17
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
18
18
|
* return (
|
|
19
19
|
* <div>
|
|
20
|
-
* {JSON.stringify(
|
|
20
|
+
* {JSON.stringify(result)}
|
|
21
21
|
* </div>
|
|
22
22
|
* );
|
|
23
23
|
* };
|
|
@@ -26,16 +26,31 @@ import { useDocReference } from "./useDocReference";
|
|
|
26
26
|
export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }) => {
|
|
27
27
|
const ref = useDocReference({ path, reference, pathSegments });
|
|
28
28
|
const [doc, setDoc] = useState(null);
|
|
29
|
+
const [isError, setIsError] = useState(false);
|
|
30
|
+
const [isFetching, setIsFetching] = useState(true);
|
|
31
|
+
const [error, setError] = useState(null);
|
|
29
32
|
useEffect(() => {
|
|
30
33
|
const unsubscribe = ref
|
|
31
34
|
? onSnapshot(ref, {
|
|
32
35
|
next: async (snapshot) => {
|
|
36
|
+
setIsFetching(false);
|
|
33
37
|
setDoc(snapshot.data() || null);
|
|
38
|
+
setError(null);
|
|
39
|
+
setIsError(false);
|
|
34
40
|
},
|
|
35
|
-
error:
|
|
41
|
+
error: (e) => {
|
|
42
|
+
setIsError(true);
|
|
43
|
+
setError(e);
|
|
44
|
+
onError?.(e);
|
|
45
|
+
}
|
|
36
46
|
})
|
|
37
47
|
: () => { };
|
|
38
48
|
return () => unsubscribe();
|
|
39
|
-
}, [ref, doc]);
|
|
40
|
-
return
|
|
49
|
+
}, [ref, doc, isError, onError, isFetching, error]);
|
|
50
|
+
return useMemo(() => ({
|
|
51
|
+
data: doc,
|
|
52
|
+
isError,
|
|
53
|
+
isFetching,
|
|
54
|
+
error
|
|
55
|
+
}), [doc, isError, error]);
|
|
41
56
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CollectionReference, DocumentData, DocumentReference, onSnapshot } from "firebase/firestore";
|
|
2
2
|
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
3
|
+
import { useEffect, useMemo, useState } from "react";
|
|
4
4
|
import { FirebaseError } from "firebase/app";
|
|
5
5
|
import { useDocReference } from "./useDocReference";
|
|
6
6
|
|
|
@@ -15,7 +15,7 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
15
15
|
/**
|
|
16
16
|
* A reference to a collection.
|
|
17
17
|
*/
|
|
18
|
-
reference
|
|
18
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
19
19
|
/**
|
|
20
20
|
* Additional path segments that will be applied relative
|
|
21
21
|
* to the first argument.
|
|
@@ -25,7 +25,17 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
25
25
|
* A callback to be called if the listen fails or is
|
|
26
26
|
* cancelled. No further callbacks will occur.
|
|
27
27
|
*/
|
|
28
|
-
onError
|
|
28
|
+
onError?: (error: FirebaseError) => unknown;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @inline
|
|
33
|
+
*/
|
|
34
|
+
export type UseGetRealtimeDocDataResult<AppModelType> = {
|
|
35
|
+
data: AppModelType | null;
|
|
36
|
+
isError: boolean;
|
|
37
|
+
error: FirebaseError | null;
|
|
38
|
+
isFetching: boolean;
|
|
29
39
|
};
|
|
30
40
|
|
|
31
41
|
/**
|
|
@@ -35,16 +45,16 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
35
45
|
*
|
|
36
46
|
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
37
47
|
*
|
|
38
|
-
* @returns {AppModelType
|
|
48
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
39
49
|
*
|
|
40
50
|
* @example
|
|
41
51
|
* ```jsx
|
|
42
52
|
* const firebaseConfig = {};
|
|
43
53
|
* export const MyComponent = () => {
|
|
44
|
-
* const
|
|
54
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
45
55
|
* return (
|
|
46
56
|
* <div>
|
|
47
|
-
* {JSON.stringify(
|
|
57
|
+
* {JSON.stringify(result)}
|
|
48
58
|
* </div>
|
|
49
59
|
* );
|
|
50
60
|
* };
|
|
@@ -55,22 +65,40 @@ export const useGetRealtimeDocData = <AppModelType, DbModelType extends Document
|
|
|
55
65
|
pathSegments,
|
|
56
66
|
reference,
|
|
57
67
|
onError
|
|
58
|
-
}: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): AppModelType
|
|
68
|
+
}: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): UseGetRealtimeDocDataResult<AppModelType> => {
|
|
59
69
|
const ref = useDocReference({ path, reference, pathSegments });
|
|
60
70
|
const [doc, setDoc] = useState<AppModelType | null>(null);
|
|
71
|
+
const [isError, setIsError] = useState(false);
|
|
72
|
+
const [isFetching, setIsFetching] = useState(true);
|
|
73
|
+
const [error, setError] = useState<FirebaseError | null>(null);
|
|
61
74
|
|
|
62
75
|
useEffect(() => {
|
|
63
76
|
const unsubscribe = ref
|
|
64
77
|
? onSnapshot(ref, {
|
|
65
78
|
next: async (snapshot) => {
|
|
79
|
+
setIsFetching(false);
|
|
66
80
|
setDoc(snapshot.data() || null);
|
|
81
|
+
setError(null);
|
|
82
|
+
setIsError(false);
|
|
67
83
|
},
|
|
68
|
-
error:
|
|
84
|
+
error: (e) => {
|
|
85
|
+
setIsError(true);
|
|
86
|
+
setError(e);
|
|
87
|
+
onError?.(e);
|
|
88
|
+
}
|
|
69
89
|
})
|
|
70
90
|
: () => {};
|
|
71
91
|
|
|
72
92
|
return () => unsubscribe();
|
|
73
|
-
}, [ref, doc]);
|
|
93
|
+
}, [ref, doc, isError, onError, isFetching, error]);
|
|
74
94
|
|
|
75
|
-
return
|
|
95
|
+
return useMemo(
|
|
96
|
+
() => ({
|
|
97
|
+
data: doc,
|
|
98
|
+
isError,
|
|
99
|
+
isFetching,
|
|
100
|
+
error
|
|
101
|
+
}),
|
|
102
|
+
[doc, isError, error]
|
|
103
|
+
);
|
|
76
104
|
};
|