π Small (~0.2kb) debounce effect hook for React with TypeScript support
- π No dependencies.
- ποΈβ Tiny. ~0.2kb.
- π¦Ύ Performance. Used by requestAnimationFrame.
- π Types. Support TypeScript.
- π£ Easy. Use like React effect or function.
import React, { useState } from 'react';
import { useDebouncyEffect } from 'use-debouncy';
const App = () => {
const [value, setValue] = useState('');
useDebouncyEffect(
() => fetchData(value), // function debounce
400, // number of milliseconds to delay
[value], // array values that the debounce depends (like as useEffect)
);
return <input value={value} onChange={(event) => setValue(event.target.value)} />;
};
import React, { useState } from 'react';
import { useDebouncyFn } from 'use-debouncy';
const App = () => {
const handleChange = useDebouncyFn(
(event) => fetchData(event.target.value), // function debounce
400, // number of milliseconds to delay
);
return <input value={value} onChange={handleChange} />;
};
function useDebouncyEffect(fn: () => void, wait?: number, deps?: any[]): void;
Prop |
Required |
Default |
Description |
fn |
β |
|
Debounce callback. |
wait |
|
0 |
Number of milliseconds to delay. |
deps |
|
[] |
Array values that the debounce depends (like as useEffect). |
function useDebouncyFn(fn: (...args: any[]) => void, wait?: number): (...args: any[]) => void;
Prop |
Required |
Default |
Description |
fn |
β |
|
Debounce handler. |
wait |
|
0 |
Number of milliseconds to delay. |