Improve Your React App Performance With Debounce

Improve Your React App Performance With Debounce

Get easy and quick React performance with a custom debounce hook. Learn how to create a custom debounce hook that will significantly increase the performance of your React applications. Debounce is simply the act of delaying some piece of code, in our case the fetch request, until after all the rapid changes are made.

Get easy and quick React performance with a custom debounce hook.

One of the biggest benefits to React is its open ended nature. You have the freedom to construct the code however you want, but that freedom makes it very easy to make simple mistakes that can drastically slow down your application. In this quick article I will show you how to create a custom debounce hook that will significantly increase the performance of your applications.

What Is Debounce?

Before we can build a custom debounce hook we first need to understand what debounce is and to do that we need to understand when you would use debounce. Take the below code for example.

useEffect(() => {
  fetch(`https://example.com?q=${query}`)
    .then(res => res.json())
    .then(data => setData(data))
}, [query])

This code is pretty simple and fetches new data every time the query changes. I have seen code like this all over the place in React and that is because it is simple to write and for the most part works. This code does have some issues, though.

If your query changes very quickly then you will be putting a ton of extra stress on the server you are calling with fetch which could slow down your application since you are calling the server every time the query changes.

Another big issue is that network requests can take a varied amount of time and if your query changes often then sometimes the requests that have old query values will come back after requests with new query values which will cause you to display data based on previous queries instead of the most recent query. This is a subtle bug which is hard to catch so it is important to fix it right away since it could easily leak into production.

Debounce is simply the act of delaying some piece of code, in our case the fetch request, until after all the rapid changes are made. This means if query changes 100 time in a second we will only run this code once after that second is over instead of running it 100 times and running into the problems addressed above. Generally this is done with the setTimeout function. So now let’s take a look at how to do this.

Basic React Debounce

As I mentioned above setTimeout is the best way to handle debouncing in JavaScript since we can delay code with it and clear out previous timeouts to prevent multiple executions of the code.

useEffect(() => {
  const timeout = setTimeout(() => {    fetch(`https://example.com?q=${query}`)
        .then(res => res.json())
        .then(data => setData(data))
    }, [query])
  }, 250)  
  return () => clearTimeout(timeout)

The above code may look pretty confusing, but we really only changed a small amount of code.

The first thing we did was wrap our entire fetch request in a setTimeout with a 250 millisecond delay. This means that the fetch code will not run until after a 250 millisecond delay. This alone would just cause a delay between when our query changes and when the fetch is called without addressing any of the above problems. The way we fix those problems is with the final return statement in the useEffect.

react javascript programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Hire Dedicated React Native Developer in India | React Native Development

Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.

Hire React Js Developer from Expert React JS Development Company

Are you looking to [hire React JS developer](https://www.mobiwebtech.com/react-js-development-company/ "hire React JS developer") from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team...

A Short Guide to React Native App Development

React Native is undoubtedly one of the most widely used cross-platform frameworks for creating native-like apps. This framework can be easily used for developing brand-new apps from scratch and even in existing iOS or Android projects.easily used for developing brand-new apps from scratch and even in existing iOS or Android projects.