Skip to content

useThrottle

Returns a throttled copy of a changing value.

Returns a throttled copy of a changing value.

Import

import { useThrottle } from '@kamod-ch/hooks'

Live demo

Basic demo

Returns a throttled copy of a changing value.

Immediate: 0

Throttled: 0

import { useState } from 'preact/hooks'
import { useThrottle } from '@kamod-ch/hooks'

export default function UseThrottleDemo() {
  const [value, setValue] = useState(0)
  const throttled = useThrottle(value, { wait: 500 })

  return (
    <div>
      <input type="range" min={0} max={100} value={value} onInput={(event) => setValue(Number(event.currentTarget.value))} />
      <p><strong>Immediate:</strong> {value}</p>
      <p><strong>Throttled:</strong> {throttled}</p>
    </div>
  )
}

API

TypeScript signature
import type { ThrottleOptions } from './throttleOptions.js';
declare function useThrottle<T>(value: T, options?: ThrottleOptions): T;
export default useThrottle;
/

SSR considerations

This hook is safe to import during SSR. If your effect body touches the DOM, keep that work inside the effect callback.

Browser compatibility

Works in any modern browser supported by Preact.

Related hooks