Signature
Parameters
Initial state value.
Throttle window in milliseconds for setter calls.
Returns
A tuple containing the current value and a throttled setter function.
Learn more about Mintlify
Enter your email to receive updates about new features and product releases.
Manages local state with throttled updates triggered through the setter
function useThrottledState<T>(
defaultValue: T,
wait: number
): readonly [T, (value: T) => void]
import { useThrottledState } from '@kuzenbo/hooks';
function Counter() {
const [count, setCount] = useThrottledState(0, 1000);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment (throttled)
</button>
</div>
);
}
import { useThrottledState } from '@kuzenbo/hooks';
import { useEffect } from 'react';
function VolumeControl() {
const [volume, setVolume] = useThrottledState(50, 100);
useEffect(() => {
// Update audio volume
const audio = document.querySelector('audio');
if (audio) {
audio.volume = volume / 100;
}
}, [volume]);
return (
<div>
<label>Volume: {volume}%</label>
<input
type="range"
min="0"
max="100"
value={volume}
onChange={(e) => setVolume(Number(e.target.value))}
/>
</div>
);
}
import { useThrottledState } from '@kuzenbo/hooks';
import { useEffect } from 'react';
function MouseTracker() {
const [position, setPosition] = useThrottledState({ x: 0, y: 0 }, 100);
useEffect(() => {
const handleMouseMove = (e: MouseEvent) => {
setPosition({ x: e.clientX, y: e.clientY });
};
window.addEventListener('mousemove', handleMouseMove);
return () => window.removeEventListener('mousemove', handleMouseMove);
}, []);
return (
<div>
Mouse position: {position.x}, {position.y}
</div>
);
}
import { useThrottledState } from '@kuzenbo/hooks';
import { useEffect } from 'react';
function AutoSaveForm() {
const [formData, setFormData] = useThrottledState(
{ name: '', email: '' },
2000
);
useEffect(() => {
if (formData.name || formData.email) {
fetch('/api/autosave', {
method: 'POST',
body: JSON.stringify(formData)
});
}
}, [formData]);
return (
<form>
<input
placeholder="Name"
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
/>
<input
placeholder="Email"
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
/>
</form>
);
}