react-firehooks
Version:
Lightweight dependency-free collection of React hooks for Firebase
26 lines (25 loc) • 1.4 kB
JavaScript
import { get } from "firebase/database";
import { useCallback } from "react";
import { useGet } from "../internal/useGet.js";
import { defaultConverter, isQueryEqual } from "./internal.js";
/**
* Returns the DataSnapshot of the Realtime Database query. Does not update the DataSnapshot once initially fetched
* @template Value Type of the object value
* @param query Realtime Database query
* @param options Options to configure how the object is fetched
* `converter`: Function to extract the desired data from the DataSnapshot. Similar to Firestore converters. Default: `snap.val()`.
* @returns User, loading state, and error
* - value: Object value; `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 useObjectValueOnce(query, options) {
const { converter = defaultConverter } = options !== null && options !== void 0 ? options : {};
const getData = useCallback(async (stableQuery) => {
const snap = await get(stableQuery);
return converter(snap);
// TODO: add options as dependency
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return useGet(query !== null && query !== void 0 ? query : undefined, getData, isQueryEqual);
}