How to Use Debounce with useQuery in React Query?

Learn how to use debounce with useQuery in React Query to fetch filtered data based on user input without triggering excessive network requests.

Summarize with AI

Not enough time? get the key points instantly.

Instead of debouncing the fetch function directly, you should debounce the query parameters (e.g., the search value) so that the queryKey changes only after a delay. This way useQuery in React Development is called only when the input has stabilized, not on every keystroke.

1. Create a useDebounce Hook

This hook will delay the value and only set the debounced value after the specified delay:

function useDebounce(value, delay) {  const [debouncedValue, setDebouncedValue] = useState(value);  useEffect(() => {

    const handler = setTimeout(() => setDebouncedValue(value), delay);

    return () => clearTimeout(handler);

  }, [value, delay]);

  return debouncedValue;

}

2. Use Debounced Value in Your Query Component

Now use useDebounce with useQuery like this:

const debouncedSearch = useDebounce(search, 500);

const { data, isLoading, isError } = useQuery({

  queryKey: [‘todos’, page, debouncedSearch],

  queryFn: () => fetch(`/api/todos?page=${page}&q=${debouncedSearch}`)

               .then(res => res.json()),

  enabled: !!debouncedSearch,

});

This way the query will only run after the debounced value changes

Also Read:

Why This Works

  • Debouncing the queryKey instead of the query function matches React Query’s caching and lifecycle logic.
  • useQuery will only trigger when debouncedSearch changes, not on every keystroke.
  • You still get all the benefits of caching, isLoading, isError and other React Query states

Resources

Insights from our engineering leaders.

Our team shares practical insights on the technologies and engineering decisions shaping enterprise software today, grounded in real client work.

ReactJS

Filter Items Based On Multiple Properties

Why am I getting this problem? If you apply separate filters in sequence or only check one property at a time your code will match…