7 useEffect in React JS With Real Examples, Mistakes & Fixes

This guide breaks down useEffect in React JS examples with real scenarios, mistakes, and best practices for scalable apps.
Diagram explaining React useEffect with real examples, mistakes, and fixes by Enstacked

Introduction

What is useeffect in React with Example?

React useEffect hook illustration showing useEffect syntax and component lifecycle behavior

Why do we use useEffect in ReactJS?

Concept image representing confusion and common questions around React useEffect hook
      // Incorrect approach
function Dashboard() {
  fetch("/api/stats");
  return <h1>Dashboard</h1>;
}

    
      // Correct approach
useEffect(() => {
  fetch("/api/stats");
}, []);

    

Is useEffect good or bad?

Balanced scale showing good vs bad React useEffect usage and best practices

When useEffect is good?

When useEffect becomes a problem?

      // Unnecessary useEffect
useEffect(() => {
  setTotal(price * quantity);
}, [price, quantity]);

    

Why developers say useEffect is bad

Best ReactJS useEffect example you should know about!

List of common React useEffect examples including data fetching, localStorage sync, and cleanup
      useEffect(() => {
  async function fetchData() {
    const res = await fetch("/api/products");
    const data = await res.json();
    setProducts(data);
  }

  fetchData();
}, []);

    
      useEffect(() => {
  async function fetchUser() {
    const res = await fetch(`/api/users/${userId}`);
    const data = await res.json();
    setUser(data);
  }

  fetchUser();
}, [userId]);

    
      useEffect(() => {
  localStorage.setItem("theme", theme);
}, [theme]);

    
      useEffect(() => {
  document.title = `Orders (${count})`;
}, [count]);

    
      useEffect(() => {
  function handleResize() {
    setWidth(window.innerWidth);
  }

  window.addEventListener("resize", handleResize);

  return () => {
    window.removeEventListener("resize", handleResize);
  };
}, []);

    
      useEffect(() => {
  const timer = setInterval(() => {
    setTime(prev => prev + 1);
  }, 1000);

  return () => clearInterval(timer);
}, []);

    
      useEffect(() => {
  if (!isLoggedIn) return;

  fetch("/api/dashboard");
}, [isLoggedIn]);

    

Final Thoughts

Frequently Asked Questions(FAQs) 

useEffect is neither good nor bad. It is essential for side effects but becomes problematic when used to manage core UI logic or derive state from state. Intentional use makes it powerful. Habitual use makes it fragile.

Use useEffect only when your logic:

  • Interacts with something outside React
  • Needs to run after render
  • Has clearly defined dependencies
  • Includes cleanup if required

If logic can be handled during render or via event handlers, it should not be inside useEffect.

No. useEffect itself is not the side effect. It is the mechanism React provides to run side effects after rendering. The code inside the effect performs the side effect.

How many times will useEffect run?

  • No dependency array → runs after every render
  • Empty dependency array ([]) → runs once after initial render
  • With dependencies → runs whenever any dependency changes
  • Cleanup function → runs before the next effect or on unmount

Understanding this behavior prevents most useEffect bugs.

Contact Us

Let's talk business!

We're happy to answer any questions you may have and
help you determine which of our services best fit your needs.

What happens next?

  • 1.We Schedule a call at your convenience.
  • 2.We do a discovery and consulting meting.
  • 3.We prepare a proposal.

Schedule a Free Consultation

Services

  • Hire Developers
  • Web Development
  • Full Stack
  • Mobile App Development
  • UI/UX Design
  • Maintenance & Support
  • Customer Software Development
  • QA Service
  • Digital Marketing
  • Code Audit
  • Other Services