Hooks
Functions
useDebounce
This hook provides an easy way to debounce any function, ensuring that it only executes after a specified delay.
Example
import { useState, useCallback } from 'react'
import Input from '@/components/ui/Input'
import useDebounce from '@/utils/hooks/useDebounce'
import type { ChangeEvent } from 'react'
const Example = () => {
const [query, setQuery] = useState('')
const handleSearch = (input: string) => {
console.log('Searching for:', input)
};
const debouncedSearch = useDebounce(handleSearch, 300);
const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
const value = event.target.value
setQuery(value)
debouncedSearch(value)
}, [debouncedSearch])
return (
<Input
type="text"
value={query}
onChange={handleChange}
placeholder="Search..."
/>
);
};
export default Example
Params
| param | Description | Type | Default |
|---|---|---|---|
| func | The function to debounce. | (...args: any) | - |
| wait | The number of milliseconds to delay. If not provided, the function will be debounced with a default delay (usually determined by lodash) | number | - |
API
Return
| return | Description | Type | Default |
|---|---|---|---|
| func | A debounced version of the provided function, which will delay its execution based on the specified wait time and options. | (...args: any) | - |