functionuseDebounce(value,wait=200){const[debounceValue,setDebounceValue]=setState(value);useEffect(()=>{consttimer=setTime(()=>{setDebounceValue(value);},wait);return()=>clearTimeout(timer);},[value,wait]);returndebounceValue;}// use useDebounceconst[input,setInput]=setState();constdebounceInput=useDebounce(input);useEffect(()=>{//http with debounceInput},[debounceInput]);
Thanks for sharing Jackzhoumie. I'll try your solution today. Note: for my test I was required to use Lodash's debounce that's why I had such a strong focus on using it.
So you can avoid importing Lodash debounce and it works just about the same. The only difference I see is that you have to handle the initial render to NOT trigger the debounced function call. PS: I edited my original post to include this solution.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
no need loash actuall.
use this hook
Thanks for sharing Jackzhoumie. I'll try your solution today. Note: for my test I was required to use Lodash's debounce that's why I had such a strong focus on using it.
Actually you are right, I managed to make a working example on the same Codesandbox project: codesandbox.io/s/react-debounced-d...
So you can avoid importing Lodash debounce and it works just about the same. The only difference I see is that you have to handle the initial render to NOT trigger the debounced function call. PS: I edited my original post to include this solution.