Skip to content
Advertisement

useEffect debounce search technique

i’m trying to implement search functionality but i don’t want to call the api every time i type something . here is the code:

JavaScript

Advertisement

Answer

you can use the setTimeout() function and clearTimeout in the cleanup function and the useRef hook to get the previous state to call the API only once :

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement