Skip to main content

useThrottledValue

function useThrottledValue<T>(value: T, ms: number): T;

Returns value, updated at most once every ms. Use it for values that change very often, such as a scroll position or pointer coordinates, when a little lag is fine but waiting for them to stop, as useDebouncedValue does, is not. The first value is returned at once, and so is the first change. Changes during the wait that follows collapse into one update with the latest value when it ends. With ms of 0 or less (or NaN) it follows value straight away. A pending update is dropped when the component unmounts.

Live demo​

Preview, updated at most every 0.5 s:

Nothing to preview

Example​

import { useThrottledValue, useWindowScroll } from "react-honey-hooks";

function ReadingProgress() {
const { y } = useWindowScroll();
const throttledY = useThrottledValue(y, 100);
return <ProgressBar value={throttledY} />;
}