ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
36 lines • 1.83 kB
JavaScript
import * as React from 'react';
import { useMemo, useRef } from 'react';
import debounce from 'lodash/debounce.js';
import { useEvent } from "./useEvent.js";
// allow the hook to work in SSR
const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
/**
* Hook somewhat equivalent to useEvent, but with a debounce
* Returns a debounced callback which will not change across re-renders unless the
* callback or delay changes
* @see https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
* @see https://github.com/facebook/react/issues/14099#issuecomment-440013892
*/
export const useDebouncedEvent = (callback, delay) => {
// Create a ref that stores the debounced callback
const debouncedCallbackRef = useRef(Object.assign(() => {
throw new Error('Cannot call an event handler while rendering.');
}, { cancel: () => { }, flush: () => undefined }));
// Keep a stable ref to the callback (in case it's an inline function for instance)
const stableCallback = useEvent(callback);
// Whenever callback or delay changes, we need to update the debounced callback
useLayoutEffect(() => {
const debounced = debounce(stableCallback, delay);
debouncedCallbackRef.current = debounced;
return () => {
debounced.flush();
};
}, [stableCallback, delay]);
// The function returned by useMemo will invoke the debounced callback
// Its dependencies array is empty, so it never changes across re-renders
return useMemo(() => Object.assign((...args) => debouncedCallbackRef.current(...args), {
cancel: () => debouncedCallbackRef.current.cancel(),
flush: () => debouncedCallbackRef.current.flush(),
}), []);
};
//# sourceMappingURL=useDebouncedEvent.js.map