UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

26 lines (22 loc) 674 B
'use strict'; var react = require('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; react.useEffect(() => { if (!ready) return; if (fetcher == null) return; if (data != null) return; fetch(init).catch(console.warn); }, [ready, data, fetch]); } exports.useOnce = useOnce; //# sourceMappingURL=use-once.cjs.map