codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

21 / 61 topics
18Context API Basics19Hooks: Introduction and Overview20Using the useState Hook21Using the useEffect Hook22Creating Custom Hooks23Memoization in React
Tutorials/React.js/Using the useEffect Hook
⚛️React.js

Using the useEffect Hook

Updated 2026-04-20
3 min read

Using the useEffect Hook

Introduction

In React, useEffect is a hook that allows you to perform side effects in function components. Side effects are operations that affect the outside world, such as data fetching, subscriptions, or manually changing the DOM. The useEffect hook provides a way to encapsulate these side effects within your functional components.

This tutorial will cover how to use the useEffect hook effectively, including its syntax, common use cases, and best practices for optimizing performance.

Basic Syntax

The basic syntax of the useEffect hook is as follows:

import React, { useEffect } from 'react';

function MyComponent() {
  useEffect(() => {
    // Code to run after every render
    console.log('Component rendered');

    // Optional cleanup function
    return () => {
      console.log('Cleanup');
    };
  }, []); // Dependency array

  return <div>My Component</div>;
}

Parameters

  • Effect Function: This is the function where you place your side effect logic. It will run after every render by default.
  • Dependency Array: An optional array of values that the effect depends on. If any value in this array changes, the effect will re-run.

Common Use Cases

1. Data Fetching

One of the most common use cases for useEffect is fetching data from an API. Here’s how you can do it:

import React, { useState, useEffect } from 'react';

function DataFetchingComponent() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => setData(data))
      .catch(error => console.error('Error fetching data:', error));
  }, []); // Empty dependency array means this effect runs only once after the initial render

  return (
    <div>
      {data ? <pre>{JSON.stringify(data, null, 2)}</pre> : 'Loading...'}
    </div>
  );
}

2. Subscriptions

If you need to subscribe to an external data source or event, useEffect is perfect for that:

import React, { useState, useEffect } from 'react';

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

  useEffect(() => {
    const intervalId = setInterval(() => {
      setCount(prevCount => prevCount + 1);
    }, 1000);

    // Cleanup function to clear the interval
    return () => clearInterval(intervalId);
  }, []); // Empty dependency array

  return <div>Count: {count}</div>;
}

3. DOM Manipulation

Sometimes you need to manually manipulate the DOM, which can be done inside an effect:

import React, { useEffect } from 'react';

function DomManipulationComponent() {
  useEffect(() => {
    const element = document.getElementById('myElement');
    if (element) {
      element.style.color = 'red';
    }
  }, []); // Empty dependency array

  return <div id="myElement">This text will be red</div>;
}

Dependency Array

The dependency array is a crucial part of useEffect. It controls when the effect runs:

  • Empty Array ([]): The effect runs only once after the initial render, similar to componentDidMount in class components.

    useEffect(() => {
      console.log('Runs only once');
    }, []);
    
  • No Dependency Array: The effect runs after every render, similar to componentDidUpdate.

    useEffect(() => {
      console.log('Runs on every render');
    });
    
  • With Dependencies: The effect runs whenever the values in the dependency array change.

    useEffect(() => {
      console.log('Runs when count changes', count);
    }, [count]);
    

Cleanup Functions

The cleanup function is optional but highly recommended for effects that create subscriptions or timers. It ensures that resources are properly released when the component unmounts or before the effect re-runs.

useEffect(() => {
  const subscription = someAPI.subscribe();

  return () => {
    subscription.unsubscribe();
  };
}, []);

Best Practices

  1. Avoid Dependencies: Only include values in the dependency array that are actually used inside the effect. This helps prevent unnecessary re-renders.

  2. Use Functional Updates: When updating state based on previous state, use functional updates to avoid stale state issues.

    useEffect(() => {
      setCount(prevCount => prevCount + 1);
    }, []);
    
  3. Optimize Performance: For complex effects, consider using useCallback or useMemo to prevent unnecessary re-renders of child components.

  4. Error Handling: Always handle errors in your effect logic to avoid unhandled promise rejections.

  5. Debugging: Use console logs or debugging tools to understand when and why your effects are running.

Conclusion

The useEffect hook is a powerful tool for managing side effects in React function components. By understanding its syntax, common use cases, and best practices, you can write more efficient and maintainable code. Remember to always consider the dependency array carefully and provide cleanup functions when necessary to avoid memory leaks and unexpected behavior.

By following this comprehensive guide, you should now have a solid grasp of how to use useEffect in your React applications.


PreviousUsing the useState HookNext Creating Custom Hooks

Recommended Gear

Using the useState HookCreating Custom Hooks