Skip to content

use-throttled-fn

  • A hook which returns a throttled function.

Parameters

ParameterTypeRequiredDefault ValueDescription
cbFunction-A callback which is to be throttled.
delaynumber300A delay in milliseconds after that the callback gets fired.

Returns

  • It returns a function which is throttled version of passed callback.

Usage

ts
import { useState } from 'react'
import { useThrottledFn } from 'classic-react-hooks'

export default function YourComponent() {
   const [throttledInput, setThrottledInput] = useState('')
   const updateInput = useThrottledFn((e) => {
      setThrottledInput(e.target.value)
   }, 300)

   return (
      <div>
         <input onChange={updateInput} placeholder='type something...' />
         <p>
            value - <span>{throttledInput}</span>
         </p>
      </div>
   )
}