react-firehooks
Version:
Lightweight dependency-free collection of React hooks for Firebase
24 lines (23 loc) • 1.43 kB
JavaScript
import { useCallback } from "react";
import { useGet } from "../internal/useGet.js";
import { getDocFromSource, isDocRefEqual } from "./internal.js";
/**
* Returns the data of a Firestore DocumentReference
* @template AppModelType Shape of the data after it was converted from firestore
* @template DbModelType Shape of the data in firestore
* @param reference Firestore DocumentReference that will be subscribed to
* @param options Options to configure how the document is fetched
* @returns Document data, loading state, and error
* - value: Document data; `undefined` if document does not exist, is currently being fetched, or an error occurred
* - loading: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
* - error: `undefined` if no error occurred
*/
export function useDocumentDataOnce(reference, options) {
const { source = "default", snapshotOptions } = options !== null && options !== void 0 ? options : {};
const { serverTimestamps = "none" } = snapshotOptions !== null && snapshotOptions !== void 0 ? snapshotOptions : {};
const getData = useCallback(async (stableRef) => {
const snap = await getDocFromSource(stableRef, source);
return snap.data({ serverTimestamps });
}, [serverTimestamps, source]);
return useGet(reference !== null && reference !== void 0 ? reference : undefined, getData, isDocRefEqual);
}