I found this code from this answer https://stackoverflow.com/a/68780417/26420821
How does it work?
export function useSearchDebounce(delay = 350) {
const [search, setSearch] = useState(null);
const [searchQuery, setSearchQuery] = useState(null);
useEffect(() => {
const delayFn = setTimeout(() => {
console.log("setTimeout called");
setSearch(searchQuery);
}, delay);
return () => clearTimeout(delayFn);
}, [searchQuery, delay]);
return [search, setSearchQuery];
}
Usage :
const [search, setSearch] = useSearchDebounce();
<input onChange={(e) => setSearch(e.target.value)}/>
If we assume user types "abc" together in the input field with delay set to 5000,
At first, searchQuery will be "a" , it will setTimeout() to run after 5 secs,
Then searchQuery will be "ab" , it will again setTimeout() to run after 5 secs,
Then searchQuery will be "abc" , it will again setTimeout() to run after 5 secs
But when I tested console.log() executed just once, Why didn't setTimeout() execute 3 times ?
Am I misunderstanding something ?
I don't understand the code
() => clearTimeout(delayFn);which is called whenever the useeffect is rerun due to dependency changes. This clears the pending timeout and creates a new one.useEffecthook cleanup function runs prior to the next effect being applied and when the component unmounts?