UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

24 lines (21 loc) 651 B
import { useEffect } from 'react'; /** * Do a request on mount and url change only if there is no data yet * @warning Will still try to fetch is there is an error * @param query * @example You want to get some data once and share it in multiple components */ function useOnce(query, init) { const { fetcher, ready, data, fetchOrThrow: fetch } = query; useEffect(() => { if (!ready) return; if (fetcher == null) return; if (data != null) return; fetch(init).catch(console.warn); }, [ready, data, fetch]); } export { useOnce }; //# sourceMappingURL=use-once.mjs.map