AI Dev Tools
ยท3 min readยทexplainer

React Component Lifecycle Explained: Birth, Life & Death ๐Ÿงฌ

React components go through stages just like us humans โ€” they're born, they live, and eventually they die. Let's understand this in plain English with zero jargon.

You open Swiggy to order food. ๐Ÿ•

The moment you tap the app, the home screen appears (born). You scroll, search, and it updates as you type (lives). You close the app โ€” the screen vanishes (dies).

That's exactly how a React component works.

Every component has three stages in life:

txt
๐ŸŸข MOUNT     โ†’   component appears on screen
๐Ÿ”„ UPDATE    โ†’   something changes (user types, data loads)
๐Ÿ”ด UNMOUNT   โ†’   component disappears from screen

๐ŸŸข Stage 1: Birth (Mount)

When a component first appears on the screen, React runs it for the very first time.

Think of it like Zomato loading your restaurant list โ€” the moment the page opens, it fetches your location, loads the cards, shows the UI. All of that happens once at birth.

In code, this is useEffect with an empty array:

jsx
useEffect(() => {
  fetchRestaurants(); // runs ONCE when component appears
}, []);

The [] at the end = "run this only at birth, never again."

Memory trick: Empty [] = Empty womb = Born once ๐Ÿฃ


๐Ÿ”„ Stage 2: Life (Update)

Now you're using the app. You change the city from Mumbai โ†’ Delhi. The restaurant list re-renders with new data.

That's an update. React watches for state or prop changes and re-runs the component.

jsx
const [city, setCity] = useState("Mumbai");
 
useEffect(() => {
  fetchRestaurants(city); // runs every time 'city' changes
}, [city]);

Every time city changes โ†’ effect runs โ†’ new restaurants load. ๐Ÿ”„

Memory trick: [city] in the array = "Watch this thing, run when it changes."


โŒ Stage 3: Death (Unmount)

You navigate away from the restaurant page. React removes that component from the screen entirely.

But here's the problem โ€” if you had a timer running, or an API call in progress, it keeps running even after the component is gone. Like leaving a Swiggy order open after you've already eaten. ๐Ÿ›

You need to clean up before dying:

jsx
useEffect(() => {
  const timer = setInterval(checkDelivery, 1000);
 
  return () => {
    clearInterval(timer); // runs when component dies
  };
}, []);

That return () => {} inside useEffect is the cleanup function โ€” it runs at death.


The Full Picture

txt
Component created
      โ†“
  ๐ŸŸข MOUNT
  useEffect([]) runs
      โ†“
  State/props change?
      โ†“
  ๐Ÿ”„ UPDATE
  useEffect([dep]) runs
      โ†“
  Component removed
      โ†“
  โŒ UNMOUNT
  cleanup function runs

Lifecycle vs. Human Life ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘

| Human Life | React Component | Code | | -------------- | ------------------- | --------------------------- | | Born | Mounted | useEffect(() => {}, []) | | Growing up | Re-rendering | useState, useEffect | | Daily habits | Watching for change | useEffect([dep]) | | Death | Unmounted | return () => cleanup() |


Class vs. Functional (Quick Look)

If you ever see older React code with class components, the lifecycle looks like this:

jsx
class OldSchool extends React.Component {
  componentDidMount()   { /* birth */ }
  componentDidUpdate()  { /* update */ }
  componentWillUnmount(){ /* death */ }
}

In modern React (hooks), all three collapse into just useEffect. One hook to rule them all. ๐Ÿ’


The Full Flow as a Diagram


One-Line Memory Tricks ๐Ÿง 

  • useEffect(() => {}, []) โ†’ Runs once at birth
  • useEffect(() => {}, [x]) โ†’ Runs when x changes
  • return () => {} inside effect โ†’ Runs at death
  • No deps array? โ†’ Runs every single render (usually avoid this)

Real Scenario: Swiggy Live Tracker ๐Ÿ›ต

Let's say you're building that little "order is 2km away" tracker:

jsx
function DeliveryTracker({ orderId }) {
  const [distance, setDistance] = useState(null);
 
  useEffect(() => {
    // ๐ŸŸข BIRTH: start tracking
    const interval = setInterval(async () => {
      const d = await fetchDistance(orderId);
      setDistance(d);
    }, 3000);
 
    // โŒ DEATH: stop tracking
    return () => clearInterval(interval);
  }, [orderId]); // ๐Ÿ”„ UPDATE: if order changes, restart
 
  return <p>Delivery is {distance}km away</p>;
}

Three lines of logic. Three lifecycle stages. One clean component. โœจ


TL;DR

React components live a full life:

  1. Mount โ†’ appears on screen, run side effects once
  2. Update โ†’ state/props changed, re-render, re-run watched effects
  3. Unmount โ†’ gone from screen, run cleanup

useEffect is your lifecycle manager. Master it, and you've mastered 70% of React.

Now go build something. ๐Ÿš€

ShareTweet

Related Posts