🌐
W3Schools
w3schools.com › react › react_useeffect.asp
React useEffect Hooks
Use setTimeout() to count 1 second after initial render: import { useState, useEffect } from 'react'; import { createRoot } from 'react-dom/client'; function Timer() { const [count, setCount] = useState(0); useEffect(() => { setTimeout(() => ...
🌐
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.
Discussions

What’s the difference between useState and useEffect?
I have seen these two new concepts introduced in react v16. ... To put it simply, both useState and useEffect enhance functional components to make them do things that classes can but functional components (without hooks) cannot: useState allows functional components to have state, like this.state in class components. useEffect allows functional components to have lifecycle methods (such as componentDidMount, componentDidUpdate and componentWillUnmount) in one single API. Refer to the examples ... More on stackoverflow.com
🌐 stackoverflow.com
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
I feel like 90% of React tutorials are useState and useEffect.
Because you’ll be using it often. Both useState and useEffect are the probably the most used hooks in the React ecosystem. If you want to expand your knowledge past that just read the documentation. It’s well written and interactive so you code along. More on reddit.com
🌐 r/react
82
250
February 3, 2025
useMemo to replace useState and useEffect
The latter. https://react.dev/learn/you-might-not-need-an-effect is a great read touching on this sort of thing. More on reddit.com
🌐 r/reactjs
29
58
April 28, 2023
🌐
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: ...
🌐
Medium
srhussain99.medium.com › react-hooks-understanding-usestate-and-useeffect-with-simple-practical-examples-d137d6fa884d
React Hooks : Understanding useState and useEffect with simple practical examples | by Rahil Shaikh | Medium
September 22, 2022 - We then implement useEffect function, notice how we have passed marks inside array as an argument in useEffect functions to trigger useEffect only when the marks change and called getResult function to set the result as Pass if marks is greater than 35 and fail if it is less than 35 using setResult function, then we have simply displayed the result in react JSX. Now try running the app and enter marks less than 35 , result will be displayed as Fail and when marks entered is greater than 35 result will be Pass · Hope This helped you understand useState and useEffect Hooks, If you liked the blog give some claps👏 below and share with your friends!!
🌐
Syncfusion
syncfusion.com › blogs › react › understanding react’s useeffect and usestate hooks
Understanding React’s useEffect and useState Hooks | Syncfusion Blogs
November 6, 2025 - You can fetch API data only when the component mounts by using the React useState and useEffect Hooks. It is not permitted to use async directly for the useEffect function. Instead, you can define and then invoke an async function within the useEffect function. See the possibilities for yourself with live demos of Syncfusion React components. ... Refer to the following code example.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › difference-between-usestate-and-useeffect-hook-in-reactjs
Difference Between useState and useEffect Hook in ReactJS - GeeksforGeeks
July 23, 2025 - When the button is clicked ...se</button> </div> ); } export default Counter; ... useEffect is the hook that allows you to perform side effects in functional components....
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-the-usestate-and-useeffect-hooks-in-your-project
React Hooks – How to Use the useState & useEffect Hooks in Your Project
October 8, 2024 - From the example above, the useEffect hook is executed conditionally dependent on the value of the isMounted state variable. Clicking the button causes the value of isMounted to alternate, either activating or deactivating the effect depending on the updated value. This is deemed appropriate because the Hook is invoked at the highest level of the component and its condition is determined by the overarching logic within the component. The React useState ...
🌐
SheCodes
shecodes.io › athena › 125174-difference-between-usestate-and-useeffect-in-react
[React] - Difference between useState and useEffect in | SheCodes
Learn about the difference between useState and useEffect hooks in React, and how they are used for state management and side effects.
Find elsewhere
🌐
Medium
medium.com › @manojitdas180306 › understanding-the-differences-between-usestate-and-useeffect-in-react-5296ce78d574
Understanding the Differences Between useState and useEffect in React | by Manojit Das | Medium
July 9, 2024 - In this example, useState initializes a count state variable with a value of 0. The setCount function is used to update this state, which triggers a re-render of the component with the new state value.
🌐
Codedamn
codedamn.com › news › react js
Mastering the Use of useState and useEffect Hooks in React.js
May 7, 2023 - In this example, we set up an event listener for the window's resize event within the useEffect hook. To clean up the side effect, we return a function that removes the event listener.
🌐
DEV Community
dev.to › enitschorn › react-s-useeffect-usestate-hooks-55fe
React's useEffect & useState hooks - DEV Community
May 12, 2021 - In a very simple way, useState lets you add React state to function components (like setState for class components). A little tip when using this hook: split state into multiple state variables based on which values tend to change together (especially helpful when dealing with objects or arrays) and use it for simple state management. If things get more complex in the way you manage state, there are other tools for that. While I didn't find useState as complex as useEffect, there are some important characteristics to keep in mind when working with it:
🌐
Medium
medium.com › @loons.create › react-hooks-how-to-use-usestate-and-useeffect-example-914c161df34d
React Hooks — How To Use useState and useEffect Example | by Asbar Ali | Medium
January 31, 2022 - Instead of react class, Implemented the JavaScript function and named as Counter. Inside there, I initialized state hook as we defined earlier. setCount variable is simply used to mutate the count state variable.
Top answer
1 of 3
83

To put it simply, both useState and useEffect enhance functional components to make them do things that classes can but functional components (without hooks) cannot:

  • useState allows functional components to have state, like this.state in class components.
  • useEffect allows functional components to have lifecycle methods (such as componentDidMount, componentDidUpdate and componentWillUnmount) in one single API.

Refer to the examples below for further illustration:

useState

class CounterClass extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 1 };
  }
  
  render() {
    return <div>
      <p>Count: {this.state.count}</p>
      <button onClick={() => this.setState({ 
        count: this.state.count + 1
      })}>Increase</button>
    </div>;
  }
}

