Refer to the previous post to understand what is debounce and when to use debounce.
In this thread🧵, we'll implement debounce in 2 different ways
...
The general idea here is to start with 0 timeout, then if the debounced function is called again, reset the timer to the specified delay. In case of timeout, call the debounce function.
Thus every call to a debounce function resets the timer and delays the call. Let’s dive in and see what a debounce looks like-
const debounce = function(func, delay){
let timer;
return function () { //anonymous function
const context = this;
const args = arguments;
clearTimeout(timer);
timer = setTimeout(()=> {
func.apply(context, args)
},delay);
}
}
Another way to implement debouncing is using lodash. Lodash provides a debounce method that we can use to limit the rate of execution of the handler function. Just wrap the callback function with the debounce method and provide the amount of delay we want between two events.
import { debounce } from 'lodash'
const debouncedSearch = useCallback(
debounce(val => {
onSearch(val)
}, 750),
[onSearch]
)
But there is a problem with useCallback here, you get the following error:
useCallback
expects an inline function. Handing it the returned function from a debounce or throttle doesn't cut the mustard. Why? The ESLint rule can't figure out what the exhaustive dependencies should be. How do we fix this?
To remove this error, use useMemo
instead:
import { debounce } from 'lodash';
const debouncedSearch = useMemo(
() =>
debounce(
(val) => {
onSearch(val)
},750),
[onSearch]
);
Types of Rich Text Editors In the previous post I explained what are RTEs and why are they used, in this post, I am going to throw some light on the types of RTEs, and will make you familiar with the sample content format. https://fifo.im/p/35ghmd1uxln0
Rich Text Editor In recent years, the field of Content Creation and Representation on Digital platforms has seen massive disruption. This transition has shown how companies are racing to build the best experience for content creators in the enterprise domain and trying to find innovative ways to break the traditional molds of sharing and consuming content.