useDebouncy
π Small (~0.2kb) debounce effect hook for React with TypeScript support
![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Feavam%2Fuse-debouncy.svg?type=shield)
![](https://github.com/eavam/use-debouncy/raw/HEAD/assets/example.gif)
Features
- π No dependencies.
- ποΈβ Tiny. ~0.2kb. Size Limit controls the size.
- π¦Ύ Performance. Used by requestAnimationFrame.
- π Types. Support TypeScript.
- π£ Easy. Use like React effect or function.
Installation
NPM
npm install use-debouncy
Yarn
yarn add use-debouncy
Import bit component
Check bit component here
bit import eavam.use-debouncy/use-debounce
Usage
Use as effect hook
import React, { useState } from 'react';
import useDebouncy from 'use-debouncy/effect';
const App = () => {
const [value, setValue] = useState('');
useDebouncy(
() => fetchData(value),
400,
[value],
);
return (
<input value={value} onChange={(event) => setValue(event.target.value)} />
);
};
Use as callback function
import React, { useState } from 'react';
import useDebouncy from 'use-debouncy/fn';
const App = () => {
const handleChange = useDebouncy(
(event) => fetchData(event.target.value),
400,
);
return <input value={value} onChange={handleChange} />;
};
API Reference
useDebouncy/effect
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). |
useDebouncy/fn
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. |
License
![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Feavam%2Fuse-debouncy.svg?type=large)