How useEffect works in ReactJS ?
Last Updated :
09 Oct, 2024
The useEffect hook in React is used to handle the side effects in the functional components. It allows us to perform operations like data fetching, subscriptions, and manual DOM updates. It runs on every re-render which can be controlled using the dependency array.
Syntax:
useEffect(() => {
// Side effect logic
return () => {
// Optional cleanup function
};
}, [dependencies]);
Parameter:
- Effect function: Function containing the side effect code.
- Dependency array: Contains dependencies to control the effects.
Return:
Optionally returns a cleanup function to clear the resources or processes before unmount and re-render.
How useEffect Works?
useEffect executes the code on every render and asloThe working of useEffect involves 3 steps
Mounting
It is the pahse when the component is added to the DOM, resulting in the first render of the ui. And on mounting, useEffect executes the code defined such as data fetching, animations etc.
Updating
This phase triggers when any od the component state and prop changes and the component is re-rendered. As useEffectr runs on every render it execures the sideeffects. It can be controlled with the dependency array.
Unmounting or Cleanup
This phase involves removing the componet from the dom. At this time the useEffect returns a function. This cleanup function is executed during unmount to remove the side effects like event listeners, timers and networking tasks.
So, When we want to perform something after each render of component then we can use the useEffect() hook. By using this Hook, we tell React that our component needs to do something after render by passing a function. React remember the function we passed in useEffect() hook and call it later after performing the DOM updates.
Steps to Create React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:
It will look like the following.

Example: This example demonstrates a component that displays an alert every time a button is clicked, showing the updated click count using the useEffect hook
JavaScript
// Filename - App.js
import React, { useState, useEffect } from "react";
function App() {
const [count, setCount] = useState(0);
useEffect(() => {
alert(`You clicked ${count} times`);
});
const handleUpdate = () => {
setCount(count + 1);
};
return (
<div>
<div>You have clicked {count} times</div>
<button onClick={handleUpdate}>Click me</button>
</div>
);
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
Explanation: As we can from the above example whenever we update the state, React re-render the component, and just after that useEffect() hook call function that we have passed.
Similar Reads
ReactJS useEffect Hook The useEffect hook is one of the most commonly used hooks in ReactJS used to handle side effects in functional components. Before hooks, these kinds of tasks were only possible in class components through lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount.What is
4 min read
ReactJS useLayoutEffect Hook The React JS useLayoutEffect works similarly to useEffect but rather works asynchronously like the useEffect hook, it fires synchronously after all DOM loading is done loading. This is useful for synchronously re-rendering the DOM and also to read the layout from the DOM. But to prevent blocking the
2 min read
How React Works? React is a tool made by Facebook to help create user interfaces, especially for single-page applications where the interface changes often and data updates a lot. It lets developers build reusable pieces of the interface, each managing its own state, which makes apps more efficient and simpler to ma
10 min read
Effect Management with useEffect Hook in React useEffect serves as a foundational tool in React development, enabling developers to orchestrate side effects within functional components systematically. It facilitates the management of asynchronous tasks, such as data fetching and DOM manipulation, enhancing code organization and maintainability.
3 min read
How To Call Loading Function With React useEffect? The useEffect runs by default after every render of the component. When placing useEffect in our component we tell React that we want to run the callback as an effect. React will run the effect after rendering and after performing the DOM updates. If we pass only a callback, the callback will run af
2 min read
Why useLayoutEffect Hook is beneficial in React ? useLayoutEffect is a React Hook that is similar to useEffect, but it fires synchronously after all DOM mutations. It's typically used for imperative DOM mutations or measurements, where you need to be sure that the DOM is updated before continuing. Here's an example of how you might use useLayoutEff
2 min read
How To Avoid Infinite Loops When Using useEffect() in ReactJS? useEffect() can lead to infinite loops, causing performance issues or crashes if not used correctly. In this article, we will explain what causes these infinite loops and how to avoid them when working with useEffect() in ReactJS.Avoid infinite loops in useEffect() by providing appropriate dependenc
4 min read
How to Use Flux to Manage State in ReactJS? State management in ReactJS is important for building dynamic and responsive applications. Flux, an architecture for managing state, provides a structured approach to handle data flow and state changes efficiently in React applications. In this article, we will explore the Flux to Manage State in Re
5 min read
Ref Hooks in React Ref Hooks provide a way to access and interact with the DOM (Document Object Model) nodes directly within functional components. Before hooks, refs were primarily used in class components. However, with Ref Hooks, functional components can also take advantage of this capability, enhancing flexibilit
4 min read
Difference Between useState and useEffect Hook in ReactJS ReactJS is the powerful JavaScript library used for building user interfaces especially single-page applications(SPAs). Two of the most commonly used hooks in React are useState and useEffect. These hooks make functional components more powerful by allowing them to manage the state and handle side e
3 min read