Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

React useEffect Hook Explained for Beginners (With Examples)

Posted on April 11, 2025 • 6 min read • 1,131 words
Share via
Mapagam
Link copied to clipboard

Learn how to use the React useEffect hook with examples. Master side effects, data fetching, and component lifecycle in React for beginners.

On this page
1. What is the useEffect Hook? 2. Basic Syntax of useEffect 2.1 Example: Basic useEffect 3. The Role of Dependencies in useEffect 3.1 Effect Without Dependencies 3.2 Effect With an Empty Dependencies Array 3.3 Effect With Specific Dependencies 4. Common Use Cases for useEffect 4.1 Data Fetching 4.2 Cleanup Operations 4.3 Event Listeners 5. Conditional Effects in useEffect 5.1 Example: Conditional Side Effects 6. Conclusion

React is a powerful JavaScript library that simplifies the process of building dynamic, interactive user interfaces. One of the core features of React is the ability to handle side effects in functional components, and the useEffect hook plays a critical role in this.

1. What is the useEffect Hook?

The useEffect hook is a function that allows you to perform side effects in functional React components. Side effects are operations that don’t directly affect the rendering of the UI but are necessary for the application to function properly. These can include tasks like data fetching, subscribing to events, timers, and manually manipulating the DOM.

Before the introduction of hooks in React 16.8, side effects in React were handled in class components using lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount. With the useEffect hook, developers can now handle these side effects in functional components, making functional components more powerful.

2. Basic Syntax of useEffect

The basic syntax of the useEffect hook looks like this:

useEffect(() => {
  // Code to perform side effect here
}, [dependencies]);
  • Callback Function: The first argument is a function that contains the code to perform the side effect.
  • Dependencies Array: The second argument is an array of dependencies. This array controls when the side effect should run.

2.1 Example: Basic useEffect

Here’s a simple example of how to use useEffect in a functional component:

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

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

  useEffect(() => {
    console.log("useEffect has been triggered!");
    document.title = `You clicked ${count} times`;
  }, [count]);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

In this example, every time the count state changes, the useEffect hook is triggered, and the document title is updated accordingly.

3. The Role of Dependencies in useEffect

One of the most important aspects of the useEffect hook is the dependencies array. This array helps determine when the effect should run.

3.1 Effect Without Dependencies

If you omit the second argument, useEffect will run after every render, regardless of whether any state or props have changed. This is similar to componentDidUpdate in class components.

Example:

useEffect(() => {
  console.log("This runs after every render!");
});

3.2 Effect With an Empty Dependencies Array

If you pass an empty array [] as the second argument, the effect will only run once when the component mounts, and it will not run again on subsequent renders. This is similar to componentDidMount in class components.

Example:

useEffect(() => {
  console.log("This runs only once when the component mounts!");
}, []);

3.3 Effect With Specific Dependencies

When you pass specific state or props values in the dependencies array, the effect will only run when those values change. This allows you to optimize when the side effect should trigger.

Example:

useEffect(() => {
  console.log("This runs when 'count' changes!");
}, [count]);

4. Common Use Cases for useEffect

4.1 Data Fetching

One of the most common use cases for the useEffect hook is to fetch data from an API when the component mounts. Let’s walk through a simple example that fetches data from a public API.

Example:

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

function DataFetcher() {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/posts')
      .then((response) => response.json())
      .then((jsonData) => {
        setData(jsonData);
        setLoading(false);
      })
      .catch((error) => console.error('Error fetching data:', error));
  }, []); // Empty array means it only runs once on mount

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <h1>Fetched Data</h1>
      <ul>
        {data.map((post) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
    </div>
  );
}

In this example, useEffect is used to fetch data from an API when the component mounts. The empty dependencies array [] ensures that the data is fetched only once when the component is first rendered.

4.2 Cleanup Operations

Another key feature of useEffect is that it can handle cleanup operations. You can return a cleanup function inside the effect, which will be executed when the component unmounts or when the effect is triggered again (if dependencies change).

