HEX
Server: Apache/2
System: Linux server1c 2.6.32-042stab145.3 #1 SMP Thu Jun 11 14:05:04 MSK 2020 x86_64
User: prospack (1025)
PHP: 8.2.23
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/prospack/public_html/wp-content/themes/astra/admin/assets/hooks/useDebounceEffect.js
import { useEffect } from 'react';
import { debounce } from '@astra-utils/helpers';

/**
 * A hook that wraps a callback function with a debounce effect.
 *
 * This hook is designed to delay the execution of a function until after a specified delay.
 * It's particularly useful for handling events that occur rapidly, such as typing in a text input.
 *
 * @param {Function} callback - The function to debounce.
 * @param {number} delay - The delay in milliseconds before the function is executed.
 * @param {Array} dependencies - An array of dependencies that trigger the effect.
 */
function useDebounceEffect( callback, delay, dependencies ) {
	useEffect( () => {
		const debouncedCallback = debounce( callback, delay );

		debouncedCallback();

		// Cleanup on unmount or when dependencies change.
		return () => debouncedCallback.cancel && debouncedCallback.cancel();
	}, [ callback, delay, ...dependencies ] );
}

export default useDebounceEffect;