function CounterFunction() {
  const [count, setCount] = React.useState(1);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => 
        setCount(count + 1)}
      >Increase</button>
    </div>
  );
}

ReactDOM.render(
  <div>
    <CounterClass />
    <CounterFunction />
  </div>
, document.querySelector('#app'));
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="app"></div>

useEffect

class LifecycleClass extends React.Component {
  componentDidMount() {
    console.log('Mounted');
  }
  
  componentWillUnmount() {
    console.log('Will unmount');
  }
  
  render() {
    return <div>Lifecycle Class</div>;
  }
}

function LifecycleFunction() {
  React.useEffect(() => {
    console.log('Mounted');
    return () => {
      console.log('Will unmount');
    };
  }, []); // Empty array means to only run once on mount.
  return (
    <div>Lifecycle Function</div>
  );
}

ReactDOM.render(
  <div>
    <LifecycleClass />
    <LifecycleFunction />
  </div>
, document.querySelector('#app'));
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="app"></div>

Read more about useState and useEffect on the official React docs.

2 of 3
29

For useState()

First, we have the functional component which did not supported state, in other words, a functional component is a stateless component.

Now, with Hooks, we have the functional component but stateful. It is achieved by using useState.


For useEffect()

First, with stateless functional component, we didn't have component lifecycle hooks. In other words, whenever you want to use component lifecycle hooks, you should consider using class component.

Now, we are able to use component lifecycle hooks without using class component. It is achieved by using useEffect. In other words, now whenever we want to use component lifecycle hooks, we already have two options either using class component or using Hooks with useEffect.


UPDATE

what’s the exact difference between useState and useEffect?

In simple words, useState allows our functional components which used to be stateless become stateful. And useEffect allows our functional components leverage the component lifecycle hooks which were, in the past, only supported for class components.

🌐
freeCodeCamp
freecodecamp.org › news › react-hooks-useeffect-usestate-and-usecontext
How to Use React Hooks – useEffect, useState, and useContext Code Examples
December 4, 2023 - The cleanup function returned by useEffect clears the interval when the component is unmounted. The useContext hook is used to consume values from a React context. Context provides a way to pass data through the component tree without having ...
🌐
DEV Community
dev.to › sreashi › usestate-and-useeffect-in-react-287c
useState and useEffect in React - DEV Community
May 29, 2021 - ... The useState hook is a function that takes one argument, which is the initial state, and it returns two values: the current state and a function that can be used to update the state.
🌐
React
react.dev › learn › you-might-not-need-an-effect
You Might Not Need an Effect – React
Simplify this component by removing all the unnecessary state and Effects. ... 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 ?
🌐
DEV Community
dev.to › wdp › best-practices-for-usestate-and-useeffect-in-react-4aea
Best Practices for useState and useEffect in React - DEV Community
January 18, 2024 - Let's clarify this with an example. Imagine you have a list of fruits and you want to show only the ones that match what the user types in a search box. You might think about using useEffect like this: import React, { useState, useEffect } from ...
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › fetching-data-from-an-api-with-useeffect-and-usestate-hook
Fetching Data Using useEffect and useState Hook - GeeksforGeeks
August 12, 2025 - useEffect → runs code when the component first loads (or when certain values change), perfect for calling an API. In this example, we'll create a React component called RandomUserData that fetches random user data from the Random Data API.
🌐
Ais
ais.com › home › blog › hooked on react: usestate and useeffect
Hooked on React: useState and useEffect - Applied Information Sciences
August 28, 2023 - The variable will change from the useState, and will cause the useEffect to run. A couple of things to note about the useEffect are that; the first time it will run is when the component that’s using it is mounted. And if the array is empty, then useEffect will only run once. One thing that makes the useEffect tricky is when calling a function in useEffect.
🌐
DEV Community
dev.to › keoshaug › react-usestate-and-useeffect-55fk
React: useState and useEffect - DEV Community
October 11, 2023 - Each component maintains its own state but only sets the initial state for its children.[4] In React, there is a clear basic hierarchy of parent component(s) and children. The parents pass props - properties - to the children. Props cannot be changed by the components themselves but are passed to children by parent components. ... In order to actually use useState, it has to be imported into the component in which it is being used, as show above. useState will be imported in parentheses (so will useEffect, as we shall see).