New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@reworkk/react-debounce

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@reworkk/react-debounce

Use debounce in React

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

Use debounce in React

  • Installation
  • Advantages
  • Examples
  • API reference

Installation

  • yarn add @reworkk/react-debounce
  • npm install @reworkk/react-debounce

Advantages

  • No dependencies
  • Written in TypeScript

Examples

Expensive calculation

const [expensiveCalculation] = useDebounceCallback(() => { ... });
// if user clicks twice, the first call will be cancelled
return <button onClick={expensiveCalculation}>Click</button>

Search and HTTP requests

const [searchText, setSearchText] = useDebounceState('');
const searchParam = useMemo(() => filterValue === '' ? '' : `?search=${searchText}`, [searchText]);

useEffect(() => {
  fetch(`/api/url${searchParam}`);
}, [searchParam]);

// when you stop typing, setSearchText will be called after 300ms (default timeout)
return <input defaultValue={searchText} onChange={({ target }) => setSearchText(target.value)} />

Hooks API reference

useDebounceCallback(callback, options)

Arguments

ArgumentDescription
callbackFunction, that will be executed after delay, if DebouncedCallback will be executed
optionsConfiguration object to control hook behavior (Options)

Options

OptionDescription
timeMsDelay before function will be executed (default - 300 ms)
depsDependencies that indicate that hook callback is changed (default - [])

Returns [DebouncedCallback, CancelFunction]

TypeDescription
DebouncedCallbackFunction that will execute provided callback after timeout (default - 300 ms)
CancelFunctionFunction that allows to prevent callback execution

useDebounceState(defaultValue, timeMs)

Arguments

ArgumentDescription
defaultValueInitial state
timeMsDelay before state will be updated (default - 300 ms)

Returns [State, SetStateFunction, CancelFunction]

TypeDescription
StateCurrent state
SetStateFunctionFunction that allows to update current state
CancelFunctionFunction that allows to prevent execution of SetStateFunction

Keywords

debounce

FAQs

Package last updated on 14 Sep 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