UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

40 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useIsDataLoaded = void 0; const react_1 = require("react"); const react_query_1 = require("@tanstack/react-query"); /** * Check if react-query has already fetched data for a query key. * * This hook is reactive. * * @example * const isCustomerLoaded = useIsDataLoaded(['customers', 'getOne', { id: customerId }]); * * @returns {boolean} true if the data is loaded, false otherwise */ const useIsDataLoaded = (queryKey, options = {}) => { const { enabled = true } = options; const queryClient = (0, react_query_1.useQueryClient)(); const [isDataLoaded, setDataLoaded] = (0, react_1.useState)(() => { if (!enabled) { return false; } return queryClient.getQueryData(queryKey) !== undefined; }); (0, react_1.useEffect)(() => { if (!enabled) return; if (queryClient.getQueryData(queryKey) === undefined) { const observer = new react_query_1.QueryObserver(queryClient, { queryKey }); const unsubscribe = observer.subscribe(result => { setDataLoaded(!result.isPending); unsubscribe(); }); return unsubscribe; } }, [enabled, queryKey, queryClient]); return isDataLoaded; }; exports.useIsDataLoaded = useIsDataLoaded; //# sourceMappingURL=useIsDataLoaded.js.map