script.jsimport {useState, useEffect, useCallback} from '../preact@10.5.13/hooks.js';
export function useLoader(_callback, {delay}, deps = []) {
const [loading, setLoading] = useState(false);
const callback = useCallback(_callback, deps); // これを入れないと、callbackが何度もrenderingされてしまう
useEffect(() => {
(async () => {
const timer = setTimeout(() => setLoading(true), delay);
await callback();
clearTimeout(timer);
setLoading(false);
})();
}, [callback, delay, ...deps]);
return {loading};
}