MCPcopy Index your code
hub / github.com/CSFrequency/react-firebase-hooks / useDocument

Function useDocument

firestore/useDocument.ts:29–59  ·  view source on GitHub ↗
(
  docRef?: DocumentReference<T> | null,
  options?: Options
)

Source from the content-addressed store, hash-verified

27} from './types';
28
29export const useDocument = <T = DocumentData>(
30 docRef?: DocumentReference<T> | null,
31 options?: Options
32): DocumentHook<T> => {
33 const { error, loading, reset, setError, setValue, value } = useLoadingValue<
34 DocumentSnapshot<T>,
35 FirestoreError
36 >();
37 const ref = useIsFirestoreRefEqual<DocumentReference<T>>(docRef, reset);
38
39 useEffect(() => {
40 if (!ref.current) {
41 setValue(undefined);
42 return;
43 }
44 const unsubscribe = options?.snapshotListenOptions
45 ? onSnapshot(
46 ref.current,
47 options.snapshotListenOptions,
48 setValue,
49 setError
50 )
51 : onSnapshot(ref.current, setValue, setError);
52
53 return () => {
54 unsubscribe();
55 };
56 }, [ref.current]);
57
58 return [value as DocumentSnapshot<T>, loading, error];
59};
60
61export const useDocumentOnce = <T = DocumentData>(
62 docRef?: DocumentReference<T> | null,

Callers 2

useDocumentDataFunction · 0.85

Calls 2

useIsFirestoreRefEqualFunction · 0.90
setValueFunction · 0.85

Tested by

no test coverage detected