<?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: yashi srivastava</title>
    <description>The latest articles on Forem by yashi srivastava (@yashi_srivastava_9166487c).</description>
    <link>https://forem.com/yashi_srivastava_9166487c</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%2F2999405%2Ff209b0e3-f28a-4d25-b328-83ee446aa2d0.png</url>
      <title>Forem: yashi srivastava</title>
      <link>https://forem.com/yashi_srivastava_9166487c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yashi_srivastava_9166487c"/>
    <language>en</language>
    <item>
      <title>🧭 React Navigation &amp; Routing</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Mon, 13 Oct 2025 16:43:56 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/react-navigation-routing-complete-notes-166k</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/react-navigation-routing-complete-notes-166k</guid>
      <description>&lt;h3&gt;
  
  
  🔹 1. Why We Need Routing
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;HTML&lt;/strong&gt;, we can create navigation bars using &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags, but every time we click a link, the &lt;strong&gt;entire page reloads&lt;/strong&gt;.&lt;br&gt;
This makes navigation &lt;strong&gt;slower&lt;/strong&gt; because each page loads a new HTML file.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;React&lt;/strong&gt;, navigation happens &lt;strong&gt;inside a single HTML file&lt;/strong&gt; using &lt;strong&gt;JavaScript&lt;/strong&gt;, so the page doesn’t reload.&lt;br&gt;
👉 It gives a &lt;strong&gt;smooth and fast&lt;/strong&gt; user experience.&lt;/p&gt;


&lt;h3&gt;
  
  
  🔹 2. Example – Navigation in HTML vs React
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"home.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"contact.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;➡ Each click reloads the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Example (Using React Router):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/contact"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;➡ No page reload — React updates the view dynamically.&lt;/p&gt;




&lt;h3&gt;
  
  
  🔹 3. React Router Setup
&lt;/h3&gt;

&lt;p&gt;In React, we use &lt;strong&gt;BrowserRouter&lt;/strong&gt; to define all routes in one place — usually in &lt;code&gt;App.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Home&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Contact&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Contact&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/contact"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/contact"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Advantage:&lt;/strong&gt;&lt;br&gt;
We no longer need to add the navigation bar to each page manually — React Router handles everything in one place.&lt;/p&gt;


&lt;h3&gt;
  
  
  🔹 4. URL Example (Dynamic Navigation)
&lt;/h3&gt;

&lt;p&gt;When we visit a site like &lt;strong&gt;Myntra&lt;/strong&gt;, the URL changes dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myntra.com/clothes
myntra.com/clothes/tshirts
myntra.com/clothes/jeans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the &lt;strong&gt;URL changes&lt;/strong&gt;, the page doesn’t reload — React just updates the &lt;strong&gt;DOM&lt;/strong&gt; smoothly.&lt;/p&gt;




&lt;h3&gt;
  
  
  🔹 5. Nested Routes
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Nested routing&lt;/strong&gt; means creating routes inside another route.&lt;br&gt;
Example: &lt;code&gt;/dashboard/clothes&lt;/code&gt;, &lt;code&gt;/dashboard/makeup&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We define nested routes inside a parent route, and the &lt;strong&gt;nested components&lt;/strong&gt; render inside the parent using an &lt;strong&gt;Outlet&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Outlet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Dashboard&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Dashboard&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"clothes"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Clothes&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"makeup"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Makeup&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Outlet&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Nested components will render here */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clothes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Clothes Section&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Makeup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Makeup Section&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"dashboard"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dashboard&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"clothes"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Clothes&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"makeup"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Makeup&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/dashboard&lt;/code&gt; → shows Dashboard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/dashboard/clothes&lt;/code&gt; → shows Clothes inside Dashboard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/dashboard/makeup&lt;/code&gt; → shows Makeup inside Dashboard&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  💡 In Short:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;HTML&lt;/th&gt;
&lt;th&gt;React&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Navigation&lt;/td&gt;
&lt;td&gt;Reloads full page&lt;/td&gt;
&lt;td&gt;Updates single page dynamically&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Setup&lt;/td&gt;
&lt;td&gt;Separate HTML files&lt;/td&gt;
&lt;td&gt;One &lt;code&gt;BrowserRouter&lt;/code&gt; for all routes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nested Pages&lt;/td&gt;
&lt;td&gt;Not possible&lt;/td&gt;
&lt;td&gt;Easy with &lt;code&gt;Outlet&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Experience&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;td&gt;Fast &amp;amp; smooth&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>🌱 Understanding Prop Drilling and How to Avoid It in React</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Mon, 13 Oct 2025 16:24:04 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/understanding-prop-drilling-and-how-to-avoid-it-in-react-59e3</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/understanding-prop-drilling-and-how-to-avoid-it-in-react-59e3</guid>
      <description>&lt;p&gt;Have you ever found yourself passing the same data through multiple components just to get it to a deeply nested child?&lt;br&gt;
If yes — then you’ve experienced something called prop drilling.&lt;/p&gt;

&lt;p&gt;In this post, let’s explore what prop drilling is, why it can be a problem, and how React’s Context API helps us solve it efficiently.&lt;/p&gt;

&lt;p&gt;🧩 What Are Props in React?&lt;/p&gt;

&lt;p&gt;In React, props (short for “properties”) are how we pass data from a parent component to its children.&lt;br&gt;
They can be variable values, functions, or even entire components.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Child({ message }) {
  return &amp;lt;p&amp;gt;{message}&amp;lt;/p&amp;gt;;
}

function Parent() {
  return &amp;lt;Child message="Hello from Parent!" /&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;Here, the Parent component passes a prop called message to the Child.&lt;br&gt;
That’s simple enough — but what happens when we have many levels of nested components?&lt;/p&gt;

&lt;p&gt;🧭 What Is Prop Drilling?&lt;/p&gt;

&lt;p&gt;Prop drilling happens when we need to pass props through multiple layers of components to reach a deeply nested one.&lt;/p&gt;

&lt;p&gt;Let’s see an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [count, setCount] = React.useState(0);
  return &amp;lt;LevelOne count={count} setCount={setCount} /&amp;gt;;
}

function LevelOne({ count, setCount }) {
  return &amp;lt;LevelTwo count={count} setCount={setCount} /&amp;gt;;
}

function LevelTwo({ count, setCount }) {
  return &amp;lt;LevelThree count={count} setCount={setCount} /&amp;gt;;
}

