
Security News
New React Server Components Vulnerabilities: DoS and Source Code Exposure
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.
@custom-react-hooks/use-debounce
Advanced tools
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.
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.
Choose and install individual hooks that suit your project needs, or install the entire collection for a full suite of utilities.
npm install @custom-react-hooks/use-debounce
or
yarn add @custom-react-hooks/use-debounce
npm install @custom-react-hooks/all
or
yarn add @custom-react-hooks/all
Function Execution Delay: Delays the execution of a function until a certain amount of time has passed since its last invocation, effectively controlling the rate at which the function fires.
Configurable Delay and Max Wait: Offers the flexibility to set both a delay for the debounce and a maximum waiting time, ensuring the function is executed after a certain period even if the input continues.
Leading and Trailing Edge Execution: Provides options for executing the function at the start (leading edge) or at the end (trailing edge) of the debounce delay, catering to different use case requirements.
Improved Performance in Rapid Input Scenarios: Particularly useful in scenarios involving rapid user inputs, such as typing in search fields or adjusting UI elements, to prevent excessive function calls.
Cancel Functionality: Includes a mechanism to cancel the debounced action, offering additional control over the debounced function's behavior.
Optimized for User Interaction Scenarios: Ideal for use cases like search inputs, form validations, or any scenario where user input can trigger frequent updates or API calls.
Versatile Application: Can be used in various scenarios beyond input fields, such as debouncing API calls, resize or scroll event handlers, and more.
Memory Leak Prevention: Cleans up timeouts on component unmount to prevent memory leaks, ensuring better resource management in React applications.
Refined User Experience: Enhances user experience by reducing the frequency of potentially disruptive or intensive operations during rapid user interactions.
Rate-limiting for User Actions: Useful for rate-limiting user actions, preventing excessive or unintended interactions, like rapid button clicks or toggle switches.
Here's an example of using useDebounce in a search input component:
import React, { useState } from 'react';
import { useDebounce } from '@custom-react-hooks/all';
const DebounceComponent = () => {
const [inputValue, setInputValue] = useState('');
const [debouncedValue, setDebouncedValue] = useState('');
const [updateDebouncedValue] = useDebounce(
(val) => {
setDebouncedValue(val);
},
1000,
{ maxWait: 2000 },
);
const handleChange = (e) => {
const value = e.target.value;
setInputValue(value);
updateDebouncedValue(value);
};
return (
<div>
<input
type="text"
value={inputValue}
onChange={handleChange}
placeholder="Type here..."
/>
<p>Debounced Value: {debouncedValue}</p>
</div>
);
};
export default DebounceComponent;
In this component, the search function is debounced, which means it will only execute 500 milliseconds after the user stops typing.
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.[debouncedFunction, cancelDebounce]:
debouncedFunction (Function): The debounced version of the provided function.cancelDebounce (Function): A function that can be called to cancel the debounced action.useDebounce hook is useful for optimizing performance in scenarios where you want to limit the frequency of function execution.Your contributions are welcome! Feel free to submit issues or pull requests to improve the useDebounce hook.
FAQs
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.
The npm package @custom-react-hooks/use-debounce receives a total of 472 weekly downloads. As such, @custom-react-hooks/use-debounce popularity was classified as not popular.
We found that @custom-react-hooks/use-debounce demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.