gridcn2.0.0
Types and hooks

useDebounce

Delay a value with useDebounce until it stops changing, for example before the value feeds a request.

Typing fires a change on every key. useDebounce returns a copy of a value that follows the value only after the value stays the same for a wait. The hook does not depend on the grid.

pnpm dlx shadcn@latest add @gridcn/use-debounce

The item installs to components/gridcn/use-debounce.ts. The same file holds useDebouncedInput.

useDebounce

function useDebounce<T>(value: T, delay?: number | { delayMs?: number }): T;

Debounce a state value before it feeds a request:

import { useState } from "react";
import { useDebounce } from "@/components/gridcn/use-debounce";

export function SearchBox() {
  const [query, setQuery] = useState("");
  const debouncedQuery = useDebounce(query, 300);
  return (
    <>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      <p>Searching for {debouncedQuery}</p>
    </>
  );
}

The input shows each keystroke at once. The line below it changes 300 ms after the last keystroke.

Arguments

ArgumentTypeDefault
valueTRequired
delaynumber | { delayMs?: number }300

Behavior

  • The first render returns value.
  • Each change of value or delayMs restarts the wait.
  • An unmount cancels a pending update.
  • With a delay of 0 or less, the hook returns value itself and sets no timer.

inputDebounceMs

The grid waits for typing in its own text inputs through useDebouncedInput. Quick search describes the inputDebounceMs prop.

On this page