function LevelThree({ count, setCount }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Notice how we had to pass count and setCount through every level, even though only LevelThree actually uses them.&lt;br&gt;
That’s prop drilling — and it can quickly become messy as your app grows.&lt;/p&gt;

&lt;p&gt;⚙️ Why Is Prop Drilling a Problem?&lt;/p&gt;

&lt;p&gt;It clutters intermediate components with props they don’t even use.&lt;/p&gt;

&lt;p&gt;It makes code harder to maintain and refactor.&lt;/p&gt;

&lt;p&gt;Adding or removing data often means changing multiple files.&lt;/p&gt;

&lt;p&gt;If you’ve ever updated a prop name and had to touch several components — that’s a sign of prop drilling trouble.&lt;/p&gt;

&lt;p&gt;🪜 What About State Lifting?&lt;/p&gt;

&lt;p&gt;Before we fix prop drilling, it’s important to understand state lifting.&lt;/p&gt;

&lt;p&gt;When two or more components need to share data, we can “lift” the state up to their common ancestor.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Parent() {
  const [count, setCount] = React.useState(0);
  return (
    &amp;lt;&amp;gt;
      &amp;lt;ChildA count={count} /&amp;gt;
      &amp;lt;ChildB setCount={setCount} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, Parent holds the state so both ChildA and ChildB can access it.&lt;br&gt;
This is state lifting, and it’s useful — but it doesn’t always solve prop drilling.&lt;/p&gt;

&lt;p&gt;🌐 Avoiding Prop Drilling with Context API&lt;/p&gt;

&lt;p&gt;React provides a built-in solution: Context API.&lt;br&gt;
It allows us to share data globally without manually passing props through every level.&lt;/p&gt;

&lt;p&gt;Here’s how it works:&lt;/p&gt;

&lt;p&gt;Create a context&lt;/p&gt;

&lt;p&gt;Provide it to components&lt;/p&gt;

&lt;p&gt;Consume it wherever needed&lt;/p&gt;

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

&lt;p&gt;`import React, { createContext, useContext, useState } from "react";&lt;/p&gt;

&lt;p&gt;// Step 1: Create Context&lt;br&gt;
const GlobalContext = createContext();&lt;/p&gt;

&lt;p&gt;function App() {&lt;br&gt;
  const [age, setAge] = useState(25);&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    // Step 2: Provide context value&lt;br&gt;
    &lt;br&gt;
      &lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function Profile() {&lt;br&gt;
  return ;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function UserDetails() {&lt;br&gt;
  // Step 3: Consume context&lt;br&gt;
  const { age, setAge } = useContext(GlobalContext);&lt;br&gt;
  return (&lt;br&gt;
    &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;p&amp;gt;Age: {age}&amp;lt;/p&amp;gt;
  &amp;lt;button onClick={() =&amp;gt; setAge(age + 1)}&amp;gt;Increase Age&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;);&lt;br&gt;
}&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, the UserDetails component can access and modify age without any prop drilling. 🎉&lt;/p&gt;

&lt;p&gt;🧠 A Quick Recap&lt;br&gt;
Concept Description&lt;br&gt;
Props   Data passed from parent to child&lt;br&gt;
Prop Drilling   Passing props through multiple intermediate components&lt;br&gt;
State Lifting   Moving state up so multiple components can share it&lt;br&gt;
Context API A global way to share state without manual prop passing&lt;br&gt;
⚡ Final Thoughts&lt;/p&gt;

&lt;p&gt;Prop drilling isn’t “bad” — it’s part of how React components communicate.&lt;br&gt;
But when your app grows and you need to share data across multiple components, Context API (or tools like Redux, Zustand, or Recoil) can make your life much easier.&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;p&gt;Use props for simple cases.&lt;br&gt;
Use Context for shared global state.&lt;/p&gt;

&lt;p&gt;💬 What about you?&lt;br&gt;
Have you run into prop drilling before?&lt;br&gt;
How did you solve it — Context, Redux, or another library?&lt;br&gt;
Share your experience below! 🚀&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>React Hooks &amp; Performance Concepts</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Sat, 11 Oct 2025 20:38:55 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/react-hooks-performance-concepts-3blf</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/react-hooks-performance-concepts-3blf</guid>
      <description>&lt;h2&gt;
  
  
  1️⃣ &lt;strong&gt;Stopwatch Example — Understanding &lt;code&gt;useRef&lt;/code&gt; with setInterval&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
I built a simple stopwatch using React to understand &lt;code&gt;useRef&lt;/code&gt; and state updates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Concepts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Problem:&lt;/strong&gt;
Using &lt;code&gt;setState&lt;/code&gt; directly in &lt;code&gt;setInterval&lt;/code&gt; can lead to stale state issues:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Might use old `time` value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;
Use the functional updater to access the latest state:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevTime&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevTime&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Why &lt;code&gt;useRef&lt;/code&gt; is important:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stores the interval ID across re-renders (&lt;code&gt;intervalRef.current&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Doesn’t trigger re-renders when updated.&lt;/li&gt;
&lt;li&gt;Allows us to clear the interval safely:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;intervalRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;intervalRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Important Takeaways:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useRef&lt;/code&gt; = persistent, mutable storage across renders.&lt;/li&gt;
&lt;li&gt;Always clear old intervals before starting a new one to prevent multiple intervals running simultaneously.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt; triggers UI updates; &lt;code&gt;useRef&lt;/code&gt; stores values silently.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  2️⃣ &lt;strong&gt;&lt;code&gt;useState&lt;/code&gt; vs &lt;code&gt;let&lt;/code&gt; vs &lt;code&gt;useRef&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
I experimented with a &lt;code&gt;money&lt;/code&gt; variable using &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;useState&lt;/code&gt;, and &lt;code&gt;useRef&lt;/code&gt; to understand state management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Observations:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using &lt;code&gt;let&lt;/code&gt;&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Value changes in memory but &lt;strong&gt;does not trigger UI updates&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Resets on re-renders since it’s recreated each time.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using &lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Changes &lt;strong&gt;trigger re-renders&lt;/strong&gt;, so updated values reflect in the UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using &lt;code&gt;useRef&lt;/code&gt;&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Stores a &lt;strong&gt;persistent value&lt;/strong&gt; across renders without triggering UI updates.&lt;/li&gt;
&lt;li&gt;Access using &lt;code&gt;.current&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Perfect for storing values like timers, previous state, or DOM references.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Memory Tip:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;State = UI updates&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Ref = silent memory storage&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3️⃣ &lt;strong&gt;Performance Optimization — &lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
Built a Fibonacci calculator to understand when recalculations happen unnecessarily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Observations:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Fibonacci function recalculates on every render, even if only unrelated state changes (&lt;code&gt;count&lt;/code&gt;) happen.&lt;/li&gt;
&lt;li&gt;Causes unnecessary computation and slows performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Solutions:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;useCallback&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memoizes a function reference.&lt;/li&gt;
&lt;li&gt;Only recreates the function if dependencies change.&lt;/li&gt;
&lt;li&gt;Prevents unnecessary function recreation across re-renders.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;useMemo&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memoizes the result of a calculation.&lt;/li&gt;
&lt;li&gt;Recalculates only if dependencies change (e.g., input value).&lt;/li&gt;
&lt;li&gt;Avoids unnecessary computation when unrelated state changes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Memory Tip:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Callback = remembers function&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Memo = remembers result&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4️⃣ &lt;strong&gt;Important Lessons Learned&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Re-renders:&lt;/strong&gt; Functions and variables are recreated, but refs remain stable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intervals &amp;amp; Timers:&lt;/strong&gt; Always store in &lt;code&gt;useRef&lt;/code&gt; and clear them to prevent duplicate runs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State vs Ref:&lt;/strong&gt; Use state for UI updates; use ref for persistent data without re-renders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Hooks:&lt;/strong&gt; &lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt; prevent unnecessary recalculation and function recreation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging Tip:&lt;/strong&gt; Always check console logs to confirm variable updates that don’t reflect in UI (common with &lt;code&gt;let&lt;/code&gt; or ref values).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5️⃣ &lt;strong&gt;Practical Takeaways for Developers&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;For timers or intervals, always store the reference in &lt;code&gt;useRef&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;useState&lt;/code&gt; when UI needs to update, &lt;code&gt;useRef&lt;/code&gt; when you want to persist values silently.&lt;/li&gt;
&lt;li&gt;Use functional updates with &lt;code&gt;setState&lt;/code&gt; to avoid stale closures.&lt;/li&gt;
&lt;li&gt;Optimize performance with &lt;code&gt;useCallback&lt;/code&gt; (functions) and &lt;code&gt;useMemo&lt;/code&gt; (values).&lt;/li&gt;
&lt;li&gt;Always clear intervals or timeouts to prevent memory leaks and bugs.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  6️⃣ &lt;strong&gt;Memory Mnemonics&lt;/strong&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hook&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Tip&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;UI reactive value&lt;/td&gt;
&lt;td&gt;&lt;em&gt;State = UI updates&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;useRef&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Persistent storage across renders&lt;/td&gt;
&lt;td&gt;&lt;em&gt;Ref = silent memory&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;useCallback&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Memoize function references&lt;/td&gt;
&lt;td&gt;&lt;em&gt;Callback = remembers function&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;useMemo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Memoize computed values&lt;/td&gt;
&lt;td&gt;&lt;em&gt;Memo = remembers result&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>performance</category>
      <category>tutorial</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript revision blew my mind today.</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Tue, 30 Sep 2025 17:01:12 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/javascript-revision-blew-my-mind-today-3db</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/javascript-revision-blew-my-mind-today-3db</guid>
      <description>&lt;p&gt;We can do console.log(Object.prototype) → and it shows the properties and methods that all objects inherit.But try console.log (&lt;code&gt;Object.__proto__&lt;/code&gt;) → it gives ƒ () { [native code] }.&lt;/p&gt;

&lt;p&gt;Ever wondered why?&lt;br&gt;
Because our initial object is created using a &lt;strong&gt;constructor&lt;/strong&gt;.&lt;br&gt;
And only in constructors we can add properties by using &lt;code&gt;prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, who inherits?&lt;br&gt;
It’s like our global &lt;code&gt;Object&lt;/code&gt; constructor. The objects created from it inherit through &lt;code&gt;prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That &lt;code&gt;prototype&lt;/code&gt; itself has inner things we use—like &lt;code&gt;.create()&lt;/code&gt; and &lt;code&gt;.__proto__&lt;/code&gt;.&lt;br&gt;
All these are functions living on &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;lets deep dive&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  1 — Quick overview
&lt;/h2&gt;

&lt;p&gt;Every object in JavaScript has a hidden pointer to another object (its &lt;em&gt;prototype&lt;/em&gt;). When you ask for a property, JS checks the object first, then follows that pointer up the chain until it finds the property or reaches &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  2 — &lt;code&gt;Object.create(proto)&lt;/code&gt; example (start here)
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello from parent!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// shows child as {}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// shows the parent object in console&lt;/span&gt;
&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;               &lt;span class="c1"&gt;// "Hello from parent!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What happened (step by step):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;child&lt;/code&gt; is created as an empty object.&lt;/li&gt;
&lt;li&gt;Internally, JS sets &lt;code&gt;child&lt;/code&gt;’s hidden slot &lt;code&gt;[[Prototype]]&lt;/code&gt; to point to &lt;code&gt;parent&lt;/code&gt;.
(DevTools shows this as &lt;code&gt;child.__proto__&lt;/code&gt;.)&lt;/li&gt;
&lt;li&gt;When you call &lt;code&gt;child.greet()&lt;/code&gt;, JS doesn’t find &lt;code&gt;greet&lt;/code&gt; on &lt;code&gt;child&lt;/code&gt;, so it looks at &lt;code&gt;child.__proto__&lt;/code&gt; (the &lt;code&gt;parent&lt;/code&gt;) and finds &lt;code&gt;greet&lt;/code&gt; there.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;ASCII view:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;child {} 
   __proto__ → parent { greet: f }
                   __proto__ → Object.prototype
                                      __proto__ → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3 — Understanding the console output: &lt;code&gt;{ greet: ƒ }  [[Prototype]]: Object&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When the console shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ greet: ƒ }
[[Prototype]]: Object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{ greet: ƒ }&lt;/code&gt; = the &lt;code&gt;parent&lt;/code&gt; object (it has a &lt;code&gt;greet&lt;/code&gt; function).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[[Prototype]]: Object&lt;/code&gt; = meaning the &lt;code&gt;parent&lt;/code&gt; object itself has a prototype, which is &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The double brackets &lt;code&gt;[[Prototype]]&lt;/code&gt; mean this is an internal slot (not a normal enumerable property).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Simple sentence:&lt;/strong&gt; the console is telling you the &lt;code&gt;parent&lt;/code&gt; has its own parent — &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4 — &lt;code&gt;__proto__&lt;/code&gt; vs &lt;code&gt;prototype&lt;/code&gt; (exact difference)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;__proto__&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exists on &lt;em&gt;objects&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;It’s the &lt;em&gt;actual&lt;/em&gt; reference (internal slot) to the parent object.&lt;/li&gt;
&lt;li&gt;You can inspect it with &lt;code&gt;obj.__proto__&lt;/code&gt; or &lt;code&gt;Object.getPrototypeOf(obj)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;code&gt;prototype&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exists on &lt;em&gt;constructor functions&lt;/em&gt; (e.g., &lt;code&gt;function Person(){}&lt;/code&gt; → &lt;code&gt;Person.prototype&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;It is the object that will become the &lt;code&gt;__proto__&lt;/code&gt; of instances created by that constructor.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Proof code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5 — What &lt;code&gt;new&lt;/code&gt; does (exact, ordered steps)
&lt;/h2&gt;

&lt;p&gt;When you run &lt;code&gt;const x = new C(args)&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new empty object: &lt;code&gt;{}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set the object’s internal &lt;code&gt;[[Prototype]]&lt;/code&gt; to &lt;code&gt;C.prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;C&lt;/code&gt; with &lt;code&gt;this&lt;/code&gt; set to that new object, filling properties on &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return the new object (unless constructor explicitly returns another object).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Visual:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x { own properties }
   __proto__ → C.prototype { shared methods }
                   __proto__ → Object.prototype
                                      __proto__ → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  6 — Property lookup algorithm (how JS finds a property)
&lt;/h2&gt;

&lt;p&gt;When you do &lt;code&gt;obj.someProp&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check &lt;code&gt;obj&lt;/code&gt;’s own properties. If found → return it.&lt;/li&gt;
&lt;li&gt;Else, set &lt;code&gt;obj = obj.__proto__&lt;/code&gt; and repeat step 1.&lt;/li&gt;
&lt;li&gt;Continue until &lt;code&gt;obj&lt;/code&gt; becomes &lt;code&gt;null&lt;/code&gt;. If not found → &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is why &lt;code&gt;child.greet()&lt;/code&gt; works even though &lt;code&gt;greet&lt;/code&gt; is not on &lt;code&gt;child&lt;/code&gt;: the lookup finds &lt;code&gt;greet&lt;/code&gt; on &lt;code&gt;parent&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  7 — Where to put data vs methods (practical rule)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Put per-object data inside the constructor (each instance gets its own copy).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Put shared methods on the constructor’s &lt;code&gt;prototype&lt;/code&gt; (all instances reuse the same function).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why:&lt;/strong&gt; methods on &lt;code&gt;prototype&lt;/code&gt; save memory and make behavior consistent for all instances.&lt;/p&gt;




&lt;h2&gt;
  
  
  8 — The top of the chain: &lt;code&gt;Object.prototype&lt;/code&gt; → &lt;code&gt;null&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Most objects eventually point to &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.prototype.__proto__&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;. That &lt;code&gt;null&lt;/code&gt; is the end — no more lookup beyond that.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  9 — Function vs Object (short note)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Object&lt;/code&gt; itself is a function (&lt;code&gt;typeof Object === 'function'&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Functions are objects too: they have their own &lt;code&gt;__proto__&lt;/code&gt; (usually &lt;code&gt;Function.prototype&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;So constructors and built-ins follow the same prototype rules (they are objects that link to prototypes).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  10 — How to inspect safely (recommended tools)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Object.getPrototypeOf(obj)&lt;/code&gt; → preferred way to get prototype (safer than &lt;code&gt;__proto__&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;obj.hasOwnProperty('x')&lt;/code&gt; → checks own properties only.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(obj.__proto__)&lt;/code&gt; or expand object in DevTools → shows &lt;code&gt;[[Prototype]]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  11 — Common mistakes &amp;amp; best practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Don’t rely on &lt;code&gt;__proto__&lt;/code&gt;&lt;/strong&gt; for setting prototypes in production (it’s slower). Use &lt;code&gt;Object.create(proto)&lt;/code&gt; or &lt;code&gt;Object.setPrototypeOf()&lt;/code&gt; only when needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don’t mutate shared prototype state&lt;/strong&gt; carelessly — changing &lt;code&gt;PrototypeObject.sharedArray.push(...)&lt;/code&gt; affects all instances.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;prototype&lt;/code&gt; for methods, constructor for instance data.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  12 — Short cheat-sheet (memorize this)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Object.create(proto)&lt;/code&gt; → create object whose &lt;code&gt;__proto__&lt;/code&gt; is &lt;code&gt;proto&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__proto__&lt;/code&gt; → object’s hidden parent pointer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;prototype&lt;/code&gt; → constructor’s blueprint object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; → links instance &lt;code&gt;__proto__&lt;/code&gt; to constructor’s &lt;code&gt;prototype&lt;/code&gt;, then runs constructor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object.prototype.__proto__ === null&lt;/code&gt; → chain end.&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>## 📌 Deep Dive into `useState`, Re-renders, and Optimizations in React</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Mon, 07 Apr 2025 18:00:41 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/-deep-dive-into-usestate-re-renders-and-optimizations-in-react-ajm</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/-deep-dive-into-usestate-re-renders-and-optimizations-in-react-ajm</guid>
      <description>&lt;p&gt;&lt;strong&gt;&amp;gt; learned the whole concept from this simple project&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjteuzqzluiciradwbhro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjteuzqzluiciradwbhro.png" alt="Image description" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  🔄 What does &lt;strong&gt;re-render&lt;/strong&gt; mean in &lt;code&gt;useState&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Re-render(means calling function)in React happens when we &lt;strong&gt;call the function returned by &lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt; (like &lt;code&gt;setColor&lt;/code&gt;) to update the state. A common confusion arises when we use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when I click a button and use &lt;code&gt;setColor("green")&lt;/code&gt;, the component re-renders.&lt;/p&gt;




&lt;p&gt;Got it! Here's your original post with your tone and style kept intact — just added the new part you wanted, cleaned up the logic a bit, and kept it simple:&lt;/p&gt;




&lt;h3&gt;
  
  
  💭 But how does it work, if it's a &lt;code&gt;const&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;React makes us feel like this variable is being &lt;strong&gt;recreated every time&lt;/strong&gt;, but here’s the core truth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;"black"&lt;/code&gt; is used &lt;strong&gt;only once&lt;/strong&gt; — when the component mounts for the &lt;strong&gt;first time&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;After that, &lt;strong&gt;React preserves the state&lt;/strong&gt; between re-renders.
&lt;/li&gt;
&lt;li&gt;Every time you call &lt;code&gt;setColor(...)&lt;/code&gt;, React re-renders the component &lt;strong&gt;with the preserved state&lt;/strong&gt;, not the original &lt;code&gt;"black"&lt;/code&gt; — means any value which was preserved like red, green etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now even though you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this re-render, React &lt;strong&gt;ignores&lt;/strong&gt; &lt;code&gt;"green"&lt;/code&gt; because the state is already preserved with the latest value (like &lt;code&gt;"green"&lt;/code&gt; from previous update).&lt;/p&gt;

&lt;p&gt;👉 Here's what’s really going on:&lt;br&gt;&lt;br&gt;
In every render, &lt;code&gt;const [Color, setColor] = useState("black");&lt;/code&gt; runs again — so &lt;code&gt;Color&lt;/code&gt; is getting created again because it's a &lt;code&gt;const&lt;/code&gt; (you can’t reassign it, but you can recreate it on every function call), and it's stored in the stack.&lt;br&gt;&lt;br&gt;
But when the value of &lt;code&gt;Color&lt;/code&gt; doesn’t change, it just takes the value from the preserved state.&lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;setColor&lt;/code&gt; is created only &lt;strong&gt;one time&lt;/strong&gt;, and its reference is stored in the &lt;strong&gt;heap&lt;/strong&gt;, so it doesn’t get recreated on every render.&lt;/p&gt;


&lt;h3&gt;
  
  
  🔁 Functions Re-execute, But State Persists
&lt;/h3&gt;

&lt;p&gt;Yes, it's true that every time the component re-renders, the function (like &lt;code&gt;App&lt;/code&gt; or &lt;code&gt;Main&lt;/code&gt;) &lt;strong&gt;runs again&lt;/strong&gt; and all its variables are &lt;strong&gt;freshly created in memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But React holds onto the state &lt;strong&gt;separately&lt;/strong&gt;, outside of the function scope — that’s how &lt;code&gt;useState&lt;/code&gt; works.&lt;/p&gt;


&lt;h3&gt;
  
  
  🧠 Re-rendering for Primitive vs Non-Primitive Types
&lt;/h3&gt;

&lt;p&gt;There’s a subtle behavior in &lt;code&gt;useState&lt;/code&gt; that’s often overlooked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;primitive values&lt;/strong&gt; (&lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;), if you call &lt;code&gt;setState&lt;/code&gt; with the &lt;strong&gt;same value&lt;/strong&gt;, React &lt;strong&gt;may still re-render&lt;/strong&gt;, even though the value didn't change.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;React doesn’t compare the old vs new primitive deeply — it just assumes change and re-renders.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;But for &lt;strong&gt;non-primitive types&lt;/strong&gt; (&lt;code&gt;object&lt;/code&gt;, &lt;code&gt;array&lt;/code&gt;, &lt;code&gt;function&lt;/code&gt;), React compares the &lt;strong&gt;reference&lt;/strong&gt;.
If the &lt;strong&gt;reference doesn't change&lt;/strong&gt;, React won’t trigger a re-render.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So yes — it’s kind of ironic: &lt;strong&gt;React re-renders even when the primitive value is the same&lt;/strong&gt;, but &lt;strong&gt;avoids re-rendering for same-reference non-primitives like objects,arrays.&lt;/strong&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  ✅ Optimization with &lt;code&gt;useEffect&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;To prevent running side effects unnecessarily — like changing the background color — we use &lt;code&gt;useEffect&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React will &lt;strong&gt;only&lt;/strong&gt; update the background color if &lt;code&gt;Color&lt;/code&gt; has &lt;strong&gt;actually changed&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Even though &lt;code&gt;useState&lt;/code&gt; may trigger a re-render with the same primitive value, &lt;code&gt;useEffect&lt;/code&gt; &lt;strong&gt;won’t run&lt;/strong&gt; unless the dependency (&lt;code&gt;Color&lt;/code&gt;) is &lt;strong&gt;different&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So this is where &lt;code&gt;useEffect&lt;/code&gt; &lt;strong&gt;helps us control unnecessary operations&lt;/strong&gt;, especially with primitives.&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚠️ What if you don’t provide a dependency array?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// runs on every render&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will cause the effect to run &lt;strong&gt;after every render&lt;/strong&gt;, regardless of what changed.&lt;/p&gt;

&lt;p&gt;But if you do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// runs only once — on initial render&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then it runs &lt;strong&gt;only on first mount&lt;/strong&gt;, just like &lt;code&gt;componentDidMount&lt;/code&gt; in class components.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧩 Component Nesting, Reusability, and the Need for &lt;code&gt;memo&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now the advanced bit.&lt;/p&gt;

&lt;p&gt;When we render a UI, we often build it using &lt;strong&gt;multiple components&lt;/strong&gt;. That’s how we &lt;strong&gt;split UI&lt;/strong&gt; into reusable parts.&lt;/p&gt;

&lt;p&gt;But here’s the issue:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If only &lt;strong&gt;one part of the UI is changing&lt;/strong&gt;, React still re-runs &lt;strong&gt;all components&lt;/strong&gt; used inside the main function — even if they didn't change.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s unnecessary, and over time, can slow things down.&lt;/p&gt;




&lt;h3&gt;
  
  
  🛡️ Enter &lt;code&gt;React.memo&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;React provides a hook called &lt;code&gt;memo&lt;/code&gt; which helps with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MemoizedComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes React:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Render &lt;code&gt;MyComponent&lt;/code&gt; &lt;strong&gt;only once initially&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Then &lt;strong&gt;skip re-rendering&lt;/strong&gt; it on future parent renders — &lt;strong&gt;unless props change&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So now, if you're rendering multiple UIs like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MainUI&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AnotherUI&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if only &lt;code&gt;MainUI&lt;/code&gt; needs to update, without &lt;code&gt;memo&lt;/code&gt;, both re-render. But with &lt;code&gt;memo&lt;/code&gt;, React skips re-rendering &lt;code&gt;AnotherUI&lt;/code&gt; if its props haven’t changed. Huge performance win.&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚠️ But yes — there's a &lt;strong&gt;tradeoff&lt;/strong&gt; with &lt;code&gt;memo&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When you wrap a component with &lt;code&gt;memo&lt;/code&gt;, &lt;strong&gt;React adds extra comparison logic&lt;/strong&gt; to check if the props have changed.
&lt;/li&gt;
&lt;li&gt;That’s some &lt;strong&gt;additional overhead&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If your component always receives new props, &lt;strong&gt;it will re-render anyway&lt;/strong&gt;, so &lt;code&gt;memo&lt;/code&gt; doesn't help.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ So &lt;strong&gt;use &lt;code&gt;memo&lt;/code&gt; when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The component is large or expensive to render&lt;/li&gt;
&lt;li&gt;It receives &lt;strong&gt;stable props&lt;/strong&gt; that don't change often&lt;/li&gt;
&lt;li&gt;The parent re-renders &lt;strong&gt;frequently&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🎯 Final Conclusion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useState()&lt;/code&gt; uses the initial value &lt;strong&gt;only once&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;React re-renders the component when state changes — but keeps the latest state preserved.&lt;/li&gt;
&lt;li&gt;Re-renders recreate the function, but React gives you &lt;strong&gt;the last state&lt;/strong&gt; back.&lt;/li&gt;
&lt;li&gt;React doesn’t optimize re-renders for primitives — but you can control side effects using &lt;code&gt;useEffect&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For better performance, use &lt;code&gt;React.memo&lt;/code&gt; to &lt;strong&gt;skip re-rendering components&lt;/strong&gt; that don’t need to update.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This session helped me break some long-standing confusion. If you're also learning React, &lt;strong&gt;go deep&lt;/strong&gt; — don't just learn syntax, understand the behavior.&lt;/p&gt;

&lt;p&gt;React is not just about rendering UI — it’s about &lt;strong&gt;rendering smartly&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;✨ If you found this helpful or you're on your own React journey — drop a like, share with a fellow dev, and feel free to comment your thoughts or doubts!&lt;br&gt;
Let's learn together and build better. 💻🚀&lt;/p&gt;

&lt;h1&gt;
  
  
  ReactJS #WebDev #CodeNewbie #LearnTogether
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>🚀 I Just Cleared Some 🔥Core React &amp; JavaScript Doubts — and You Should Know These Too!</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Mon, 07 Apr 2025 17:46:01 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/i-just-cleared-some-core-react-javascript-doubts-and-you-should-know-these-too-4bk1</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/i-just-cleared-some-core-react-javascript-doubts-and-you-should-know-these-too-4bk1</guid>
      <description>&lt;p&gt;Hey folks! Today was one of &lt;em&gt;those&lt;/em&gt; sessions where the lightbulb 💡 &lt;em&gt;really&lt;/em&gt; went off — I finally understood some confusing but &lt;strong&gt;fundamental&lt;/strong&gt; stuff in React and JavaScript. Here's a breakdown of what I learned (and trust me, it's gold 👑 for beginners and even intermediates who missed the tiny details):&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚛️ 1. &lt;strong&gt;Why JSX Needs Double Curly Braces for Styling&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;"Wait, I'm passing a JavaScript object, so why can't I just wrap it directly with {} like that?"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Red&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;❌&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now I know:&lt;br&gt;
Because JSX doesn't allow curly braces around attributes like that. In JSX, attributes must be in the format:&lt;br&gt;
attributeName={jsValue}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Red&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in incorrect example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button {style={...}}&amp;gt; ... &amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're trying to put JavaScript inside the HTML tag syntax itself, which breaks JSX rules.&lt;br&gt;
JSX expects props (like style) to be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;tagName propName={jsExpression} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button style={ { backgroundColor: "red" } }&amp;gt;Red&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is valid because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;style is the prop name&lt;/li&gt;
&lt;li&gt;{ ... } is the JSX way of saying “the value of this prop is a JavaScript expression”&lt;/li&gt;
&lt;li&gt;And that expression happens to be an object
🔁&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔁 2. &lt;strong&gt;Does the Whole Component Re-render on State Change?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I asked:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When I click a button and change color using &lt;code&gt;setColor("pink")&lt;/code&gt;, does the whole component re-render? Does React repaint everything?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ Answer: &lt;strong&gt;Yes, the function re-runs, but React is smart!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The entire JSX is &lt;strong&gt;re-evaluated&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;But React &lt;strong&gt;only updates the actual DOM parts that changed&lt;/strong&gt; (thanks to Virtual DOM and diffing)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So your UI stays efficient and smooth 🧈✨&lt;/p&gt;




&lt;h3&gt;
  
  
  👀 3. &lt;strong&gt;Why Does &lt;code&gt;console.log()&lt;/code&gt; Run Twice in React?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I dropped a &lt;code&gt;console.log("let's see")&lt;/code&gt; inside JSX and freaked out when it printed twice 😅&lt;/p&gt;

&lt;p&gt;💡 Reason:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;React.StrictMode&lt;/code&gt;&lt;/strong&gt; in development &lt;strong&gt;intentionally renders components twice&lt;/strong&gt; (but ONLY in dev mode) to catch bugs.&lt;/p&gt;

&lt;p&gt;✅ Production will render just once. No worries! All part of the dev experience.&lt;/p&gt;


&lt;h3&gt;
  
  
  🧠 4. &lt;strong&gt;JS Return Rule: Why Code After &lt;code&gt;return&lt;/code&gt; is Ignored&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I made this mistake:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ doesn't work&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lesson learned:&lt;br&gt;
➡️ &lt;strong&gt;After &lt;code&gt;return&lt;/code&gt;, nothing else inside the function runs.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
So if you want to return &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt;, include it inside the &lt;code&gt;return (...)&lt;/code&gt; block.&lt;/p&gt;


&lt;h3&gt;
  
  
  📦 5. &lt;strong&gt;Do I Have to Use the Same Prop Name?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I was passing state like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I wondered: &lt;em&gt;Do I have to call it &lt;code&gt;count&lt;/code&gt; in the child?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;✅ Nope!&lt;/p&gt;

&lt;p&gt;You can rename it however you want when passing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="na"&gt;totalClicks&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just make sure the name matches in the child:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;totalClicks&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;totalClicks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔄 Arrays/Objects confusion – Built-in or User-Defined?&lt;br&gt;
Absolute 🔥 of a question:&lt;/p&gt;

&lt;p&gt;“Why do we call arrays/objects user-defined if we didn’t define how they work?”&lt;/p&gt;

&lt;p&gt;Here’s the truth:&lt;/p&gt;

&lt;p&gt;Object and Array types are built-in to JavaScript ✅&lt;/p&gt;

&lt;p&gt;But when you create a structure with them, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: "Aman", age: 22 };
const colors = ["red", "green"];

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

&lt;/div&gt;



&lt;p&gt;That structure is user-defined — you decide the keys, values, and layout. You didn’t create the engine, but you decided what’s inside it.&lt;/p&gt;

&lt;p&gt;🧠 Built-in container, user-defined content.&lt;/p&gt;

&lt;p&gt;Primitive types&lt;br&gt;
These are the basic building blocks — they are predefined and immutable (can't be changed directly).&lt;/p&gt;

&lt;p&gt;String&lt;/p&gt;

&lt;p&gt;Number&lt;/p&gt;

&lt;p&gt;Boolean&lt;br&gt;
These are called primitive because:&lt;/p&gt;

&lt;p&gt;They hold single values.&lt;/p&gt;

&lt;p&gt;They are not objects.&lt;/p&gt;

&lt;p&gt;They are copied by value.&lt;/p&gt;

&lt;p&gt;So yes, they are predefined by the language.&lt;/p&gt;

&lt;h3&gt;
  
  
  💡 TL;DR:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Predefined&lt;/th&gt;
&lt;th&gt;User-defined&lt;/th&gt;
&lt;th&gt;Stored As&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primitive&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;By value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Non-primitive&lt;/td&gt;
&lt;td&gt;✅ (base) + ✅ you define shape&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;By reference&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  🧩 Topics I Crushed Today:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JSX attribute syntax
&lt;/li&gt;
&lt;li&gt;React re-render logic ⚛️
&lt;/li&gt;
&lt;li&gt;StrictMode behavior 👀
&lt;/li&gt;
&lt;li&gt;JavaScript return rules 🧠
&lt;/li&gt;
&lt;li&gt;Prop naming and passing 📨
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're learning React — bookmark this, share it, and remember:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Even your "smallest doubts" are super valid — clear them early and loud! 🙌&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Let me know if you want this as a tweet thread, blog intro, or carousel — we can style it however you like! 🎨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Computer Networks-2</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Mon, 07 Apr 2025 06:56:09 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/computer-networks-2-6cb</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/computer-networks-2-6cb</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9lq21f0cszsfjbznc0px.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9lq21f0cszsfjbznc0px.png" alt="Image description" width="800" height="614"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnzzx44uho57koxuk7x2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnzzx44uho57koxuk7x2.png" alt="Image description" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Transport Layer – End-to-End Delivery&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The transport layer ensures end-to-end delivery of data. When a client sends data to another client or requests data from a server, the data is transmitted in the form of &lt;strong&gt;packets&lt;/strong&gt;, which contain two &lt;strong&gt;IP addresses&lt;/strong&gt;:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sender's IP address&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Receiver's IP address&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, imagine a scenario where a user has multiple tabs open (Google, YouTube, LinkedIn, etc.). When data is received, how does the system know &lt;strong&gt;which tab requested it&lt;/strong&gt; and where to deliver it?  &lt;/p&gt;

&lt;p&gt;This is determined by the &lt;strong&gt;port number&lt;/strong&gt;, as each tab operates on a unique &lt;strong&gt;port&lt;/strong&gt;. When data is delivered, it comes with a &lt;strong&gt;destination port number&lt;/strong&gt;, ensuring that it reaches the correct application. The &lt;strong&gt;port number is 16-bit&lt;/strong&gt;, meaning there are &lt;strong&gt;2¹⁶ (65,536) possible ports&lt;/strong&gt;. This is why we say that the &lt;strong&gt;transport layer provides port-to-port delivery&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;For example, if you are requesting data from YouTube over &lt;strong&gt;HTTPS&lt;/strong&gt;, then the request uses &lt;strong&gt;port 443&lt;/strong&gt; (which is the default port for HTTPS). When the packet is sent, it includes:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sender’s IP address&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sender’s port number (tab’s port)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Receiver’s IP address&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Receiver’s port number (443 for HTTPS in this case)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the data reaches the server, it looks at the &lt;strong&gt;port number&lt;/strong&gt; and routes the request accordingly (e.g., to the HTTPS service). The server then sends the response back with these &lt;strong&gt;same four details&lt;/strong&gt; to ensure correct delivery.  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Data Flow Through Layers&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: Data is created.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: Adds &lt;strong&gt;port numbers&lt;/strong&gt; → Now called a &lt;strong&gt;segment&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Layer&lt;/strong&gt;: Adds &lt;strong&gt;IP addresses&lt;/strong&gt; → Now called a &lt;strong&gt;packet&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Link Layer&lt;/strong&gt;: Adds &lt;strong&gt;MAC addresses&lt;/strong&gt; → Now called a &lt;strong&gt;frame&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Physical Layer&lt;/strong&gt;: Converts frames into &lt;strong&gt;bits&lt;/strong&gt; for transmission.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;TCP vs UDP&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. TCP (Transmission Control Protocol) – Reliable Communication&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;TCP is a &lt;strong&gt;connection-oriented&lt;/strong&gt; protocol. Before sending data, it establishes a connection using a &lt;strong&gt;three-way handshake&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client → Server&lt;/strong&gt;: "Are you available?" (&lt;strong&gt;SYN&lt;/strong&gt;)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server → Client&lt;/strong&gt;: "Yes, I am available!" (&lt;strong&gt;SYN-ACK&lt;/strong&gt;)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client → Server&lt;/strong&gt;: "Okay, I am sending data now." (&lt;strong&gt;ACK&lt;/strong&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the connection is established, TCP ensures &lt;strong&gt;reliable delivery&lt;/strong&gt; by keeping track of packets. If a packet is lost, the receiver requests it again.  &lt;/p&gt;

&lt;p&gt;For example, if &lt;strong&gt;100MB of data&lt;/strong&gt; is divided into &lt;strong&gt;20 segments&lt;/strong&gt;, but only &lt;strong&gt;18 arrive&lt;/strong&gt;, the receiver will request the &lt;strong&gt;missing 2 packets&lt;/strong&gt; before assembling the complete data. This makes &lt;strong&gt;TCP reliable&lt;/strong&gt; for scenarios like emails, file downloads, and web browsing.  &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;2. UDP (User Datagram Protocol) – Faster, but Unreliable&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;UDP is &lt;strong&gt;connectionless&lt;/strong&gt;, meaning it &lt;strong&gt;does not establish a connection&lt;/strong&gt; before sending data. It simply &lt;strong&gt;starts transmitting packets&lt;/strong&gt; without checking whether they reach the destination.  &lt;/p&gt;

&lt;p&gt;Since UDP does &lt;strong&gt;not&lt;/strong&gt; retransmit lost packets, it is &lt;strong&gt;faster than TCP&lt;/strong&gt; and is used for &lt;strong&gt;real-time applications&lt;/strong&gt; like:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Live streaming&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Online gaming&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Video conferencing&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these cases, &lt;strong&gt;speed is more important than accuracy&lt;/strong&gt;. If a few packets are lost during a video call, you might notice a slight glitch, but the conversation continues without delays.  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Network Address Translation (NAT) and Private/Public IPs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In a &lt;strong&gt;LAN (Local Area Network)&lt;/strong&gt;, each device has a &lt;strong&gt;private IP address&lt;/strong&gt;, but they all share a &lt;strong&gt;common public IP address&lt;/strong&gt; assigned by the router when connecting to the internet.  &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Why do we need NAT?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;When multiple devices in a LAN request data from an external server, the router &lt;strong&gt;replaces the private IP address&lt;/strong&gt; of the sender with the &lt;strong&gt;public IP&lt;/strong&gt; before forwarding the request. However, when the response comes back, the router needs to determine &lt;strong&gt;which device in the LAN requested the data&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;NAT (Network Address Translation)&lt;/strong&gt; helps:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a request goes out, &lt;strong&gt;NAT assigns a unique port number&lt;/strong&gt; to each request.
&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;router stores a table&lt;/strong&gt; mapping these unique port numbers to the &lt;strong&gt;original private IP addresses&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;When the response comes back, &lt;strong&gt;NAT checks the port number&lt;/strong&gt; and forwards the data to the correct &lt;strong&gt;private IP&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Example&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Device A&lt;/strong&gt; (192.168.1.10) → Requests data from &lt;code&gt;example.com&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;NAT assigns &lt;strong&gt;Port 5001&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Sends request: &lt;code&gt;(Public IP, Port 5001) → example.com&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device B&lt;/strong&gt; (192.168.1.20) → Requests data from &lt;code&gt;example.com&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;NAT assigns &lt;strong&gt;Port 5002&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Sends request: &lt;code&gt;(Public IP, Port 5002) → example.com&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When the &lt;strong&gt;server&lt;/strong&gt; responds, the router looks at the &lt;strong&gt;port number&lt;/strong&gt; and forwards the response to the correct device.  &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Why Not Use MAC Addresses?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;MAC addresses &lt;strong&gt;change&lt;/strong&gt; when data moves across different networks. While MAC addresses work within a &lt;strong&gt;local network&lt;/strong&gt;, they &lt;strong&gt;cannot be used&lt;/strong&gt; for routing data over the internet because routers &lt;strong&gt;do not forward MAC addresses&lt;/strong&gt; beyond a local network. That’s why &lt;strong&gt;IP addresses&lt;/strong&gt; are used for identifying devices across different networks.  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Session Layer – Authentication &amp;amp; Authorization&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;session layer&lt;/strong&gt; manages user sessions, authentication, and authorization.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication&lt;/strong&gt;: Verifies &lt;strong&gt;who you are&lt;/strong&gt; (e.g., logging into Instagram with a username and password).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization&lt;/strong&gt;: Determines &lt;strong&gt;what you can do&lt;/strong&gt; (e.g., you can view your bank balance, but you cannot modify it unless you have special permissions).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also &lt;strong&gt;handles session checkpoints&lt;/strong&gt;, which allow downloads to resume from where they stopped instead of restarting from the beginning. &lt;strong&gt;Session hijacking&lt;/strong&gt; can occur if an attacker steals your &lt;strong&gt;session ID&lt;/strong&gt; (from cookies).  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Presentation Layer – Data Formatting &amp;amp; Security&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;presentation layer&lt;/strong&gt; ensures data is formatted correctly for different systems.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encryption/Decryption&lt;/strong&gt;: Secures communication (e.g., SSL/TLS encrypt HTTPS traffic).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Compression&lt;/strong&gt;: Reduces file sizes for faster transfer.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Translation&lt;/strong&gt;: Converts formats like &lt;strong&gt;ASCII to UTF-8&lt;/strong&gt; if two systems use different encoding.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Application Layer – User Interaction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;application layer&lt;/strong&gt; provides network services directly to users.  &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Common Protocols&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP (HyperText Transfer Protocol)&lt;/strong&gt;: Transfers web pages.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTPS (Secure HTTP)&lt;/strong&gt;: Encrypted web browsing.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP (Simple Mail Transfer Protocol)&lt;/strong&gt;: Sends emails.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FTP (File Transfer Protocol)&lt;/strong&gt;: Transfers files.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Network Transparency&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When users browse the internet, they only see the &lt;strong&gt;UI (User Interface)&lt;/strong&gt; of applications. They &lt;strong&gt;do not need to understand&lt;/strong&gt; the underlying network mechanisms handling requests, data transfer, and delivery.  &lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>Computer Network</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Fri, 04 Apr 2025 08:59:01 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/computer-network-p88</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/computer-network-p88</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;MAC Address (Media Access Control Address)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;MAC address&lt;/strong&gt;, also known as a &lt;strong&gt;physical address&lt;/strong&gt;, is a unique identifier assigned to a &lt;strong&gt;Network Interface Card (NIC)&lt;/strong&gt;. It &lt;strong&gt;cannot be changed&lt;/strong&gt; and is present in all internet-capable devices like &lt;strong&gt;computers, phones, printers, Wi-Fi routers, USB ports, and Bluetooth modules&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;Each device may have multiple NICs (e.g., Wi-Fi, USB, Bluetooth), meaning it can have multiple MAC addresses.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Structure of a MAC Address&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;MAC address&lt;/strong&gt; has a &lt;strong&gt;fixed format of 48 bits (6 bytes)&lt;/strong&gt;, divided into &lt;strong&gt;six portions&lt;/strong&gt; of &lt;strong&gt;8 bits each&lt;/strong&gt;. It is represented in &lt;strong&gt;hexadecimal (base 16)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00:00:00:00:00:00  →  FF:FF:FF:FF:FF:FF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are &lt;strong&gt;2^48 possible MAC addresses&lt;/strong&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MAC Address Distribution&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;MAC addresses are systematically assigned:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;First 24 bits&lt;/strong&gt; → Assigned to organizations (e.g., Google, Facebook).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Last 24 bits&lt;/strong&gt; → Assigned by the organization to its products.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, Google receives a block of &lt;strong&gt;2^24 MAC addresses&lt;/strong&gt; and assigns them to its devices.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Data Link Layer (DLL)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Data Link Layer (DLL)&lt;/strong&gt; ensures &lt;strong&gt;hop-to-hop delivery&lt;/strong&gt; using &lt;strong&gt;MAC addresses&lt;/strong&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How Devices Identify Each Other in a LAN&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;strong&gt;router assigns private IP addresses&lt;/strong&gt; to each device in a &lt;strong&gt;Local Area Network (LAN)&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;All devices share the &lt;strong&gt;same public IP&lt;/strong&gt; for external communication.
&lt;/li&gt;
&lt;li&gt;When &lt;strong&gt;Device A wants to send data to Device B&lt;/strong&gt;, it first needs &lt;strong&gt;B’s MAC address&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Address Resolution Protocol (ARP)&lt;/strong&gt; is used:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Device A broadcasts&lt;/strong&gt;: &lt;em&gt;"Who has this IP address? Send me your MAC address."&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device B responds&lt;/strong&gt; with its MAC address.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device A sends the data to B.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Use MAC Addresses When We Have IP Addresses?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;✔ &lt;strong&gt;IP addresses can change&lt;/strong&gt;, but MAC addresses remain fixed.&lt;br&gt;&lt;br&gt;
✔ MAC addresses work at &lt;strong&gt;Layer 2 (Data Link Layer)&lt;/strong&gt;, while IP addresses work at &lt;strong&gt;Layer 3 (Network Layer)&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
✔ &lt;strong&gt;Within a LAN&lt;/strong&gt;, data must be delivered to the exact device, requiring MAC addresses.&lt;br&gt;&lt;br&gt;
✔ When sending data &lt;strong&gt;outside the LAN&lt;/strong&gt;, the &lt;strong&gt;IP stays the same&lt;/strong&gt;, but the &lt;strong&gt;MAC address changes at each router&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;💡 &lt;em&gt;Think of it like parcel delivery:&lt;/em&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IP address&lt;/strong&gt; = Destination city
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MAC address&lt;/strong&gt; = Delivery truck (changes at each stop)
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MAC Addresses in Crime Investigation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Since &lt;strong&gt;IP addresses can change&lt;/strong&gt;, but &lt;strong&gt;MAC addresses remain fixed&lt;/strong&gt;, law enforcement can trace devices involved in cybercrimes—even if the IP is altered.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Functions of the Data Link Layer (DLL)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Framing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;📌 The &lt;strong&gt;Network Layer&lt;/strong&gt; sends data with &lt;strong&gt;source &amp;amp; destination IPs&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
📌 The &lt;strong&gt;DLL&lt;/strong&gt; adds &lt;strong&gt;source &amp;amp; destination MAC addresses&lt;/strong&gt;, creating a &lt;strong&gt;frame&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
📌 The receiver detects the data using a &lt;strong&gt;specific bit pattern&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It starts with &lt;strong&gt;7-bit synchronization bits (101010...1010)&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;When it sees &lt;strong&gt;11&lt;/strong&gt;, it knows &lt;strong&gt;real data is coming&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Error Detection &amp;amp; Handling&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;✔ The DLL &lt;strong&gt;detects errors&lt;/strong&gt; (e.g., signal corruption) and &lt;strong&gt;prevents faulty data&lt;/strong&gt; from being forwarded.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Flow Control&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A sender may send &lt;strong&gt;more data than a router can handle&lt;/strong&gt;, leading to &lt;strong&gt;packet loss&lt;/strong&gt;.   &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a router can handle only 100 packets at a time, sending 1,000 packets at once will result in data loss. 🚨&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Solution? ✅ Break it into 10 chunks of 100 packets to ensure smooth delivery! 📦📦📦.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The router then signals: &lt;em&gt;"Send more data, I’m ready."&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;em&gt;Real-world example:&lt;/em&gt; If you have &lt;strong&gt;100MB storage&lt;/strong&gt;, but send &lt;strong&gt;500MB&lt;/strong&gt;, it won’t fit. Instead, send it in chunks.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Access Control&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; In a &lt;strong&gt;bus topology&lt;/strong&gt;, multiple devices share a network wire. If two devices send data &lt;strong&gt;at the same time&lt;/strong&gt;, &lt;strong&gt;collisions occur&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; The DLL ensures &lt;strong&gt;only one device sends data at a time&lt;/strong&gt;, preventing &lt;strong&gt;corruption &amp;amp; congestion&lt;/strong&gt;.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Network Layer (NL)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Packet Routing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;✔ The &lt;strong&gt;Network Layer&lt;/strong&gt; decides the best path for data.&lt;br&gt;&lt;br&gt;
✔ &lt;strong&gt;Routers&lt;/strong&gt; guide packets from &lt;strong&gt;one router to another&lt;/strong&gt; until they reach their destination.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Fragmentation &amp;amp; Reassembly&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Some routers &lt;strong&gt;cannot handle large packets&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; A &lt;strong&gt;5MB packet&lt;/strong&gt; arrives at a router that can only handle &lt;strong&gt;2.5MB&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Network Layer splits data into smaller fragments&lt;/strong&gt; before sending it.
&lt;/li&gt;
&lt;li&gt;At the destination, fragments are &lt;strong&gt;reassembled into the original data&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Congestion Control&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Too many devices send data at once, &lt;strong&gt;slowing down the network&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; The Network Layer:&lt;br&gt;&lt;br&gt;
✔ &lt;strong&gt;Limits the number of packets&lt;/strong&gt; sent to avoid congestion.&lt;br&gt;&lt;br&gt;
✔ &lt;strong&gt;Manages bandwidth&lt;/strong&gt; (controls data transfer speed).  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;✔ &lt;strong&gt;MAC addresses&lt;/strong&gt; are fixed, while &lt;strong&gt;IP addresses can change&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
✔ &lt;strong&gt;The Data Link Layer&lt;/strong&gt; ensures &lt;strong&gt;error handling, access control, &amp;amp; framing&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
✔ &lt;strong&gt;The Network Layer&lt;/strong&gt; handles &lt;strong&gt;routing, fragmentation, &amp;amp; congestion control&lt;/strong&gt;.  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>React useState Hook</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Thu, 03 Apr 2025 20:45:06 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/react-usestate-hook-1m92</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/react-usestate-hook-1m92</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstyjsas7om8j3vgbtdoo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstyjsas7om8j3vgbtdoo.png" alt="Image description" width="666" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In React, to create a UI with two buttons (Increment and Decrement), we use the onClick event instead of addEventListener in JavaScript. The function can be defined separately or directly inside {} in JSX.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Doesn't UI Update Initially?&lt;/strong&gt;&lt;br&gt;
When clicking the buttons, console.log(count) shows the value changing, but the UI does not update. In vanilla JavaScript, we manually manipulate the DOM to reflect the changes. While we can do the same in React by selecting tags and updating them, React handles DOM manipulation internally, keeping our code clean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Clean Code Matters?&lt;/strong&gt;&lt;br&gt;
Direct DOM manipulation results in repeated code, especially when updating the same variable across multiple elements.&lt;/p&gt;

&lt;p&gt;React simplifies this by introducing hooks, which are just functions.&lt;/p&gt;

&lt;p&gt;The most commonly used hook is useState.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding useState&lt;/strong&gt;&lt;br&gt;
When a variable's value changes, we say its state is changing. The useState hook helps manage state in React.&lt;br&gt;
const [count, setCount] = useState(0);&lt;/p&gt;

&lt;p&gt;useState(0) returns an array with two values:&lt;/p&gt;

&lt;p&gt;count → initialized with 0.&lt;/p&gt;

&lt;p&gt;setCount → a function to update count.&lt;/p&gt;

&lt;p&gt;Why a function? Instead of direct manipulation, React says:&lt;br&gt;
"Call my function (setCount(newValue)) whenever you update the variable, and I’ll handle UI updates automatically."&lt;/p&gt;

&lt;p&gt;How does React update the UI?&lt;/p&gt;

&lt;p&gt;When setCount(count) is called, React rerenders the component.&lt;/p&gt;

&lt;p&gt;Any JSX that uses count will automatically show the updated value.&lt;/p&gt;

&lt;p&gt;This prevents repetitive code for updating the UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Sorting in JavaScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [10, 2, 5, 1];  
arr.sort((a, b) =&amp;gt; a - b);  
console.log(arr); // Output: [1, 2, 5, 10]

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

&lt;/div&gt;



&lt;p&gt;If a - b is negative, a comes first.&lt;/p&gt;

&lt;p&gt;If positive, b comes first.&lt;/p&gt;

&lt;p&gt;The useState Twist 🚀&lt;br&gt;
React prevents unnecessary re-renders to optimize performance. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [8, 5, 9, 7, 4];  
const [temp, setArr] = useState(arr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;temp stores the array and is displayed in the UI.&lt;/p&gt;

&lt;p&gt;Suppose we sort it and update state:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;temp.sort((a, b) =&amp;gt; a - b);&lt;br&gt;
setArr(temp);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Issue: UI doesn’t update.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbt90g2bsg17r7ur7v3aq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbt90g2bsg17r7ur7v3aq.png" alt="Image description" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript stores primitive values in the stack, but arrays &amp;amp; objects are stored in the heap.&lt;/p&gt;

&lt;p&gt;The stack only holds a reference (address) to the heap’s array.&lt;/p&gt;

&lt;p&gt;After sorting, the same reference remains, so React sees no change and doesn’t rerender.&lt;/p&gt;

&lt;p&gt;Solution? Clone the array!&lt;br&gt;
setArr([...temp]); // Creates a new array with a different address.&lt;/p&gt;

&lt;p&gt;Now, React detects the state change and updates the UI.&lt;/p&gt;

&lt;p&gt;filter() in JavaScript&lt;br&gt;
Returns a new array based on a condition.&lt;/p&gt;

&lt;p&gt;React’s power lies in efficient rendering, avoiding unnecessary DOM updates and keeping performance optimized.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Today's Learning Update: Backend Basics</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Wed, 02 Apr 2025 22:11:30 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/todays-learning-update-backend-basics-3il4</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/todays-learning-update-backend-basics-3il4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Storing Images in a Database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We don’t store the actual image in the database; instead, we store its link while the image is hosted elsewhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Collections, Schemas &amp;amp; Databases&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Collection → Similar to a table in SQL.&lt;/p&gt;

&lt;p&gt;Schema → Defines the fields stored in a collection (e.g., id, name, email).&lt;/p&gt;

&lt;p&gt;Database (DB) → A container for all collections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP Methods &amp;amp; Postman&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;POST → Sends data to the database (used for creating records).&lt;/p&gt;

&lt;p&gt;GET → Fetches data from the database.&lt;/p&gt;

&lt;p&gt;Postman → A tool to send requests to the backend and check responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backend Function Calls&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we send a POST/GET request from Postman, it's like calling a function.&lt;/p&gt;

&lt;p&gt;These functions must be defined in our backend (JS file).&lt;/p&gt;

&lt;p&gt;The JS file acts as a medium that queries the database to store or retrieve data.&lt;/p&gt;

&lt;p&gt;Postman is like a frontend where we send and request data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Destructuring &amp;amp; Spread Operator in JavaScript&lt;/strong&gt;&lt;br&gt;
Destructuring&lt;br&gt;
&lt;code&gt;const obj = { name: "John Doe", email: "john@example.com" };&lt;br&gt;
const { name, email } = obj;  &lt;br&gt;
console.log(name);  // John Doe&lt;br&gt;
console.log(email); // john@example.com&lt;/code&gt;&lt;br&gt;
Makes it easier to access object properties directly.&lt;br&gt;
Spread Operator (...)&lt;br&gt;
&lt;code&gt;const arr1 = [1, 2, 3, 4];&lt;br&gt;
const arr2 = [5, 6];&lt;br&gt;
const arr3 = [...arr1, ...arr2];  &lt;br&gt;
console.log(arr3); // [1, 2, 3, 4, 5, 6]&lt;/code&gt;&lt;br&gt;
Combines multiple arrays/objects into one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User Registration &amp;amp; Validations&lt;/strong&gt;&lt;br&gt;
Validations ensure that only correct data enters the database (e.g., email format, age limit).&lt;/p&gt;

&lt;p&gt;When updating an entry, we need to explicitly enable validation:&lt;br&gt;
runValidators: true&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Timestamps (createdAt, updatedAt)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Track when a user registered and when they last updated their profile.&lt;/p&gt;

&lt;p&gt;Platforms like Instagram use this to filter users based on registration date (e.g., last 7 days, 1 month).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Immutable Fields&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some fields cannot be changed after creation (e.g., emailID).&lt;/p&gt;

&lt;p&gt;Must be enforced at the schema level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;API-Level Validation&lt;/strong&gt;&lt;br&gt;
API acts as a middleware between Postman (client) and the database (server).&lt;/p&gt;

&lt;p&gt;API validation checks user data before sending it to the database to:&lt;/p&gt;

&lt;p&gt;Avoid unnecessary DB calls (reducing cost &amp;amp; improving performance).&lt;/p&gt;

&lt;p&gt;Enhance user experience by preventing delays.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>My Learning(what is component in react)?</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Tue, 01 Apr 2025 13:20:05 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/my-learningwhat-is-component-in-react-5776</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/my-learningwhat-is-component-in-react-5776</guid>
      <description>&lt;ul&gt;
&lt;li&gt;I was making a React project—a Myntra clone—so I learned about React components from that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I created the Header, Body, and Footer components and rendered all of them inside the App component.&lt;/p&gt;

&lt;p&gt;Currently, all these components are in a single file named App.js, making the code complex to read and understand.&lt;/p&gt;

&lt;p&gt;What I learned: When working in a company, we do not write all code in one file. It is not a good practice. That’s why the concept of modules was introduced.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What is a module?&lt;/strong&gt;
A module is something that you will use repeatedly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is just like a function that should be kept in a separate JavaScript file.&lt;/p&gt;

&lt;p&gt;This is what we call a component in React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- What is a component?&lt;/strong&gt;&lt;br&gt;
In React, a component is simply a function written in a separate file, which is then exported and imported as needed.&lt;/p&gt;

&lt;p&gt;For example, create a components folder and move all component files (e.g., Header.js, Footer.js, etc.) into it.&lt;/p&gt;

&lt;p&gt;You can name your files as you like, but it is advisable to name them the same as the function name for better readability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Exporting Components&lt;/strong&gt;&lt;br&gt;
There are two ways to export a function in React:&lt;/p&gt;

&lt;p&gt;Write export default functionName at the end of the file.&lt;/p&gt;

&lt;p&gt;Write export default before defining the function.&lt;/p&gt;

&lt;p&gt;export default function Header() { ... }&lt;/p&gt;

&lt;p&gt;-** File Extensions**&lt;br&gt;
You can use either .js or .jsx extensions while creating files in React.&lt;/p&gt;

&lt;p&gt;It is advisable to use .jsx if you are writing JSX in your code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Handling Dummy Data&lt;/strong&gt;
Any dummy data (data you are fetching for UI development) should be kept inside a folder named utils.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-** Important Rules I Learned**&lt;br&gt;
Variable Export Rule:&lt;/p&gt;

&lt;p&gt;In JavaScript, you cannot write export default before variable declarations like let, const, or var.&lt;/p&gt;

&lt;p&gt;Incorrect:export default const arr = [1, 2, 3]; &lt;br&gt;
Correct:const arr = [1, 2, 3]; &lt;br&gt;
export default arr;&lt;/p&gt;

&lt;p&gt;Only One Default Export per File:&lt;/p&gt;

&lt;p&gt;A file can have only one export default.&lt;/p&gt;

&lt;p&gt;If you need to export multiple items (functions, arrays, objects), use named exports instead.&lt;/p&gt;

&lt;p&gt;Example of named export:&lt;br&gt;
export { Header, Footer, arr1, arr2 };&lt;/p&gt;

&lt;p&gt;Corresponding import:import { Header, Footer, arr1, arr2 } &lt;br&gt;
If you use export default, you can import it without curly braces&lt;br&gt;
If you use export (without default), you must use curly braces when importing.&lt;br&gt;
You can also rename an imported component using as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Why Use React?&lt;/strong&gt;
React gives developers control over the UI while handling DOM manipulation efficiently.
This is why we use ReactDOM.createRoot(), as React takes care of updating the DOM optimally.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>2D Arrays</title>
      <dc:creator>yashi srivastava</dc:creator>
      <pubDate>Mon, 31 Mar 2025 20:57:15 +0000</pubDate>
      <link>https://forem.com/yashi_srivastava_9166487c/2d-arrays-21g7</link>
      <guid>https://forem.com/yashi_srivastava_9166487c/2d-arrays-21g7</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fripf9kt9w8wbq0os1v4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fripf9kt9w8wbq0os1v4m.png" alt="Image description" width="635" height="933"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6wtg8a47cs0fv2jct7dg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6wtg8a47cs0fv2jct7dg.png" alt="Image description" width="577" height="907"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today, I solved the Rotate Image by 90° Clockwise problem. Initially, I attempted to solve it using extra space, but then I tried to optimize it without using additional space. Although I couldn't fully optimize it without space yet, it was a valuable learning experience.&lt;/p&gt;

&lt;p&gt;Before this, I had learned about row-major and column-major ordering and how data is stored. While databases typically use column-major order, the actual in-memory storage of a matrix is in a linear form, not organized in row-major or column-major order. This means the matrix is stored as a contiguous block of memory, and we can calculate the corresponding index for an element using its position in this linearized structure.&lt;/p&gt;

&lt;p&gt;I also practiced matrix transposition:&lt;/p&gt;

&lt;p&gt;First, I transposed the matrix using extra space.&lt;/p&gt;

&lt;p&gt;Then, I learned how to transpose a matrix without extra space by swapping elements in place.&lt;/p&gt;

&lt;p&gt;Additionally, I learned how to reverse rows in a matrix without using extra space. This was a great exercise in optimizing space complexity while performing matrix manipulations.&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀_&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
