Skip to Content
BBRICKSv0.7.4
DataHooks

useDebouncedCallback

fn once calls have stopped for ms (default 300), with the last call's arguments. The function identity is stable; the latest fn is always the one called. A pending call is dropped on unmount.

import { useDebouncedCallback } from '@ergasti/bricks';

Examples

Debounce a handler

The function runs once, ms after the last call (300 here); flush runs it now (use it on blur) and cancel drops the pending call.

Code
import { useState } from 'react';
import { useDebouncedCallback } from '@ergasti/bricks';

function Typing() {
  const [sent, setSent] = useState('');
  const send = useDebouncedCallback((q: string) => setSent(q), 300);
  return (
    <div style={{ display: 'grid', gap: 6, maxWidth: 320 }}>
      <input aria-label="Search" placeholder="Type quickly…" onChange={(e) => send(e.target.value)} onBlur={send.flush} />
      <small>Sent after a pause: “{sent}”</small>
    </div>
  );
}

Signature

function useDebouncedCallback<A>(fn: (...args: A) => void, ms?: number): DebouncedCallback<A>

Parameters

Prop

Type

Returns

DebouncedCallback<A>

useAction · useDataLanguage · useDrawerResource · useHashState

Source

src/data/useDebouncedCallback.ts (line 17) on GitHub.

On this page