useDebouncy
🌀 Small (~0.2kb) debounce effect hook for React with TypeScript support
Features
- 👌 No dependencies.
- 🏋️ Tiny. ~0.2kb.
- 🦾 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
Usage
Use as effect hook
import React, { useState } from 'react';
import { useDebouncyEffect } from 'use-debouncy';
const App = () => {
const [value, setValue] = useState('');
useDebouncyEffect(
() => fetchData(value),
400,
[value],
);
return <input value={value} onChange={(event) => setValue(event.target.value)} />;
};
Use as callback function
import React, { useState } from 'react';
import { useDebouncyFn } from 'use-debouncy';
const App = () => {
const handleChange = useDebouncyFn(
(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. |
Development
License