( docRef?: DocumentReference<T> | null, options?: Options )
| 27 | } from './types'; |
| 28 | |
| 29 | export 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 | |
| 61 | export const useDocumentOnce = <T = DocumentData>( |
| 62 | docRef?: DocumentReference<T> | null, |
no test coverage detected