JavaScript Snippet

React useDebounce Hook

Difficulty: Easy

Debouncing a fast-changing value (a search input, a window resize, a slider) is one of the first React-specific utilities every project needs. This snippet shows the simplest one-state useDebounce hook, a leading-edge variant for instant first reactions, and a useDebouncedCallback variant that wraps a function instead of a value. Pick the shape that matches what your component actually needs.

Code Snippets
/

React useDebounce Hook

React useDebounce Hook

Debouncing a fast-changing value (a search input, a window resize, a slider) is one of the first React-specific utilities every project needs. This snippet shows the simplest one-state useDebounce hook, a leading-edge variant for instant first reactions, and a useDebouncedCallback variant that wraps a function instead of a value. Pick the shape that matches what your component actually needs.

JavaScript
Easy
3 snippets
react
hooks
performance-optimization
code-template

220 views

7

The hook stores the latest value in local state and, every time the input changes, schedules a setTimeout to copy it across after delay ms. The cleanup function in useEffect cancels the previous timeout whenever the value changes again, which is the entire trick: only the last keystroke ever lands. Reach for this when a downstream effect (a network call, an expensive layout) should not run on every keystroke. Time complexity is O(1) per change, and the debounced state always converges to the latest input after delay ms of quiet.