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))
};
Top comments (0)