Have you ever wondered what happens when a React component is created, updated, or removed from the screen? React components go through different stages during their lifetime. These stages are known as the Component Lifecycle.
In modern React, the useEffect() hook helps us perform tasks such as fetching data, updating the document title, setting timers, and handling event listeners.
What is the Component Lifecycle in React?
The component lifecycle refers to the different stages a component goes through from the moment it appears on the screen until it is removed.
A React component generally goes through three important stages:
- Mounting – The component is created and displayed on the screen.
- Updating – The component re-renders when its state or props change.
- Unmounting – The component is removed from the screen.
1. Mounting Phase
Mounting is the stage when a component is created and displayed for the first time.
For example, when we open a webpage and a React component appears on the screen, the component is in its mounting phase.
We can use useEffect() to run a function only once when the component initially mounts.
import { useEffect } from "react";
function App() {
useEffect(() => {
console.log("Component Mounted");
}, []);
return <h1>Hello React!</h1>;
}
export default App;
How does this code work?
- useEffect() is a React hook used to perform side effects.
- The function inside useEffect() runs after the component renders.
- The empty dependency array [] tells React to run the effect when the component initially mounts.
2. Updating Phase
The updating phase occurs when a component re-renders because its state or props have changed.
We can use useEffect() to run a function whenever a particular state value changes.
import { useState, useEffect } from "react";
function App() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log("Count Updated:", count);
}, [count]);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}
export default App;
How does this code work?
- useState(0) creates a state variable called count.
- setCount() updates the count value.
- When the count changes, the component re-renders.
- The useEffect() runs whenever the count value changes.
3. Unmounting Phase
Unmounting is the stage when a component is removed from the screen.
Sometimes, we need to clean up tasks such as timers, event listeners, or subscriptions when a component is removed.
We can use the cleanup function inside useEffect().
import { useEffect } from "react";
function App() {
useEffect(() => {
console.log("Component Mounted");
return () => {
console.log("Component Unmounted");
};
}, []);
return <h1>Hello React!</h1>;
}
export default App;
How does this code work?
- The effect runs after the component renders.
- The cleanup function is returned from the useEffect().
- The cleanup function runs when the component is removed from the screen.
Understanding the useEffect Dependency Array
The dependency array determines when the effect runs.
Example 1: Without a dependency array
useEffect(() => {
console.log("Runs after every render");
});
This effect runs after every render, including the initial render and subsequent re-renders.
Example 2: With an empty dependency array
useEffect(() => {
console.log("Runs after initial render");
}, []);
This effect runs after the initial render. In development, React Strict Mode may run an additional setup and cleanup cycle.
Example 3: With a dependency
useEffect(() => {
console.log("Runs when count changes");
}, [count]);
This effect runs after the initial render and whenever the count value changes.
Conclusion
Understanding the React component lifecycle helps us know when components are created, updated, and removed from the screen. The useEffect() hook makes it easier to handle side effects in functional components.
By understanding the dependency array and cleanup function, you can use useEffect() more effectively in your React applications.

Top comments (0)