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