Skip to main content

useThrottle

The useThrottle hook limits how often a fast-changing value updates. The returned value changes at most once per interval and always settles on the latest value, which makes it a good fit for scroll, resize, or slider values that would otherwise trigger expensive work on every change.

Usage​

Installation​

import { useThrottle } from "hookstorm";

Parameters​

  • value — The value to throttle.
  • interval — (optional) The minimum time in milliseconds between updates. Default: 500.

Return Values​

The throttled value, typed the same as the input.

Example Usage​

import { ReactElement, useState } from "react";
import { useThrottle } from "hookstorm";

export default function ThrottledSlider(): ReactElement {
const [value, setValue] = useState(0);
const throttledValue = useThrottle(value, 300);

return (
<div>
<input
type="range"
min={0}
max={100}
value={value}
onChange={(e) => setValue(Number(e.target.value))}
/>
<p>Live: {value}</p>
<p>Throttled: {throttledValue}</p>
</div>
);
}

Notes​

  • The first change after a quiet period is applied immediately; changes inside the interval are collapsed into one trailing update.
  • Use useDebounce instead when you only care about the value after the user stops changing it.