New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

use-debouncy

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-debouncy

🌀 Small (~0.2kb) debounce effect hook for React with TypeScript support


Version published
Weekly downloads
38K
decreased by-14.21%
Maintainers
1
Weekly downloads
 
Created

useDebouncy

🌀 Small (~0.2kb) debounce effect hook for React with TypeScript support

license dependents types codecov

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

Check bit component here

# Import bit component
bit import eavam.use-debouncy/use-debounce

Usage

Demo codesandbox

Use as effect hook

import React, { useState } from 'react';
import useDebouncy from 'use-debouncy/effect'; // <== importing from effect

const App = () => {
  const [value, setValue] = useState('');

  useDebouncy(
    () => 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)} />
  );
};

Use as callback function

import React, { useState } from 'react';
import useDebouncy from 'use-debouncy/fn'; // <== importing from fn

const App = () => {
  const handleChange = useDebouncy(
    (event) => fetchData(event.target.value), // function debounce
    400, // number of milliseconds to delay
  );

  return <input value={value} onChange={handleChange} />;
};

API Reference

useDebouncy/effect

function useDebouncyEffect(fn: () => void, wait?: number, deps?: any[]): void;
PropRequiredDefaultDescription
fnDebounce callback.
wait0Number 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;
PropRequiredDefaultDescription
fnDebounce handler.
wait0Number of milliseconds to delay.

FAQs

Package last updated on 19 Apr 2021

Did you know?

Socket

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.

Install

Related posts