UNPKG

react-firehooks

Version:

Lightweight dependency-free collection of React hooks for Firebase

29 lines (28 loc) 1.5 kB
import { onSnapshot } from "firebase/firestore"; import { useCallback } from "react"; import { useMultiListen } from "../internal/useMultiListen.js"; import { isQueryEqual } from "./internal.js"; /** * Returns and updates a QuerySnapshot of multiple Firestore queries * @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 subscribed to * @param options Options to configure the subscription * @returns Array with tuple for each query: * - 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 useQueries(queries, options) { const { snapshotListenOptions } = options !== null && options !== void 0 ? options : {}; const { includeMetadataChanges = false, source = "default" } = (snapshotListenOptions !== null && snapshotListenOptions !== void 0 ? snapshotListenOptions : {}); const onChange = useCallback((query, next, error) => onSnapshot(query, { includeMetadataChanges, source, }, { next, error, }), [includeMetadataChanges, source]); // @ts-expect-error `useMultiListen` assumes a single value type return useMultiListen(queries, onChange, isQueryEqual); }