Logo

dev-resources.site

for different kinds of informations.

Optimizing React Performance with useDebounce.

Published at
12/23/2023
Categories
react
performance
debounce
optimisation
Author
alwaz
Author
5 person written this
alwaz
open
Optimizing React Performance with useDebounce.

The term “debounce” might sound technical, but its concept is like a traffic light for signals in the world of software development. Imagine you’re at a busy intersection, and there’s a button to cross the road. If you press and release it quickly, the system might get confused, thinking you pressed it multiple times.

Debouncing is giving the system a short break. Picture the traffic light saying, “Wait a sec, let’s make sure the person wants to cross before changing the signal again” In software development, this concept is crucial when dealing with heavy tasks, such as numerous API calls, where efficiency is key.

The Initial Approach

import { useEffect, useState } from "react";
import { getUsers } from "./api";
import User from "./type";
import "./styles.css";

export default function App2() {
  const [search, setSearch] = useState<string>("");
  const [loading, setLoading] = useState<boolean>(false);
  const [user, setUser] = useState<User[]>([]);

  useEffect(() => {
    const loadUsers = async () => {
      setLoading(true);
      const users = await getUsers(search);
      setUser(users?.users);
      setLoading(false);
    };

    loadUsers();
  }, [search]);

  const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearch(e.target.value);
  };

  return (
    <div className="App">
      <input type="text" value={search} onChange={handleSearch} />
      {loading ? (
        <>Loading...</>
      ) : (
        user.map((user) => <h2 key={user.id}>{user?.firstName}</h2>)
      )}
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Let’s take a look at a React code snippet above that searches a list of users based on user input. While this code works as you can see in the output below, it listens to every keystroke, leading to potentially unnecessary API calls and reduced performance.

Introducing Debounce for Performance

To tackle this issue, we introduced the concept of debounce and created a custom hook, useDebounce, to delay updates to a value until a specified time has passed since the last update.

import { useEffect, useState } from "react";

export const useDebouce = <T>(value: T, delay = 500) => {
  const [deboucedValue, setDebouncedValue] = useState<T>(value);

  useEffect(() => {
    const timeout = setTimeout(() => {
      setDebouncedValue(value);
    }, delay);

    return () => clearTimeout(timeout);
  }, [value, delay]);

  return deboucedValue;
};
Enter fullscreen mode Exit fullscreen mode

Now, using this custom hook to our React component.

import { useEffect, useState } from "react";
import { getUsers } from "./api";
import User from "./type";
import { useDebouce } from "./hooks";
import "./styles.css";

export default function App() {
  const [search, setSearch] = useState<string>("");
  const [loading, setLoading] = useState<boolean>(false);
  const [user, setUser] = useState<User[]>([]);
  const debouncedSearch = useDebouce(search);

  useEffect(() => {
    const loadUsers = async () => {
      setLoading(true);
      const users = await getUsers(debouncedSearch);
      setUser(users?.users);
      setLoading(false);
    };

    loadUsers();
  }, [debouncedSearch]);

  const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearch(e.target.value);
  };

  return (
    <div className="App">
      <input type="text" value={search} onChange={handleSearch} />
      {loading ? (
        <>Loading...</>
      ) : (
        user.map((user) => <h2 key={user.id}>{user?.firstName}</h2>)
      )}
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

we can observe a significant improvement as you can see in the output video below. The API call is triggered only when the user stops typing, enhancing the overall efficiency of the app.

Why It Matters?

Implementing search functionality is a common requirement in applications. Humans typically type more than one character every 500ms. By applying debounce, we can reasonably assume that the user has finished typing if there’s been no input for 500ms, optimizing the application’s performance.

Conclusion

In conclusion, understanding and applying debounce can greatly enhance the performance of your React applications. By incorporating the useDebounce hook, you can strike a balance between responsiveness and efficiency, ensuring a smoother user experience.

Try implementing debounce in your projects and share your experiences. The optimization possibilities are vast, and by mastering these concepts, you empower yourself to build more performant and responsive applications.


Connect with me to stay updated on more tips and tricks for efficient coding! Follow me on GitHub for the latest repositories and projects. Let’s connect on LinkedIn to share insights and collaborate on exciting ventures.

Happy coding!

debounce Article's
30 articles in total
Favicon
If you think debounce or onblur is not working in React, you can check it.
Favicon
Enhance Your React Native App with Debouncing for API Optimization
Favicon
Dboun
Favicon
React: Debouncing input with useEffect
Favicon
Debounce Method for Searching
Favicon
Optimizing React Performance with useDebounce.
Favicon
Understanding JavaScript Debounce and Throttling
Favicon
Debounce function with a return value using promises
Favicon
Introducing dwayne/elm-debouncer: Debounce or throttle your actions in Elm
Favicon
Debouncing De-coded
Favicon
Some Problems I Face When Building React Single Page Application And How I Solve Them
Favicon
Debounce Input in React
Favicon
Como aplicar debounce em input de busca sem lodash
Favicon
Debouncing: how to use it
Favicon
Debounce in Next.js
Favicon
Optimized React Search Bar 🔯
Favicon
Learn Debounce and Throttle
Favicon
Используем Throttle и Debounce в React
Favicon
Debouncing in Javascript using a custom function or Lodash library.
Favicon
Debounce Function in Javascript 🚀
Favicon
Debouncing TypeScript
Favicon
How to use Lodash debounce method?
Favicon
Debounce in JavaScript
Favicon
What is Debouncing? Search Suggestions - A Javascript Implementation
Favicon
Throttle and Debounce in React
Favicon
Debounce and Throttle
Favicon
useDebounce hook in React, Improve performance of your React app with Debouncing
Favicon
Debouncing (lodash) with React
Favicon
Use lodash.debounce inside a function component in React
Favicon
How to Debounce UIButton Taps

Featured ones: