Blog/NotesConcept

Common Pitfalls of useEffect Hook: Must Know for React Devs?

Understand common pitfalls of the useEffect hook when implementing asynchronous operations in React applications effectively.

Intermediate

Anuj Sharma

Last Updated Jun 24, 2026


Common Pitfalls of useEffect Hook: Must Know for React Devs?

The useEffect hook in React is a powerful tool for managing side effects in functional components. While it provides a clean way to perform actions after render, there are common pitfalls that developers might encounter when dealing with asynchronous operations.

In this blog post, we will explore these pitfalls and discuss strategies to avoid them.

7 Common Pitfalls of the useEffect Hook (with Examples)

The useEffect() Hook is one of the most powerful features in React, but it's also one of the most commonly misused. Here are seven common pitfalls to avoid.

1. Not Handling Cleanup Properly

One common mistake is forgetting to clean up subscriptions, event listeners, or timers when the component unmounts. This can lead to memory leaks and unexpected behavior.

āŒ Incorrect

useEffect(() => {
  const intervalId = setInterval(() => {
    console.log("Running...");
  }, 1000);
}, []);

The interval continues running even after the component is removed.

āœ… Correct

useEffect(() => {
  const intervalId = setInterval(() => {
    console.log("Running...");
  }, 1000);

  return () => {
    clearInterval(intervalId);
  };
}, []);

Example

Suppose you start polling an API every second. Without cleanup, the timer continues to run even after navigating away from the page, leading to memory leaks.

2. Dependency Array Mismanagement

Incorrect dependencies can lead to stale values or unnecessary effect executions.

āŒ Incorrect

useEffect(() => {
  fetchUser(userId);
}, []);

If userId changes, the effect won't re-run.

āœ… Correct

useEffect(() => {
  fetchUser(userId);
}, [userId]);

Example

Imagine a profile page where the selected user changes. Without including userId in the dependency array, the component will continue displaying the old user's information.

3. Infinite Looping

Updating state that is also listed as a dependency can create an infinite loop.

āŒ Incorrect

useEffect(() => {
  setCount(count + 1);
}, [count]);

Example

function App() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(count + 1);
  }, [count]);

  return <h1>{count}</h1>;
}

This effect continuously updates count, causing an infinite re-render loop.

āœ… Correct

useEffect(() => {
  fetchData();
}, []);

Or conditionally update the state:

useEffect(() => {
  if (count < 5) {
    setCount(count + 1);
  }
}, [count]);

4. Race Conditions

Multiple asynchronous requests can complete out of order and overwrite each other's results.

āŒ Incorrect

useEffect(() => {
  fetch(`/users/${userId}`)
    .then(response => response.json())
    .then(data => setUser(data));
}, [userId]);

Example

Suppose a user quickly switches from User 1 to User 2. If the request for User 1 finishes after User 2, it may overwrite the latest data.

āœ… Correct

useEffect(() => {
  let ignore = false;

  async function fetchUser() {
    const response = await fetch(`/users/${userId}`);
    const data = await response.json();

    if (!ignore) {
      setUser(data);
    }
  }

  fetchUser();

  return () => {
    ignore = true;
  };
}, [userId]);

5. Mixing Sync and Async Operations

Combining unrelated logic in one effect makes the code harder to understand and maintain.

āŒ Incorrect

useEffect(() => {
  document.title = title;

  async function fetchPosts() {
    const response = await fetch("/posts");
    const posts = await response.json();

    setPosts(posts);
  }

  fetchPosts();
}, [title]);

Changing the title unnecessarily triggers the API request.

āœ… Correct

Separate concerns into multiple effects.

useEffect(() => {
  document.title = title;
}, [title]);

useEffect(() => {
  async function fetchPosts() {
    const response = await fetch("/posts");
    const posts = await response.json();

    setPosts(posts);
  }

  fetchPosts();
}, []);

Updating the page title should not trigger a network request. Separate effects make the intent clearer.

6. Not Handling Errors

Ignoring errors in asynchronous operations can result in silent failures and poor user experience.

