DEV Community

Alwaz Qazi
Alwaz Qazi

Posted on

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!

Top comments (4)

Collapse
 
jorensm profile image
JorensM

Great article, thanks for sharing it!

If it's okay, may I share my article on writing a debounce function that allows you to return a value. This is useful if you want to do some calculations or fetch something and return the value after the debounce timer has run out.

(Author if you don't want me shamelessly plugging my content, let me know and I'll remove it)

Collapse
 
alwaz profile image
Alwaz Qazi

Thank you for sharing more info with your article @jorensm, it's very well written!

Collapse
 
jorensm profile image
JorensM

Thank you!

Collapse
 
tecate profile image
Eli Summers

cool topic! Thanks for the succint explanation!