react-firehooks
Version:
Lightweight dependency-free collection of React hooks for Firebase
20 lines (19 loc) • 1.25 kB
JavaScript
import { useCallback } from "react";
import { useGet } from "../internal/useGet.js";
import { getDocFromSource, isDocRefEqual } from "./internal.js";
/**
* Returns the DocumentSnapshot of a Firestore DocumentReference. Does not update the DocumentSnapshot once initially fetched
* @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 fetched
* @param options Options to configure how the document is fetched
* @returns DocumentSnapshot, loading state, and error
* - value: DocumentSnapshot; `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 useDocumentOnce(reference, options) {
const { source = "default" } = options !== null && options !== void 0 ? options : {};
const getData = useCallback((stableRef) => getDocFromSource(stableRef, source), [source]);
return useGet(reference !== null && reference !== void 0 ? reference : undefined, getData, isDocRefEqual);
}