# useDebouncedValue / useDebouncedCallback (/docs/hooks/use-debounced)





Both hooks wait until changes have stopped for `delay` ms. `useDebouncedValue` holds back a **value**, so you can put it straight in a query key. `useDebouncedCallback` holds back a **call**, so you can run it from an event handler. `DataTable`'s search box and text filters debounce their commits this way.

```tsx
import { useDebouncedCallback, useDebouncedValue } from '@42-central/ui-react';

const query = useDebouncedValue(search, 300);
const save = useDebouncedCallback((draft: string) => persist(draft), 500);
```

Both are SSR-safe: timers only start in effects and event handlers, and `useDebouncedValue` starts at the first `value`, so the server and the first client render agree.

## Usage [#usage]

Type quickly: the value follows every keystroke, while the debounced value and the search only move once you pause for 500ms.

<Preview code="&#x22;const [value, setValue] = useState('');\nconst debounced = useDebouncedValue(value, 500);\nconst search = useDebouncedCallback(runSearch, 500);\n\n<Input\n  value={value}\n  onChange={(event) => {\n    setValue(event.target.value);\n    search(event.target.value);\n  }}\n/>&#x22;">
  <DebouncedDemo />
</Preview>

## Value or callback [#value-or-callback]

Reach for `useDebouncedValue` when the value feeds something that reacts on its own, like a `useQuery` key or a derived list. Reach for `useDebouncedCallback` when you'd otherwise call a function on every change, like saving a draft or reporting a filter upward.

Both are **trailing**: they act once, with the last value or arguments, after the pause. A call still pending when the component unmounts is dropped.

`useDebouncedCallback` always calls the latest `fn`, so an inline closure works and you don't need `useCallback`. The function it returns keeps the same identity across renders until `delay` changes, so it's safe in an effect's dependencies.

## API [#api]

### useDebouncedValue [#usedebouncedvalue]

<TypeTable
  type="{
  value: {
    type: 'T',
    required: true,
    description: 'The value to debounce. Compared with Object.is, so a new object on every render keeps restarting the wait.',
  },
  delay: {
    type: 'number',
    required: true,
    description: 'How long, in ms, value must stay unchanged before it is returned.',
  },
}"
/>

Returns `T`: the last `value` that held for `delay` ms, starting at the first `value`.

### useDebouncedCallback [#usedebouncedcallback]

<TypeTable
  type="{
  fn: {
    type: '(...args: A) => void',
    required: true,
    description: 'The function to call once calls stop. Always the latest one passed, so it needn\'t be memoized.',
  },
  delay: {
    type: 'number',
    required: true,
    description: 'How long, in ms, calls must stop before fn runs.',
  },
}"
/>

Returns `(...args: A) => void`: calling it restarts the wait, and `fn` runs once with the last call's arguments.
