one-hook
Size
0.23 kb
View source

useDebounceFn

A debounce function, reactified.

Ensures that a function is only executed after a specified delay following the last invocation.

Installation

npm install @1hook/use-debounce-fn

Usage

import {  } from '@1hook/use-debounce-fn'

The useDebounceFn hook accepts a delay in milliseconds and returns a debounce function.

All calls are delayed until 1000ms have passed since the last invocation, then only the final call executes.

const {  } = (1000)

(() => .('Skipped'))
(() => .('Executed after 1000ms'))

Override the default delay:

const {  } = (1000)

(() => .('Skipped'))
(() => .('Executed after 500ms'), 500)

Cancel pending execution:

const { , ,  } = (1000)

 && ()

When the component unmounts, pending executions are automatically canceled.

API Reference

UseDebounceFnReturn

What `useDebounceFn` returns

PropTypeDefault
isPending
boolean
-
cancel
() => void
-
debounce
(fn: () => any, delay?: number | undefined) => void
-

On this page