DEV Community

Himanshupal0001
Himanshupal0001

Posted on

Debounce in Js⛹️‍♂️

Debounce is a mechanism for optimization in react. It reduces the unnecessary re-renders in react.

Where to use debounce
Remember when you type something on google and it gives you suggestions?
Debounce is used in these type of situtations. It can be used in search engines like e-commerce social media extra.
If you observe closely on typing the suggested words only appear either you complete a word or take a little pause.

Below is the code snippet you can use in these type of situations.


 let timer = 500, timeout;
    const debounce = (fun) => {
        clearTimeout(timeout);
        timeout = setTimeout(fun, timer);
    }


    const updateText = (text, id) => {
        debounce(() => props.updateText(text, id))
    };
Enter fullscreen mode Exit fullscreen mode

Top comments (0)