For example, cleaning up a timer:

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

function Timer() {
  const [seconds, setSeconds] = useState(0);

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

    // Cleanup function to clear the timer when the component unmounts
    return () => clearInterval(timer);
  }, []); // Empty array means it runs only once on mount

  return <div>Time: {seconds} seconds</div>;
}

In this example, useEffect starts a timer when the component mounts. The cleanup function clears the timer when the component unmounts, preventing memory leaks.

4.3 Event Listeners

You can also use useEffect to add and remove event listeners. For example, if you need to listen for window resize events, useEffect makes it easy.

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

function WindowSize() {
  const [size, setSize] = useState({ width: window.innerWidth, height: window.innerHeight });

  useEffect(() => {
    const handleResize = () => {
      setSize({ width: window.innerWidth, height: window.innerHeight });
    };

    window.addEventListener('resize', handleResize);

    // Cleanup event listener
    return () => {
      window.removeEventListener('resize', handleResize);
    };
  }, []); // Runs only once when the component mounts

  return (
    <div>
      <p>Width: {size.width}</p>
      <p>Height: {size.height}</p>
    </div>
  );
}

This example listens for resize events and updates the component state whenever the window size changes. The event listener is removed when the component unmounts, preventing potential memory leaks.

5. Conditional Effects in useEffect

Sometimes, you may want to conditionally trigger side effects. You can use the dependencies array to control which states or props trigger the effect. You can also conditionally set dependencies within the useEffect callback.

5.1 Example: Conditional Side Effects

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

function ConditionalEffect() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [message, setMessage] = useState('');

  useEffect(() => {
    if (isLoggedIn) {
      setMessage('Welcome back!');
    } else {
      setMessage('Please log in');
    }
  }, [isLoggedIn]); // The effect runs when 'isLoggedIn' changes

  return (
    <div>
      <p>{message}</p>
      <button onClick={() => setIsLoggedIn(!isLoggedIn)}>Toggle Login</button>
    </div>
  );
}

In this example, the side effect (updating the message) only occurs when the isLoggedIn state changes.

6. Conclusion

The useEffect hook is one of the most important and versatile hooks in React. It allows you to perform side effects like data fetching, timers, subscriptions, and more, all while keeping your components clean and efficient. By understanding how useEffect works and how to properly manage dependencies, you can create better React applications.

To summarize:

  • useEffect is used to handle side effects in React functional components.
  • The hook takes two arguments: a callback function and a dependency array.
  • It can be used for a variety of tasks, including data fetching, event handling, and cleanup.
  • Dependencies control when the effect runs, allowing for optimization and preventing unnecessary side effects.

By mastering useEffect, you will be able to write more maintainable, efficient, and reactive React applications.

React   UseEffect Hook   React Hooks   Side Effects in React   React Beginners Guide  
React   UseEffect Hook   React Hooks   Side Effects in React   React Beginners Guide  
 How to Create Your First React Component (With Example Code)
How to Use the useState Hook in React (With Simple Examples) 

More Reading!

  1. Beginner’s Guide to JavaScript Functions (With Best Practices)
  2. Understanding useState in React: A Beginner-Friendly Guide
  3. Understanding JavaScript Hoisting Without Confusion
  4. React State vs. Props: Understanding the Key Differences
  5. React Lists and Keys Explained (Why You Should Use Them)
On this page:
1. What is the useEffect Hook? 2. Basic Syntax of useEffect 2.1 Example: Basic useEffect 3. The Role of Dependencies in useEffect 3.1 Effect Without Dependencies 3.2 Effect With an Empty Dependencies Array 3.3 Effect With Specific Dependencies 4. Common Use Cases for useEffect 4.1 Data Fetching 4.2 Cleanup Operations 4.3 Event Listeners 5. Conditional Effects in useEffect 5.1 Example: Conditional Side Effects 6. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard