react-firehooks
Version:
Lightweight dependency-free collection of React hooks for Firebase
25 lines (24 loc) • 1.49 kB
JavaScript
import { useCallback } from "react";
import { useMultiGet } from "../internal/useMultiGet.js";
import { getDocsFromSource, isQueryEqual } from "./internal.js";
/**
* Returns the data of multiple Firestore queries. Does not update the data once initially fetched
* @template AppModelTypes Tuple of shapes of the data after it was converted from firestore
* @template DbModelTypes Tuple of shapes of the data in firestore
* @param queries Firestore queries that will be fetched
* @param options Options to configure how the queries are fetched
* @returns Array with tuple for each query:
* - 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 useQueriesDataOnce(queries, 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 }));
}, [source, serverTimestamps]);
// @ts-expect-error `useMultiGet` assumes a single value type
return useMultiGet(queries, getData, isQueryEqual);
}