leading.
Signature
Parameters
Initial state value.
Debounce delay in milliseconds.
Optional debounce behavior configuration.
Returns
A tuple containing the current value and a setter function.
Learn more about Mintlify
Enter your email to receive updates about new features and product releases.
Manages local state updates with debounce timing to reduce rapid re-renders
leading.
function useDebouncedState<T>(
defaultValue: T,
wait: number,
options?: UseDebouncedStateOptions
): UseDebouncedStateReturnValue<T>
Show UseDebouncedStateOptions
true, applies the first update immediately before debouncing subsequent updates.import { useDebouncedState } from '@kuzenbo/hooks';
function SearchInput() {
const [value, setValue] = useDebouncedState('', 500);
return (
<div>
<input
onChange={(e) => setValue(e.target.value)}
placeholder="Type to search..."
/>
<p>Debounced value: {value}</p>
</div>
);
}
import { useDebouncedState } from '@kuzenbo/hooks';
import { useEffect } from 'react';
function Counter() {
const [count, setCount] = useDebouncedState(0, 1000, { leading: true });
useEffect(() => {
console.log('Count updated:', count);
}, [count]);
return (
<div>
<button onClick={() => setCount(count + 1)}>Increment</button>
<p>Count: {count}</p>
</div>
);
}
import { useDebouncedState } from '@kuzenbo/hooks';
function TodoList() {
const [items, setItems] = useDebouncedState<string[]>([], 300);
const addItem = (item: string) => {
setItems((prev) => [...prev, item]);
};
return (
<div>
<button onClick={() => addItem('New item')}>Add Item</button>
<ul>
{items.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</div>
);
}
import { useDebouncedState } from '@kuzenbo/hooks';
import { useEffect, useState } from 'react';
function EmailInput() {
const [email, setEmail] = useDebouncedState('', 500);
const [isValid, setIsValid] = useState(true);
useEffect(() => {
if (email) {
setIsValid(email.includes('@'));
}
}, [email]);
return (
<div>
<input
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter email"
/>
{!isValid && <p>Invalid email address</p>}
</div>
);
}