I shall try to explain it in simple terms. useEffect is one of the most important hooks in react and is a way to handle life cycle of the component in which it is present.

useEffect runs on every render of the component (i.e when a state variable changes) and can also run every time a specific value changes that is mentioned in it's dependency array.

useEffect also provides a very useful cleanup function which can be used to remove any active listeners when the component changes

Here are a few examples:

  1. useEffect without dependency array
useEffect(() => {
    /*the code you want to run has to be in here which will keep running again and 
    again without stopping*/
})
  1. useEffect with empty dependency array
useEffect(() => {
    /*the code you want to run on every render has to be in here, the empty [] means 
    that the code will run every time this component mounts*/
},[])
  1. useEffect with state variables in dependency array
useEffect(() => {
    /*the code you want to run on every render has to be in here, the dependency
    array with state means that the code will run every time this component mounts as
    well as when these state variables change and the value will be captured by the
    useEffect*/
},[state1,state2])
  1. useEffect with state variables in dependency array and cleanup
useEffect(() => {
    /*the code you want to run on every render has to be in here, the dependency
    array with state means that the code will run every time this component mounts as
    well as when these state variables change and the value will be captured by the
    useEffect*/


    /*cleanup is used to remove any unwanted loops, intervals, listeners when the 
    component unmounts*/
    return () => console.log("clean up");
},[state1,state2])
  1. A complete example for useEffect from w3schools
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";

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

  useEffect(() => {
    let timer = setTimeout(() => {
    setCount((count) => count + 1);
  }, 1000);

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

  return <h1>I've rendered {count} times!</h1>;
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);

If you still don't understand have a look at this

w3schools link for useEffect

react official documentation for useEffect

Answer from Tostiffent on Stack Overflow
🌐
React
react.dev › reference › react › useEffect
useEffect – React
In the below example, serverUrl and roomId are reactive values, so they both must be specified as dependencies. As a result, selecting a different room in the dropdown or editing the server URL input causes the chat to re-connect.
🌐
W3Schools
w3schools.com › react › react_useeffect.asp
React useEffect Hooks
React Compiler React Quiz React ... side effects in your components. Some examples of side effects are: fetching data, directly updating the DOM, and timers....
Discussions

What is useEffect in react [duplicate]
I recently started learning React, and I learn the useState hook, however I'm struggling to learn the useEffect hook. I've looked at the documentation searched it up and also watched a video about ... More on stackoverflow.com
🌐 stackoverflow.com
[Noob] are useEffect hooks really that bad??
As other answers have said, don’t avoid them - just use them with intention. This is a fantastic, official resource for knowing when to avoid using one: https://react.dev/learn/you-might-not-need-an-effect As the above dives into, it’s not just for performance. I’ve seen plenty of bugs and layout flickers that have resulted from incorrect uses of useEffect, particularly when state is changed inside the effect callback. More on reddit.com
🌐 r/reactjs
68
121
February 10, 2025
Thoughts on avoiding useEffect/useState when possible in React?
Not sure about what you're asking here, but this isn't a new discovery. There is an entire page on the official new React docs dedicated to teaching developers to rely on useEffect only as an "escape hatch" due to its tendency to be a code smell https://react.dev/learn/you-might-not-need-an-effect Also, last year's "Goodbye, useEffect: David Khourshid" conference talk basically outlined most of your discoveries, emphasizing to rely on events + callbacks over effects for cleaner code. Which unsurprisingly, many people took issue with (nobody likes hearing years of react code they've built is wrong) https://www.youtube.com/watch?v=HPoC-k7Rxwo So when should you use an Effect then? Treat is as a synchronization tool. Or more specifically, one you are forced to use to synchronize some piece of React code with some piece of external or third party system that does not offer an event-driven or "react-friendly" way of doing things. More on reddit.com
🌐 r/reactjs
57
58
November 7, 2023
Common useEffect anti-patterns I see in code reviews (and how to fix them)
Just refer [this]( https://react.dev/learn/you-might-not-need-an-effect ) and share with your team mates. Probably give them a lunch and learn session. More on reddit.com
🌐 r/reactjs
45
110
December 15, 2025
🌐
React
legacy.reactjs.org › docs › hooks-effect.html
Using the Effect Hook – React
The Effect Hook lets you perform side effects in function components: import React, { useState, useEffect } from 'react'; function Example() { const [count, setCount] = useState(0); // Similar to componentDidMount and componentDidUpdate: ...
🌐
Overreacted
overreacted.io › a-complete-guide-to-useeffect
A Complete Guide to useEffect — overreacted
But before we jump to solutions, let’s understand the problem better. If deps contain every value used by the effect, React knows when to re-run it: useEffect(() => { document.title = 'Hello, ' + name; }, [name]);
🌐
React
react.dev › learn › you-might-not-need-an-effect
You Might Not Need an Effect – React
import { useState, useEffect } from 'react'; import { initialTodos, createTodo } from './todos.js'; export default function TodoList() { const [todos, setTodos] = useState(initialTodos); const [showActive, setShowActive] = useState(false); const [activeTodos, setActiveTodos] = useState([]); const [visibleTodos, setVisibleTodos] = useState([]); const [footer, setFooter] = useState(null); useEffect(() => { setActiveTodos(todos.filter(todo => !todo.completed)); }, [todos]); useEffect(() => { setVisibleTodos(showActive ?
Top answer
1 of 2
14

I shall try to explain it in simple terms. useEffect is one of the most important hooks in react and is a way to handle life cycle of the component in which it is present.

useEffect runs on every render of the component (i.e when a state variable changes) and can also run every time a specific value changes that is mentioned in it's dependency array.

useEffect also provides a very useful cleanup function which can be used to remove any active listeners when the component changes

Here are a few examples:

  1. useEffect without dependency array
useEffect(() => {
    /*the code you want to run has to be in here which will keep running again and 
    again without stopping*/
})
  1. useEffect with empty dependency array
useEffect(() => {
    /*the code you want to run on every render has to be in here, the empty [] means 
    that the code will run every time this component mounts*/
},[])
  1. useEffect with state variables in dependency array
useEffect(() => {
    /*the code you want to run on every render has to be in here, the dependency
    array with state means that the code will run every time this component mounts as
    well as when these state variables change and the value will be captured by the
    useEffect*/
},[state1,state2])
  1. useEffect with state variables in dependency array and cleanup
useEffect(() => {
    /*the code you want to run on every render has to be in here, the dependency
    array with state means that the code will run every time this component mounts as
    well as when these state variables change and the value will be captured by the
    useEffect*/


    /*cleanup is used to remove any unwanted loops, intervals, listeners when the 
    component unmounts*/
    return () => console.log("clean up");
},[state1,state2])
  1. A complete example for useEffect from w3schools
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";

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

  useEffect(() => {
    let timer = setTimeout(() => {
    setCount((count) => count + 1);
  }, 1000);

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

  return <h1>I've rendered {count} times!</h1>;
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);

If you still don't understand have a look at this

w3schools link for useEffect

react official documentation for useEffect

2 of 2
12

To understand useEffect, you have to understand React’s three lifestyle methods: Mounting, when the component is first render; Updating, when the component or state changes; and lastly, Unmounting.

useEffect as it says in the name defines a function (an effect) which will run at one of the three points. To differentiate between the three. You have a dependency array which defines when your effect will run.

For when the first page renders you would use an empty dependency array. For when a state changes you would use a dependency array which would look like this [state] and for when the component unmounts you would have an empty dependency array which would have a return statement in it which would run when it unmounts.

It would look like this:

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

useEffect(() => {}, [state])

useEffect(() => {return () = {}}, [])

🌐
Hygraph
hygraph.com › blog › react-useeffect-a-complete-guide
React useEffect() - A complete guide | Hygraph
January 21, 2026 - ... Let's look at a practical example of using useEffect to fetch blog posts from Hygraph a headless CMS that leverages GraphQL to serve content to your applications into a React application.
Find elsewhere
🌐
Dave Ceddia
daveceddia.com › useeffect-hook-examples
How the useEffect Hook Works (with Examples)
October 22, 2020 - If you’ve never touched classes and never intend to, you can disregard the comparison to lifecycles – but this section will still be useful for learning when useEffect runs and how it fits into React’s render cycle. Let’s look at how to run code after a component mounts (componentDidMount), after it re-renders (componentDidUpdate), and before it unmounts (componentWillUnmount). import React, { useEffect, useState } from 'react'; import ReactDOM from 'react-dom'; function LifecycleDemo() { // Pass useEffect a function useEffect(() => { // This gets called after every render, by default // (the first one, and every one after that) console.log('render!'); // If you want to implement componentWillUnmount, // return a function from here, and React will call // it prior to unmounting.
🌐
React
legacy.reactjs.org › docs › hooks-overview.html
Hooks at a Glance – React
Earlier on this page, we introduced a FriendStatus component that calls the useState and useEffect Hooks to subscribe to a friend’s online status. Let’s say we also want to reuse this subscription logic in another component. First, we’ll extract this logic into a custom Hook called useFriendStatus: import React, { useState, useEffect } from 'react'; function useFriendStatus(friendID) { const [isOnline, setIsOnline] = useState(null); function handleStatusChange(status) { setIsOnline(status.isOnline); } useEffect(() => { ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange); return () => { ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange); }; }); return isOnline; }
🌐
LogRocket
blog.logrocket.com › home › how to use the useeffect hook in react: a complete guide
How to use the useEffect hook in React: A complete guide - LogRocket Blog
June 3, 2025 - Learn how to use the useEffect Hook in React to manage side effects, handle async tasks, and avoid common mistakes with real-world examples.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › reactjs-useeffect-hook
React useEffect Hook %%page%% %%sep%% %%sitename%% - GeeksforGeeks
Initial Render Happens: React renders the component and updates the DOM. useEffect Executes After Render: It runs after the paint, not during render.
Published   5 days ago
🌐
React
react.dev › reference › react › useEffectEvent
useEffectEvent – React
A common use case for useEffectEvent ... a value you don’t want to react to. In this example, a chat component connects to a room and shows a notification when connected....
🌐
Dead Simple Chat
deadsimplechat.com › blog › react-useeffect-a-complete-guide-with-examples
React useEffect: A complete guide with examples
November 30, 2023 - Normally you can have event listeners ... setPosition] = useState({ x: 0, y: 0 }); useEffect(() => { function handleMove(e) { setPosition({ x: e.clientX, y: e.clientY }); } window.addEventListe...
🌐
DEV Community
dev.to › colocodes › 6-use-cases-of-the-useeffect-reactjs-hook-282o
6 use cases of the useEffect ReactJS hook - DEV Community
September 18, 2021 - In this example, useEffect is used to fetch new data from an API every 3 seconds. The child component useEffect receives the time as dependency and every time that dependency changes, a new fetch() is triggered.
🌐
Strapi
strapi.io › blog › what-is-react-useeffect-hook-complete-guide
What Is React useEffect Hook? Complete Guide and Examples
July 22, 2025 - Data fetching is the most common side effect in React applications. The hook runs after the DOM paints, so network latency never blocks the initial render, and the dependency array ensures requests fire only when needed. Since the effect callback can't be async, wrap your await logic in an inner function and call it immediately. import { useState, useEffect } from 'react'; function UsersList({ url }) { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const abortController = new AbortController(); c
🌐
freeCodeCamp
freecodecamp.org › news › react-hooks-useeffect-usestate-and-usecontext
How to Use React Hooks – useEffect, useState, and useContext Code Examples
December 4, 2023 - In this example, the UserProfile component uses the useAuth hook to check the user's authentication status. If authenticated, it fetches the user data and displays a personalized welcome message. If not authenticated, it prompts the user to log in. React hooks, including useState, useEffect, and useContext, have transformed the way developers write components by providing a more intuitive and flexible approach to managing state and side effects.
🌐
freeCodeCamp
freecodecamp.org › news › react-useeffect-absolute-beginners
The React useEffect Hook for Absolute Beginners
March 1, 2022 - This is why useEffect exists: to ... For example, if we wanted to change the title meta tag to display the user's name in their browser tab, we could do it within the component itself, but we shouldn't....
🌐
Linguine Code
linguinecode.com › home › blog › react useeffect hook with code examples
React useEffect hook with code examples
July 16, 2020 - By running an empty array [] as a second argument, you’re letting React know that your useEffect function doesn’t depend on any values from props or state. This will help you avoid the componentDidUpdate lifecycle. I showed an example how to avoid a trigger from a componentDidUpdate lifecycle with useEffect.