<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shivam Sharma</title>
    <description>The latest articles on Forem by Shivam Sharma (@shivamsharmxa).</description>
    <link>https://forem.com/shivamsharmxa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1189789%2F58fbc354-3e5b-4bd3-96b0-0947b6cd5e05.JPEG</url>
      <title>Forem: Shivam Sharma</title>
      <link>https://forem.com/shivamsharmxa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shivamsharmxa"/>
    <language>en</language>
    <item>
      <title>Life of a React Component: From Baby to Retirement 👶➡️👨‍💻➡️🏖️</title>
      <dc:creator>Shivam Sharma</dc:creator>
      <pubDate>Mon, 27 Jan 2025 06:05:25 +0000</pubDate>
      <link>https://forem.com/shivamsharmxa/life-of-a-react-component-from-baby-to-retirement-1aoa</link>
      <guid>https://forem.com/shivamsharmxa/life-of-a-react-component-from-baby-to-retirement-1aoa</guid>
      <description>&lt;p&gt;Hey folks!&lt;br&gt;
This is my first-ever article, so feel free to judge me—constructively, of course—so I can improve next time (just kidding... or maybe not 🤔).&lt;/p&gt;

&lt;p&gt;Okay, let’s dive in:&lt;br&gt;
What comes to your mind when you hear the word "Lifecycle"?&lt;br&gt;
Everyone will imagine different kinds of scenarios. For example: Some might think about the human lifecycle—how a child is born, grows up, learns new things from others, becomes independent, and eventually, retires (from life... just kidding, but hey, it’s kind of true!).&lt;/p&gt;

&lt;p&gt;Guess what? That’s exactly how React’s Lifecycle works.&lt;br&gt;
I’m going to break it down step by step, just like life.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Birth (Mounting):&lt;br&gt;
When the React component is created and added to the DOM, think of it like a baby taking its first breath.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Growth (Updating):&lt;br&gt;
Just like we grow, learn, and evolve, React components also update and change over time, based on new information (state or props).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retirement (Unmounting):&lt;br&gt;
And finally, when a component’s job is done, it gracefully says goodbye and is removed from the DOM. 👋&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In React terms, these are the Mounting, Updating, and Unmounting phases of the lifecycle. Simple, right? (Okay, maybe not yet, but stay with me!)&lt;/p&gt;

&lt;p&gt;Now, let’s break it down in more detail:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Birth Phase (Mounting):&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is where the magic begins—the baby React component comes into existence! When a component is mounted, React goes through the following steps:&lt;/p&gt;

&lt;p&gt;constructor(): This is like the “hello world” moment for your component. It’s born and initializes its state and props.&lt;br&gt;
render(): React says, “Alright, show me what you got!” and runs the render() method to display the UI.&lt;/p&gt;

&lt;p&gt;componentDidMount(): Once everything is ready and visible on the screen, this method runs. It’s the perfect place to fetch data, set up subscriptions, or initialize animations.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class BabyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isHappy: true };
    console.log("Baby is born!");
  }

  componentDidMount() {
    console.log("Baby has learned to walk!");
  }

  render() {
    return &amp;lt;div&amp;gt;I'm a happy baby! 😊&amp;lt;/div&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Growth Phase (Updating):
Now our baby is growing up! The component updates when:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The state changes (e.g., the baby gets a toy).&lt;br&gt;
The props change (e.g., the baby’s parent gives it a cookie 🍪).&lt;br&gt;
Here’s what happens:&lt;/p&gt;

&lt;p&gt;shouldComponentUpdate(): React asks, “Do I really need to update, or can I chill?” This is where you can optimize performance by controlling unnecessary updates.&lt;br&gt;
render(): If updates are necessary, React calls render() again to refresh the UI.&lt;br&gt;
componentDidUpdate(): React says, “Update complete! Let’s clean up or do something cool now.”&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


class TeenagerComponent extends React.Component {
  state = { mood: "happy" };

  shouldComponentUpdate(nextProps, nextState) {
    console.log("Should I update? 🤔");
    return nextState.mood !== this.state.mood; // Update only if the mood changes
  }

  componentDidUpdate() {
    console.log("Updated! Teenager got moody again.");
  }

  render() {
    return &amp;lt;div&amp;gt;Mood: {this.state.mood}&amp;lt;/div&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of it like a teenager who changes moods often—React needs to figure out when it’s worth refreshing the UI.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Goodbye Phase (Unmounting):
Every journey has an end, and so does a React component’s life. When it’s time to say goodbye, React runs:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;componentWillUnmount(): This is where you clean up—close subscriptions, clear timers, or say goodbye to anything the component was managing.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
class RetiredComponent extends React.Component {
  componentWillUnmount() {
    console.log("Time to retire. Cleaning up!");
  }

  render() {
    return &amp;lt;div&amp;gt;Goodbye, world! 👋&amp;lt;/div&amp;gt;;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Modern Twist:&lt;/strong&gt; React Hooks and useEffect&lt;br&gt;
If you’re working with functional components (as you should in modern React), the lifecycle is a bit different. Everything revolves around useEffect.&lt;/p&gt;

&lt;p&gt;How useEffect Works:&lt;br&gt;
useEffect is like the Swiss Army knife of React lifecycles—it can handle mounting, updating, and unmounting all in one place.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import React, { useState, useEffect } from "react";

function FunctionalComponent() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    console.log("Component mounted or updated!");

    return () =&amp;gt; {
      console.log("Component unmounted or cleanup happens here.");
    };
  }, [count]); // Only runs when 'count' changes

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;You clicked {count} times&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Click me&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, useEffect handles all three lifecycle phases. You can perform side effects on mount, update, or unmount all within a single hook. It’s a cleaner and more powerful way to handle side effects in functional components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways:&lt;/strong&gt;&lt;br&gt;
The React lifecycle has three phases: Mounting, Updating, and Unmounting.&lt;br&gt;
Class components use lifecycle methods like componentDidMount and componentWillUnmount.&lt;br&gt;
Functional components use useEffect to manage all lifecycle-related tasks.&lt;br&gt;
Always clean up resources in the unmounting phase to avoid memory leaks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope you all understand this, and if not—don’t worry! You’ll get it with good practice and consistency. Many people don’t understand this at first, and I was one of them. So, no need to stress!&lt;/p&gt;

&lt;p&gt;Also, feel free to drop your feedback so I can improve next time. 😊&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
