react-query-firebase 1.2.3 → 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.
@@ -13,3 +13,4 @@ export * from "./useQuery";
13
13
  export * from "./useRunTransaction";
14
14
  export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
+ export * from "./useGetRealtimeDocData";
@@ -13,3 +13,4 @@ export * from "./useQuery";
13
13
  export * from "./useRunTransaction";
14
14
  export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
+ export * from "./useGetRealtimeDocData";
@@ -0,0 +1,57 @@
1
+ import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
+ import { FirebaseError } from "firebase/app";
3
+ /**
4
+ * @inline
5
+ */
6
+ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
+ /**
8
+ * A slash-separated path to a document. Has to be omitted to use
9
+ */
10
+ path?: string;
11
+ /**
12
+ * A reference to a collection.
13
+ */
14
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
15
+ /**
16
+ * Additional path segments that will be applied relative
17
+ * to the first argument.
18
+ */
19
+ pathSegments?: string[];
20
+ /**
21
+ * A callback to be called if the listen fails or is
22
+ * cancelled. No further callbacks will occur.
23
+ */
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;
34
+ };
35
+ /**
36
+ * A hook to get realtime updates to a firestore document.
37
+ *
38
+ * @group Hook
39
+ *
40
+ * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
41
+ *
42
+ * @returns {UseGetRealtimeDocDataResult<AppModelType>}
43
+ *
44
+ * @example
45
+ * ```jsx
46
+ * const firebaseConfig = {};
47
+ * export const MyComponent = () => {
48
+ * const result = useGetRealtimeDocData('collection/documentId');
49
+ * return (
50
+ * <div>
51
+ * {JSON.stringify(result)}
52
+ * </div>
53
+ * );
54
+ * };
55
+ * ```
56
+ */
57
+ export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => UseGetRealtimeDocDataResult<AppModelType>;
@@ -0,0 +1,56 @@
1
+ import { onSnapshot } from "firebase/firestore";
2
+ import { useEffect, useMemo, useState } from "react";
3
+ import { useDocReference } from "./useDocReference";
4
+ /**
5
+ * A hook to get realtime updates to a firestore document.
6
+ *
7
+ * @group Hook
8
+ *
9
+ * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
10
+ *
11
+ * @returns {UseGetRealtimeDocDataResult<AppModelType>}
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * const firebaseConfig = {};
16
+ * export const MyComponent = () => {
17
+ * const result = useGetRealtimeDocData('collection/documentId');
18
+ * return (
19
+ * <div>
20
+ * {JSON.stringify(result)}
21
+ * </div>
22
+ * );
23
+ * };
24
+ * ```
25
+ */
26
+ export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }) => {
27
+ const ref = useDocReference({ path, reference, pathSegments });
28
+ const [doc, setDoc] = useState(null);
29
+ const [isError, setIsError] = useState(false);
30
+ const [isFetching, setIsFetching] = useState(true);
31
+ const [error, setError] = useState(null);
32
+ useEffect(() => {
33
+ const unsubscribe = ref
34
+ ? onSnapshot(ref, {
35
+ next: async (snapshot) => {
36
+ setIsFetching(false);
37
+ setDoc(snapshot.data() || null);
38
+ setError(null);
39
+ setIsError(false);
40
+ },
41
+ error: (e) => {
42
+ setIsError(true);
43
+ setError(e);
44
+ onError?.(e);
45
+ }
46
+ })
47
+ : () => { };
48
+ return () => unsubscribe();
49
+ }, [ref, doc, isError, onError, isFetching, error]);
50
+ return useMemo(() => ({
51
+ data: doc,
52
+ isError,
53
+ isFetching,
54
+ error
55
+ }), [doc, isError, error]);
56
+ };
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "docs:build": "vitepress build docs",
61
61
  "docs:preview": "vitepress preview docs"
62
62
  },
63
- "version": "1.2.3"
63
+ "version": "1.3.1"
64
64
  }
@@ -13,3 +13,4 @@ export * from "./useQuery";
13
13
  export * from "./useRunTransaction";
14
14
  export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
+ export * from "./useGetRealtimeDocData";
@@ -0,0 +1,104 @@
1
+ import { CollectionReference, DocumentData, DocumentReference, onSnapshot } from "firebase/firestore";
2
+
3
+ import { useEffect, useMemo, useState } from "react";
4
+ import { FirebaseError } from "firebase/app";
5
+ import { useDocReference } from "./useDocReference";
6
+
7
+ /**
8
+ * @inline
9
+ */
10
+ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
11
+ /**
12
+ * A slash-separated path to a document. Has to be omitted to use
13
+ */
14
+ path?: string;
15
+ /**
16
+ * A reference to a collection.
17
+ */
18
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
19
+ /**
20
+ * Additional path segments that will be applied relative
21
+ * to the first argument.
22
+ */
23
+ pathSegments?: string[];
24
+ /**
25
+ * A callback to be called if the listen fails or is
26
+ * cancelled. No further callbacks will occur.
27
+ */
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;
39
+ };
40
+
41
+ /**
42
+ * A hook to get realtime updates to a firestore document.
43
+ *
44
+ * @group Hook
45
+ *
46
+ * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
47
+ *
48
+ * @returns {UseGetRealtimeDocDataResult<AppModelType>}
49
+ *
50
+ * @example
51
+ * ```jsx
52
+ * const firebaseConfig = {};
53
+ * export const MyComponent = () => {
54
+ * const result = useGetRealtimeDocData('collection/documentId');
55
+ * return (
56
+ * <div>
57
+ * {JSON.stringify(result)}
58
+ * </div>
59
+ * );
60
+ * };
61
+ * ```
62
+ */
63
+ export const useGetRealtimeDocData = <AppModelType, DbModelType extends DocumentData = DocumentData>({
64
+ path,
65
+ pathSegments,
66
+ reference,
67
+ onError
68
+ }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): UseGetRealtimeDocDataResult<AppModelType> => {
69
+ const ref = useDocReference({ path, reference, pathSegments });
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);
74
+
75
+ useEffect(() => {
76
+ const unsubscribe = ref
77
+ ? onSnapshot(ref, {
78
+ next: async (snapshot) => {
79
+ setIsFetching(false);
80
+ setDoc(snapshot.data() || null);
81
+ setError(null);
82
+ setIsError(false);
83
+ },
84
+ error: (e) => {
85
+ setIsError(true);
86
+ setError(e);
87
+ onError?.(e);
88
+ }
89
+ })
90
+ : () => {};
91
+
92
+ return () => unsubscribe();
93
+ }, [ref, doc, isError, onError, isFetching, error]);
94
+
95
+ return useMemo(
96
+ () => ({
97
+ data: doc,
98
+ isError,
99
+ isFetching,
100
+ error
101
+ }),
102
+ [doc, isError, error]
103
+ );
104
+ };