use-debounce

PreviousNext
Docs
platehook

Preview

Loading preview…
registry/hooks/use-debounce.ts
import * as React from 'react';

export const useDebounce = <T>(value: T, delay = 500) => {
  const [debouncedValue, setDebouncedValue] = React.useState(value);

  React.useEffect(() => {
    const handler: NodeJS.Timeout = setTimeout(() => {
      setDebouncedValue(value);
    }, delay);

    // Cancel the timeout if value changes (also on delay change or unmount)
    return () => {
      clearTimeout(handler);
    };
  }, [value, delay]);

  return debouncedValue;
};

Installation

npx shadcn@latest add @plate/use-debounce

Usage

import { UseDebounce } from "@/hooks/use-debounce"
const value = UseDebounce()