āŒ Incorrect

useEffect(() => {
  fetch("/users")
    .then(response => response.json())
    .then(data => setUsers(data));
}, []);

āœ… Correct

useEffect(() => {
  async function fetchUsers() {
    try {
      const response = await fetch("/users");
      const data = await response.json();

      setUsers(data);
    } catch (error) {
      setError(error.message);
    }
  }

  fetchUsers();
}, []);

Example

If the API server is down, displaying an error message is better than showing an empty screen.

7. Overusing useEffect

Many developers use useEffect for derived state that can be computed directly during rendering.

āŒ Incorrect

const [fullName, setFullName] = useState("");

useEffect(() => {
  setFullName(firstName + " " + lastName);
}, [firstName, lastName]);

āœ… Correct

const fullName = `${firstName} ${lastName}`;

Example

There's no need to maintain another piece of state for fullName because it can be derived from existing state.

Final Thoughts

Understanding and avoiding the common pitfalls of the useEffect hook is crucial for building robust and efficient React applications. By managing cleanup, dependencies, loops, race conditions, and error handling effectively, developers can leverage the full power of useEffect while ensuring a seamless user experience.

Happy Coding :)

Love this Blog? Share it Now!

Help others discover this resource

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  šŸš€


Learn Next

Featured

100+ Top React JS Interview Questions And Answers

20 Most Asked Custom Hooks in React for InterviewsTop 10 React Performance Optimization Techniques25 Top JavaScript Interview Questions for BeginnersHow to create custom useInfiniteScroll Hook in ReactImplement useThrottle Custom Hook In React

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  šŸš€

Share your expertise

Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.

Build your portfolio

Help the community

Sharpen your skills

Earn goodies

Other Related Blogs

useMemo vs useEffect Hooks in React: Difference & Trade-Off

Anuj Sharma

Last Updated Jun 24, 2026

Explore useMemo vs useEffect in React with examples. Learn key differences between useMemo and useEffect, use cases, and when to choose one hook over the other in React applications and interviews.

Difference between React useId Hook and generating IDs using Math.random?

Anuj Sharma

Last Updated Jun 24, 2026

Understand the difference between using useId() vs generating IDs using Math.random() to better know the practical use-case of useId Hook in react applications.

useDeferredValue vs useTransition: Difference and Trade-Off

Anuj Sharma

Last Updated Jun 20, 2026

Explore useDeferredValue vs useTransition in React with examples. Learn key differences between useDeferredValue and useTransition, use cases and when to choose one over the other in React applications.

useRef vs useState in React: Difference & Trade-off

Anuj Sharma

Last Updated Jun 19, 2026

Explore useRef vs useState in React with examples. Learn the key differences between useRef and useState, use cases, advantages, and disadvantages, and when to choose one over the other in React applications.

useRef vs createRef in React: Difference and Trade Off

Anuj Sharma

Last Updated Jun 19, 2026

Explore useRef vs createRef in React with examples. Learn the key differences between useRef and createRef, use cases and when to choose one over the other in React applications

useMemo vs useCallback in React: Difference and Trade Off

Anuj Sharma

Last Updated Jun 19, 2026

Explore useMemo vs useCallback in React with examples. Learn the key differences between useMemo and useCallback, use cases, and when to choose one over the other in React applications.

useState vs useReducer in React: Understand the Difference & Trade-Off

Anuj Sharma

Last Updated Jun 19, 2026

Explore useState vs useReducer in React with examples. Learn key differences, use cases, advantages, disadvantages, and when to choose one over the other in React applications and interviews.

React.memo vs useMemo in React: Difference & Trade Off

Anuj Sharma

Last Updated Jun 19, 2026

Explore React.memo vs useMemo in React with examples. Learn key differences between React.memo and useMemo, use cases and when to choose one over the other in React applications.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewFrontend JobsQuestionsNewInterview ExperienceBlogsToolsLeaderboardFrontendGeek Chrome extensionGet the extension on the Chrome Web Store →

Ā© 2026 FrontendGeek. All rights reserved