Hey buddy! π
Ever wondered how a React component is born, lives, and dies? πΌπ»π
In this article, weβre going to talk about the lifecycle of a React component β like how it starts, updates, and cleans up.
Letβs dive in and make it super easy and fun! π
π¬ What Is a Component Lifecycle?
Think of a React component like a personβs life:
- πΆ Mounting β itβs born (rendered on the screen).
- π§ Updating β it changes and grows (when state or props change).
- β°οΈ Unmounting β itβs removed from the screen (like retiring).
Each phase gives us a chance to run some code β like calling a friend when youβre born, changing clothes when you grow, or cleaning up your room when you leave.
π Class Components & Lifecycle Methods
Before Hooks came into React, we used class components to handle lifecycles.
Letβs take a quick peek at a simple class component:
jsxCopyEditimport React from 'react';
class MyComponent extends React.Component {
componentDidMount() {
console.log("Component has mounted! π§±");
}
componentDidUpdate() {
console.log("Component updated! π");
}
componentWillUnmount() {
console.log("Component will unmount! π§Ή");
}
render() {
return <h2>Hello from a Class Component!</h2>;
}
}
π Whatβs Happening Here?
Method | When It Runs |
---|---|
componentDidMount() | After component appears on screen (once) π§± |
componentDidUpdate() | Every time props or state change π |
componentWillUnmount() | Right before it disappears π§Ή |
This is how React lets us manage each stage of a componentβs life!
π¦ Side Effects? Whatβs That?
Side effects are things your component does outside of returning UI:
- Fetching data from an API π‘
- Subscribing to a service π
- Changing the document title π
- Setting up a timer β²οΈ
In class components, youβd use lifecycle methods for side effects.
But in modern React (with functional components), we use a hook…
π§ Meet useEffect β The Lifecycle Hook
useEffect
is like a superpower for functional components. π¦ΈββοΈ
It runs after the component has rendered β just like componentDidMount
, componentDidUpdate
, and componentWillUnmount
combined.
Hereβs the basic syntax:
jsxCopyEdituseEffect(() => {
// Your side effect code here
}, [dependencies]);
π§ͺ Example: Using useEffect
to Fetch Data
Letβs fetch user data from an API when the component loads.
jsxCopyEditimport React, { useEffect, useState } from "react";
function UserProfile() {
const [user, setUser] = useState(null);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/users/1")
.then(res => res.json())
.then(data => setUser(data));
}, []); // empty array means run only once like componentDidMount
return (
<div>
<h2>User Info:</h2>
{user ? <p>{user.name}</p> : <p>Loading...</p>}
</div>
);
}
π Key Points
[]
makes it run only once, likecomponentDidMount
.- You can put values inside the array to watch for changes.
π§Ή Cleaning Up β Like componentWillUnmount
Letβs say you set up a timer or a subscription β you should clean it up!
React lets you return a function inside useEffect
that runs on unmount.
π§Ό Cleanup Example: Timer
jsxCopyEdituseEffect(() => {
const timer = setInterval(() => {
console.log("β° Tick...");
}, 1000);
return () => {
clearInterval(timer);
console.log("π§Ή Timer cleared!");
};
}, []);
Now your app wonβt have memory leaks!
π React Lifecycle Comparison Table
Phase | Class Method | Functional Hook | When It Happens |
---|---|---|---|
Mount | componentDidMount | useEffect(() => {}, []) | After first render |
Update | componentDidUpdate | useEffect(() => {}, [value]) | On dependency change |
Unmount | componentWillUnmount | return () => {} inside useEffect | When removed |
π§ Infographic: Lifecycle Flow

Mount β‘οΈ Update β‘οΈ Unmount with Hooks and Class Methods shown side-by-side
π Bonus: Common Side Effects to Use with useEffect
- π‘ Fetching data from an API
- π Subscribing to a WebSocket
- π§Ή Cleaning up intervals or event listeners
- π Updating page title or meta tags
π§ Pro Tips
β
Donβt forget the dependency array!
β
Cleanup functions avoid memory leaks.
β
useEffect can run multiple times, so keep logic tight.
π Summary β Lifecycle Made Simple
Concept | Meaning |
---|---|
Lifecycle | The different phases a component goes through |
componentDidMount | Runs after first render (like startup) |
useEffect | The modern hook to handle all lifecycle stages |
Cleanup | Code that runs when a component is removed |
Side Effects | Anything outside of rendering (like fetching) |
π Keep Learning with Web Codder!
React’s lifecycle gives your app structure and power.
With useEffect
, you’re now equipped to manage it like a boss. πΌ
π Want more React tricks and beginner-friendly lessons?
Subscribe and follow:
- πΊ YouTube: Web Codder
- πΈ Instagram: @web_codder_official
- π¬ WhatsApp Community: Join here
Weβre building smart devs from scratch β and youβre one of them! π‘π