raddix

useDebounce

Returns a debounced value of the provided value.

Installation

Install the custom hook from your command line.

npm i @raddix/use-debounce

Usage

import { useState } from 'react';
import { useDebounce } from '@raddix/use-debounce';
export default function App() {
const [value, setValue] = useState<string>();
const debouncedValue = useDebounce(value, 800);
return (
<div>
<input
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="Typed value"
/>
<p>DebouncedValue: {debouncedValue}</p>
</div>
);
};

API

Parameters

ParametersDescriptionRequiredType
valueThe value you want to bounce.Yesany
delayThe delay time in milliseconds.Nonumber

Returns

ReturnsDescriptionType
debouncedValueThe debounce value. After the delay time has elapsed without the value changing, it will be updated to the most recent value.any