react-firehooks
Version:
Lightweight dependency-free collection of React hooks for Firebase
20 lines (19 loc) • 1.17 kB
JavaScript
import { useCallback } from "react";
import { useGet } from "../internal/useGet.js";
import { getDocsFromSource, isQueryEqual } from "./internal.js";
/**
* Returns the QuerySnapshot of a Firestore query. Does not update the QuerySnapshot 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 QuerySnapshot, loading state, and error
* - value: QuerySnapshot; `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 useQueryOnce(query, options) {
const { source = "default" } = options !== null && options !== void 0 ? options : {};
const getData = useCallback(async (stableQuery) => getDocsFromSource(stableQuery, source), [source]);
return useGet(query !== null && query !== void 0 ? query : undefined, getData, isQueryEqual);
}