@xyzproject/debounce-hook-temp-1
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

useDebounce Hook

Description

useDebounce is a custom React hook that debounces a fast changing value. The hook returns a debounced value that only updates once the specified delay time has passed without the source value changing. This can be useful in various scenarios such as rate limiting search input to reduce API calls, delaying resize events to improve performance, and more.

Why useDebounce?

In many web applications, it's common to have events that fire more frequently than we'd like. For example, a user typing in a search box or resizing the browser window. If we try to handle these events immediately as they occur, it can lead to performance issues.

Debouncing is a technique where we add a delay to these rapidly firing events and only execute the event handler after the event has stopped firing for a certain period of time. This hook abstracts the debouncing logic, making it reusable across different components.

Installation

npm install @your-org/debounce-hook

or 

yarn add @your-org/debounce-hook

Usage

import useDebounce from '@your-org/debounce-hook';

function SearchComponent() {
  const [searchTerm, setSearchTerm] = useState('');
  const debouncedSearchTerm = useDebounce(searchTerm, 500);

  useEffect(() => {
    if (debouncedSearchTerm) {
      // Perform the search
    }
  }, [debouncedSearchTerm]);

  return (
    <input
      type="text"
      value={searchTerm}
      onChange={(e) => setSearchTerm(e.target.value)}
    />
  );
}

// In this example, the SearchComponent component uses the useDebounce hook to debounce the search term.
// The search is only performed when the user has stopped typing for 500 milliseconds.

Readme

Keywords

none

Package Sidebar

Install

npm i @xyzproject/debounce-hook-temp-1

Weekly Downloads

0

Version

1.0.1

License

none

Unpacked Size

3.35 kB

Total Files

6

Last publish

Collaborators

  • z3delta
  • anusingh29
  • artgrove
  • barrett_kev_sapient
  • bhuvan.chawla