This hook is built on top of debounce() from lodash and accepts the same arguments.
Example of a debounced value:
import React, { useState, useEffect } from"react"import { useDebounce } from"@corets/use-debounce"constExample= () => {const [input,setInput] =useState("")constdebouncedInput=useDebounce(input,300)consthandleSearch= () => {// fetch some data based on input... }consthandleChange= (e) =>setInput(e.target.value)// react to changes of the debounced inputuseEffect(handleSearch, [debouncedInput])return (<input type="text" onChange={handleChange} /> )}
Example of a debounced function:
import React, { useState, useEffect } from"react"import { useDebounce } from"@corets/use-debounce"constExample= () => {const [input,setInput] =useState("")consthandleSearch=useDebounce((query:string) => {// you can also access variable "input" directly, if you want to// fetch some data based on input... },300)consthandleChange= (e) => {setInput(e.target.value)// trigger search immediately, this call will be debouncedhandleSearch(e.target.value) }return (<input type="text" onChange={handleChange} /> )}
This hook accepts a third argument, identical to the one expected by the debounce() function from lodash.