react-query-firebase 1.2.3 → 1.3.0

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,48 @@
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
+ * A hook to get realtime updates to a firestore document.
28
+ *
29
+ * @group Hook
30
+ *
31
+ * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
32
+ *
33
+ * @returns {AppModelType | null}
34
+ *
35
+ * @example
36
+ * ```jsx
37
+ * const firebaseConfig = {};
38
+ * export const MyComponent = () => {
39
+ * const doc = useGetRealtimeDocData('collection/documentId');
40
+ * return (
41
+ * <div>
42
+ * {JSON.stringify(doc)}
43
+ * </div>
44
+ * );
45
+ * };
46
+ * ```
47
+ */
48
+ export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => AppModelType | null;
@@ -0,0 +1,41 @@
1
+ import { onSnapshot } from "firebase/firestore";
2
+ import { useEffect, 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 {AppModelType | null}
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * const firebaseConfig = {};
16
+ * export const MyComponent = () => {
17
+ * const doc = useGetRealtimeDocData('collection/documentId');
18
+ * return (
19
+ * <div>
20
+ * {JSON.stringify(doc)}
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
+ useEffect(() => {
30
+ const unsubscribe = ref
31
+ ? onSnapshot(ref, {
32
+ next: async (snapshot) => {
33
+ setDoc(snapshot.data() || null);
34
+ },
35
+ error: onError
36
+ })
37
+ : () => { };
38
+ return () => unsubscribe();
39
+ }, [ref, doc]);
40
+ return doc;
41
+ };
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.0"
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,76 @@
1
+ import { CollectionReference, DocumentData, DocumentReference, onSnapshot } from "firebase/firestore";
2
+
3
+ import { useEffect, 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
+ * A hook to get realtime updates to a firestore document.
33
+ *
34
+ * @group Hook
35
+ *
36
+ * @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
37
+ *
38
+ * @returns {AppModelType | null}
39
+ *
40
+ * @example
41
+ * ```jsx
42
+ * const firebaseConfig = {};
43
+ * export const MyComponent = () => {
44
+ * const doc = useGetRealtimeDocData('collection/documentId');
45
+ * return (
46
+ * <div>
47
+ * {JSON.stringify(doc)}
48
+ * </div>
49
+ * );
50
+ * };
51
+ * ```
52
+ */
53
+ export const useGetRealtimeDocData = <AppModelType, DbModelType extends DocumentData = DocumentData>({
54
+ path,
55
+ pathSegments,
56
+ reference,
57
+ onError
58
+ }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>): AppModelType | null => {
59
+ const ref = useDocReference({ path, reference, pathSegments });
60
+ const [doc, setDoc] = useState<AppModelType | null>(null);
61
+
62
+ useEffect(() => {
63
+ const unsubscribe = ref
64
+ ? onSnapshot(ref, {
65
+ next: async (snapshot) => {
66
+ setDoc(snapshot.data() || null);
67
+ },
68
+ error: onError
69
+ })
70
+ : () => {};
71
+
72
+ return () => unsubscribe();
73
+ }, [ref, doc]);
74
+
75
+ return doc;
76
+ };