🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@custom-react-hooks/use-debounce

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@custom-react-hooks/use-debounce

The `useDebounce` hook is used to delay the execution of a function until a specified amount of time has passed since it was last invoked. This is useful for handling rapid user input scenarios, such as search input fields or window resizing.

Source
npmnpm
Version
1.4.13
Version published
Weekly downloads
712
27.83%
Maintainers
1
Weekly downloads
 
Created
Source

useDebounce Hook

The useDebounce hook is used to delay the execution of a function until a specified amount of time has passed since it was last invoked. This is useful for handling rapid user input scenarios, such as search input fields or window resizing.

Installation

npm install @custom-react-hooks/use-debounce

or

yarn add @custom-react-hooks/use-debounce

Usage

Here's an example of using useDebounce in a search input component:

import React, { useState } from 'react';
import useDebounce from 'custom-react-hooks/useDebounce';

const DebounceTestComponent: React.FC = () => {
  const [inputValue, setInputValue] = useState('');

  const debouncedLog = useDebounce((val: string) => {
    console.log(`Debounced value: ${val}`);
  }, 1000)[0];

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setInputValue(e.target.value);
    debouncedLog(e.target.value);
  };

  return (
    <div>
      <input
        type="text"
        value={inputValue}
        onChange={handleChange}
      />
    </div>
  );
};

export default DebounceTestComponent;

In this component, the search function is debounced, which means it will only execute 500 milliseconds after the user stops typing.

API Reference

Parameters

  • callback (Function): The function to debounce.
  • delay (number): The number of milliseconds to delay.
  • options (object, optional): Configuration options for the debounce behavior. Options include:
    • maxWait (number): The maximum time the function can be delayed before it's executed, regardless of subsequent calls.
    • leading (boolean): If true, the function is executed on the leading edge of the timeout.
    • trailing (boolean): If true, the function is executed on the trailing edge of the timeout.

Returns

  • [debouncedFunction, cancelDebounce]:
    • debouncedFunction (Function): The debounced version of the provided function.
    • cancelDebounce (Function): A function that can be called to cancel the debounced action.

Notes

  • The useDebounce hook is useful for optimizing performance in scenarios where you want to limit the frequency of function execution.
  • Make sure to adjust the delay based on your specific use case.

Contributing

Your contributions are welcome! Feel free to submit issues or pull requests to improve the useDebounce hook.

FAQs

Package last updated on 01 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts