Now let's talk about debouncing. So in this example, as the user's typing, we're just firing off a request on every single key press, and this is what we want to avoid, because it's basically just pounding our API, and it can really slow things down. What we want is we want to wait for the user to finish typing, and then call the API. So the user types, and then once they're done for 200 milliseconds, then we call the API. This is debouncing.
The way we implement it is just with a set timeout. So I'll show you the vanilla JS example first, where essentially we have a timeout ID, and it starts off as empty, but then any time the input changes, we clear the current timeout ID. And so clear timeout is built into the web browser, and we're also going to use the timeout ID to actually set a timeout for the user. So basically, we start off by clearing this timeout, and then it's okay if you actually pass in undefined. It's not going to error out. But then we set a timeout. So we say, okay, in 200 milliseconds, we will then call the API. But if the input callback gets called again, we're going to clear the previous one. So basically, as the user types, we set a timeout, and then immediately clear it, because they typed another character. So set it, clear it, set it, clear it, and then finally, if they don't type, we clear it. We're renting this very thing, but in combination with our use effect.
So for React, the way that we can clear it out is by returning a function from our use effect. And so this is known as our cleanup function, right? We set the timeout stored inside of that timeout ID, but if the search term ever changes, we need to clear that timeout, and so the function that we return from the use effect is going to get invoked, clear the timeout, and basically, it will clear it, clear it, clear it, until the user's done typing, and then make that request off to get results. All of the other frameworks, very similar. So in Preact, we can return a callback from our use signal effect. In Svelte, we can also return a callback from our dollar sign effect. The one thing to note here, though, is that Svelte 5 doesn't automatically track dependency access inside of nested callbacks.
So the hack here is to just get rid of that and then just access it, and basically, like a no-op, just, hey, I need search, and then the Svelte compiler will know that it needs to rerun this function any time search changes, and again, we're returning that cleanup function. Of course, this is a hack. There's probably better ways to do it, but yeah, and then in solid, very similar, but they have this on cleanup function that you just import from the solid library. So there's no need to return a callback, and what's nice about that is you can actually call it anywhere. So if your cleanup function for whatever reason needs to be nested in a callback or something like that, you can get it done with that on cleanup. Inside of Angular, this is kind of, again, I apologize, but this is the kind of like the vanilla example combined with our subscription. So if the timeout changes, we clear it and then call it again if the search value changes, and then with Vue.js, fairly similar, but they give you the on cleanup function as a callback argument.
Comments