useDebouncedValue / useDebouncedCallback
Wait for a pause before acting on fast changes, like a search box that queries once the user stops typing.
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.
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
Type quickly: the value follows every keystroke, while the debounced value and the search only move once you pause for 500ms.
Code
const [value, setValue] = useState('');
const debounced = useDebouncedValue(value, 500);
const search = useDebouncedCallback(runSearch, 500);
<Input
value={value}
onChange={(event) => {
setValue(event.target.value);
search(event.target.value);
}}
/>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
useDebouncedValue
Prop
Type
Returns T: the last value that held for delay ms, starting at the first value.
useDebouncedCallback
Prop
Type
Returns (...args: A) => void: calling it restarts the wait, and fn runs once with the last call's arguments.
useLocalStorage
Persists a piece of state to localStorage, kept in sync across every component using the same key — within the tab and across tabs.
useIsomorphicLayoutEffect
useLayoutEffect during actual client rendering, useEffect during a server render pass — a drop-in replacement that silences React's SSR warning.