<?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: Rishabh Joshi</title>
    <description>The latest articles on Forem by Rishabh Joshi (@joshi16).</description>
    <link>https://forem.com/joshi16</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%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png</url>
      <title>Forem: Rishabh Joshi</title>
      <link>https://forem.com/joshi16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/joshi16"/>
    <language>en</language>
    <item>
      <title>Learn how to drastically improve your React app’s performance using lazy loading and `React.Suspense`. This guide covers route-level code splitting, loading fallbacks, best practices, and real-world benefits for Core Web Vitals.</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sat, 17 May 2025 10:48:42 +0000</pubDate>
      <link>https://forem.com/joshi16/learn-how-to-drastically-improve-your-react-apps-performance-using-lazy-loading-and-18am</link>
      <guid>https://forem.com/joshi16/learn-how-to-drastically-improve-your-react-apps-performance-using-lazy-loading-and-18am</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/joshi16/boost-react-performance-with-lazy-loading-suspense-364c" class="crayons-story__hidden-navigation-link"&gt;⚡ Boost React Performance with Lazy Loading + Suspense&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/joshi16" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" alt="joshi16 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/joshi16" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Rishabh Joshi
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Rishabh Joshi
                
              
              &lt;div id="story-author-preview-content-2494800" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/joshi16" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Rishabh Joshi&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/joshi16/boost-react-performance-with-lazy-loading-suspense-364c" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;May 17 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/joshi16/boost-react-performance-with-lazy-loading-suspense-364c" id="article-link-2494800"&gt;
          ⚡ Boost React Performance with Lazy Loading + Suspense
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/react"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;react&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/performance"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;performance&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/joshi16/boost-react-performance-with-lazy-loading-suspense-364c" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;2&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/joshi16/boost-react-performance-with-lazy-loading-suspense-364c#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            4 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>react</category>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
    <item>
      <title>⚡ Boost React Performance with Lazy Loading + Suspense</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sat, 17 May 2025 10:47:31 +0000</pubDate>
      <link>https://forem.com/joshi16/boost-react-performance-with-lazy-loading-suspense-364c</link>
      <guid>https://forem.com/joshi16/boost-react-performance-with-lazy-loading-suspense-364c</guid>
      <description>&lt;h2&gt;
  
  
  ⚙️ Why Optimize React Apps?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🧠 The Problem
&lt;/h3&gt;

&lt;p&gt;In traditional React setups, all JavaScript code is bundled (using tools like Vite or Webpack) into a few files, often a single large &lt;code&gt;main.js&lt;/code&gt; or &lt;code&gt;bundle.js&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On initial page load, the browser must download, parse, and execute this entire bundle before the application becomes interactive.&lt;/li&gt;
&lt;li&gt;Larger applications lead to larger bundles → slower &lt;em&gt;Time-to-Interactive (TTI)&lt;/em&gt; and worse &lt;em&gt;Largest Contentful Paint (LCP)&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Impact&lt;/strong&gt;: Slower apps cause user frustration, higher bounce rates, and potentially worse &lt;em&gt;SEO&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🎯 The Goal
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Significantly reduce the initial JavaScript payload.&lt;/li&gt;
&lt;li&gt;Improve &lt;em&gt;Time-to-Interactive (TTI)&lt;/em&gt;, making the app usable faster.&lt;/li&gt;
&lt;li&gt;Enhance perceived performance and overall user experience, particularly on mobile and slow network connections.&lt;/li&gt;
&lt;li&gt;Reduce memory consumption on the client-side by not loading unnecessary code&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📦 What is Lazy Loading?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Lazy loading&lt;/strong&gt; is a strategy for code splitting, where you break down your application's JavaScript bundle into smaller, manageable chunks. These chunks are then loaded on-demand, typically when a user navigates to a specific route or interacts with a particular feature, rather than all at once during the initial load.&lt;/p&gt;

&lt;h3&gt;
  
  
  📉 Before Lazy Loading:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A monolithic &lt;code&gt;bundle.js&lt;/code&gt; contains code for all components, pages, and libraries.&lt;/li&gt;
&lt;li&gt;Initial load downloads everything, including code for pages or features the user might never visit or use.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Analogy&lt;/em&gt;: Ordering every dish on the menu when you first sit down at a restaurant, even if you only plan to eat an appetizer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📈 After Lazy Loading:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The initial bundle &lt;code&gt;(main.chunk.js)&lt;/code&gt; is much smaller, containing only the essential code for the initial view.&lt;/li&gt;
&lt;li&gt;Additional &lt;code&gt;components/pages/features&lt;/code&gt; are split into separate chunks (e.g., &lt;code&gt;about.chunk.js&lt;/code&gt;, &lt;code&gt;UserProfile.chunk.js&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;These chunks are loaded dynamically via network requests when the corresponding component is needed.&lt;/li&gt;
&lt;li&gt;This leads to smaller initial bundle sizes and significantly faster application startup.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 How It Works in React
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🛠️ &lt;code&gt;React.lazy()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Introduced in React 16.6.&lt;/li&gt;
&lt;li&gt;A function that lets you render a dynamically imported component as a regular component.&lt;/li&gt;
&lt;li&gt;It takes a function that must call a dynamic &lt;code&gt;import()&lt;/code&gt;. This &lt;code&gt;import()&lt;/code&gt; call returns a Promise which resolves to a module with a default export containing the React component.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;React.lazy()&lt;/code&gt; leverages the browser's native &lt;code&gt;import()&lt;/code&gt; function, which dynamically loads modules and returns a Promise. This is key to enabling true asynchronous code loading in React apps.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ./pages/Homepage.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Homepage&lt;/span&gt; &lt;span class="o"&gt;=&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome to the Homepage!&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Homepage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ./App.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Homepage&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;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/Homepage&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;blockquote&gt;
&lt;p&gt;💡 &lt;code&gt;React.lazy&lt;/code&gt; currently only supports default exports. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ⏳ What is Suspense?
&lt;/h2&gt;

&lt;p&gt;React’s &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; component is essential for lazy loading. It lets you specify a fallback UI (like a loading spinner) to display while the lazy-loaded component's code is being fetched over the network and prepared.&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="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Suspense&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Spinner&lt;/span&gt; &lt;span class="o"&gt;=&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Loading...&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="c1"&gt;// Your loading indicator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Homepage&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;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/Homepage&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="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;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My App&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;&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="na"&gt;fallback&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;Spinner&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;Homepage&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;Suspense&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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔄 Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Lazy loading React components: The primary use case discussed here.&lt;/li&gt;
&lt;li&gt;Data fetching: While not built-in for all data fetching, Suspense is designed to work with data fetching libraries that support it (e.g., Relay, or with custom hooks in React 18+ for server components and use hook). This allows showing fallbacks while data is loading, similar to components.&lt;/li&gt;
&lt;li&gt;Coordinating loading states: Suspense can manage loading states for multiple asynchronous operations.&lt;/li&gt;
&lt;li&gt;Data fetching: &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; can show fallback UIs while data is loading — but this requires specific integrations. Before React 18, this was mainly supported via libraries like Relay. With React 18, new patterns (like the &lt;code&gt;use()&lt;/code&gt; hook and Server Components) allow Suspense to manage data-fetching more directly in compatible frameworks like &lt;strong&gt;Next.js&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Lazy Loading + Suspense with React Router
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Route-Level Code Splitting
&lt;/h3&gt;

&lt;p&gt;This is the most common and impactful application of lazy loading. By splitting your code based on routes, users only download the code for the page they are currently viewing.&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;lazy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Suspense&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="s1"&gt;react&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="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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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;Spinner&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./components/Spinner&lt;/span&gt;&lt;span class="dl"&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;Homepage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/Homepage&lt;/span&gt;&lt;span class="dl"&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;About&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/About&lt;/span&gt;&lt;span class="dl"&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;Contact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/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;Suspense&lt;/span&gt; &lt;span class="na"&gt;fallback&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;Spinner&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;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;Homepage&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;"/about"&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;About&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;Suspense&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Where to place &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;High-level placement: Wrap your entire &lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt; block for a consistent fallback.&lt;/li&gt;
&lt;li&gt;Granular placement: Use multiple &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; components to show different loaders for different parts of the UI.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧭 Final Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lazy loading&lt;/strong&gt; is a powerful technique for drastically improving initial load performance by reducing the amount of JavaScript downloaded upfront.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Suspense&lt;/strong&gt; is its essential companion, ensuring a smooth user experience by providing fallbacks during asynchronous loading states.&lt;/li&gt;
&lt;li&gt;Combining these with React Router for route-based code splitting is the most impactful optimization for many React applications.&lt;/li&gt;
&lt;li&gt;Always include &lt;em&gt;Error Boundaries&lt;/em&gt; to make your lazy loading robust.&lt;/li&gt;
&lt;li&gt;Leverage bundler features and browser developer tools to implement, verify, and measure the benefits.&lt;/li&gt;
&lt;li&gt;Implement these strategies in your production-ready apps to significantly improve real-world performance metrics and user satisfaction.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;💬 Have questions or tips on Performance? Drop them in the comments below!&lt;/p&gt;

&lt;p&gt;Likes, Comments and feedback are welcome!😅&lt;/p&gt;

&lt;p&gt;Happy coding!⚛️&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Master Routing in React with React Router — learn how client-side routing works, build nested and dynamic routes, and navigate programmatically using real-world examples and clear explanations.</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Fri, 09 May 2025 10:04:35 +0000</pubDate>
      <link>https://forem.com/joshi16/master-routing-in-react-with-react-router-learn-how-client-side-routing-works-build-nested-and-3ck3</link>
      <guid>https://forem.com/joshi16/master-routing-in-react-with-react-router-learn-how-client-side-routing-works-build-nested-and-3ck3</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/joshi16/react-router-basics-to-advanced-v6-1h9l" class="crayons-story__hidden-navigation-link"&gt;React Router Basics to Advanced (v6+)&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/joshi16" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" alt="joshi16 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/joshi16" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Rishabh Joshi
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Rishabh Joshi
                
              
              &lt;div id="story-author-preview-content-2440158" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/joshi16" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Rishabh Joshi&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/joshi16/react-router-basics-to-advanced-v6-1h9l" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;May 9 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/joshi16/react-router-basics-to-advanced-v6-1h9l" id="article-link-2440158"&gt;
          React Router Basics to Advanced (v6+)
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/react"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;react&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/router"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;router&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/joshi16/react-router-basics-to-advanced-v6-1h9l" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;1&lt;span class="hidden s:inline"&gt; reaction&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/joshi16/react-router-basics-to-advanced-v6-1h9l#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              1&lt;span class="hidden s:inline"&gt; comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            7 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>react</category>
      <category>webdev</category>
      <category>router</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React Router Basics to Advanced (v6+)</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Fri, 09 May 2025 09:55:15 +0000</pubDate>
      <link>https://forem.com/joshi16/react-router-basics-to-advanced-v6-1h9l</link>
      <guid>https://forem.com/joshi16/react-router-basics-to-advanced-v6-1h9l</guid>
      <description>&lt;h2&gt;
  
  
  What is ROUTING?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Routing refers to the process of navigating between different pages or views in a single-page application (SPA) without reloading the entire page.&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It keeps the UI in sync with the current browser URL.&lt;/li&gt;
&lt;li&gt;Allows us to build Single Page Applications (SPA).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why Routing in React?
&lt;/h2&gt;

&lt;p&gt;React apps are SPAs, meaning there's only one HTML file (&lt;code&gt;index.html&lt;/code&gt;), but multiple views (pages) that users can navigate to. Routing helps manage these views by matching the URL to the correct component.&lt;/p&gt;

&lt;p&gt;It &lt;strong&gt;enables client-side navigation without full-page reloads&lt;/strong&gt;, allowing users to move &lt;strong&gt;smoothly&lt;/strong&gt; between URLs like &lt;code&gt;/home&lt;/code&gt;, &lt;code&gt;/about&lt;/code&gt;, or &lt;code&gt;/contact&lt;/code&gt;, each displaying different components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React doesn’t have built-in routing, so we use a library like React Router (react-router-dom) for handling routing.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What is a Single Page Application (SPA)?
&lt;/h2&gt;

&lt;p&gt;A Single Page Application (SPA) dynamically updates the page without reloading it, providing a seamless user experience.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runs entirely in the browser (Client Side).&lt;/li&gt;
&lt;li&gt;Different URLs load different React components.&lt;/li&gt;
&lt;li&gt;Uses JavaScript (React) to update the UI dynamically.&lt;/li&gt;
&lt;li&gt;Fetches additional data via APIs (e.g., from a backend server).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;When the user clicks a link, the URL updates, and React dynamically loads the corresponding component.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  React Router
&lt;/h2&gt;

&lt;p&gt;React Router is the most popular library to handle navigation inside a React app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enables URL-based navigation.&lt;/li&gt;
&lt;li&gt;Changes browser URLs without refreshing the page.&lt;/li&gt;
&lt;li&gt;Keeps the UI in sync with the URL.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔧 Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Defining Routes
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;With routing, we match different URLs to different UI views. In the case of React, we match a URL to a specific React Component, and we call each of these matches&lt;/code&gt; &lt;strong&gt;&lt;code&gt;routes&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Since React Router v6.4, there are two main ways to define routes. We'll focus on the traditional declarative way.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;About&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;./About&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="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* 🚀 Enables routing */&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;Routes&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;/* 🚦 Manages route paths */&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;Route&lt;/span&gt; &lt;span class="na"&gt;index&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="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Default route */&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;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;"/about"&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;About&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Nav&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;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="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Link to Home */&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;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;"/about"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;About&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="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Link to About */&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;nav&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Concepts 🔑
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt; – The Router Provider&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's the top-level component that enables routing in your React application.&lt;/li&gt;
&lt;li&gt;It leverages the HTML5 History API to keep your UI synchronized with the browser URL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Crucially, without &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt;, React Router won't be able to manage your application's routes.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt; – Routing Container&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This component acts as a container for your individual &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; definitions.&lt;/li&gt;
&lt;li&gt;It intelligently iterates through its &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; children and renders the &lt;strong&gt;first one&lt;/strong&gt; whose &lt;code&gt;path&lt;/code&gt; matches the current browser URL.&lt;/li&gt;
&lt;li&gt;Think of &lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt; as a central highway dispatcher, directing traffic to the appropriate exits.&lt;/li&gt;
&lt;li&gt;It's the successor to the older &lt;code&gt;&amp;lt;Switch&amp;gt;&lt;/code&gt; component in previous versions of React Router.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; – Define a Path and Component&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; component serves as a mapping between a specific URL path and a corresponding React component.&lt;/li&gt;
&lt;li&gt;When the browser URL matches the &lt;code&gt;path&lt;/code&gt; of a &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt;, that route's &lt;code&gt;element&lt;/code&gt; (the React component) is rendered.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index&lt;/code&gt; prop defines the default child route (instead of &lt;code&gt;path="/"&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Page Not Found Handling:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&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;Route&lt;/span&gt; &lt;span class="na"&gt;index&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;"*"&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;PageNotFound&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;/code&gt;&lt;/pre&gt;


&lt;p&gt;The &lt;code&gt;path="*"&lt;/code&gt; acts as a &lt;strong&gt;catch-all&lt;/strong&gt;. If no other defined route matches the current URL, this route will be matched, and the &lt;code&gt;&amp;lt;PageNotFound&amp;gt;&lt;/code&gt; component will be displayed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;NavLink&amp;gt;&lt;/code&gt; / &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; – Navigation Without Reload&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These components are your primary tools for navigating between different routes within your SPA &lt;strong&gt;without triggering a full page reload&lt;/strong&gt; from the server.&lt;/li&gt;
&lt;li&gt;They render as standard &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags in the HTML but intercept the click event to perform client-side navigation managed by React Router.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;NavLink&amp;gt;&lt;/code&gt; vs. &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt;:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;NavLink&amp;gt;&lt;/code&gt; is a specialized version of &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; that provides built-in styling capabilities for the currently active route.&lt;/li&gt;
&lt;li&gt;It automatically applies an &lt;code&gt;active&lt;/code&gt; class (by default) to the rendered link when its &lt;code&gt;to&lt;/code&gt; prop matches the current URL.&lt;/li&gt;
&lt;li&gt;You can customize this behavior using the &lt;code&gt;className&lt;/code&gt; or &lt;code&gt;style&lt;/code&gt; props, or by using the &lt;code&gt;isActive&lt;/code&gt; prop for more fine-grained control.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&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;NavLink&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;Navigation&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;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;NavLink&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;NavLink&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;NavLink&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;"/about"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;NavLink&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;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Nested Routes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As your application grows, you might encounter scenarios where certain sections have their own sub-navigation and views. This is where nested routes come in handy.&lt;/li&gt;
&lt;li&gt;Imagine a &lt;code&gt;/dashboard&lt;/code&gt; section with sub-pages like &lt;code&gt;/dashboard/profile&lt;/code&gt; and &lt;code&gt;/dashboard/settings&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To implement this, you define child &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; components within a parent &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The key to rendering the content of these nested routes is the &lt;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt; component in the parent route's component.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&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;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;"profile"&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;Profile&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;"settings"&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;Settings&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;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;Outlet&amp;gt;&lt;/code&gt; in React Router&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;&amp;lt;Outlet&amp;gt;&lt;/code&gt; component serves as a &lt;strong&gt;placeholder&lt;/strong&gt; in the component rendered by a parent route.&lt;/li&gt;
&lt;li&gt;When a child route of that parent route matches the current URL, its corresponding component is rendered &lt;strong&gt;inside&lt;/strong&gt; the &lt;code&gt;&amp;lt;Outlet&amp;gt;&lt;/code&gt; within the parent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why is &lt;code&gt;&amp;lt;Outlet&amp;gt;&lt;/code&gt; essential?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;It enables you to create &lt;strong&gt;reusable layouts&lt;/strong&gt;. Common UI elements like navigation bars, sidebars, and footers can be defined in the parent component, while the specific content for the child routes is dynamically injected into the &lt;code&gt;&amp;lt;Outlet&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This promotes a clean and organized component structure.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&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;Outlet&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Dashboard&lt;/span&gt; &lt;span class="o"&gt;=&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="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;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;"/profile"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Profile&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;"/settings"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Settings&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="nt"&gt;hr&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;/* Child route components will be rendered 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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Dashboard&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Dynamic Routes with URL Parameters
&lt;/h2&gt;

&lt;p&gt;Dynamic routes allow you to create routes that can handle variable parts of the URL. This is useful for displaying details of a specific item, like a product or a user profile.&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;useParams&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="s1"&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;ProductDetails&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useParams&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;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Product ID: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;productId&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="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;"/products/:productId"&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;ProductDetails&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useParams()&lt;/code&gt; hook: This React Router hook allows you to access the dynamic parameters from the current URL. You can call this hook within the functional component rendered by the dynamic route (in this case, ProductDetails). It returns an object where the keys are the parameter names defined in the path (e.g., productId) and the values are the corresponding segments from the URL.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/:productId&lt;/code&gt;: This syntax within the path prop of the &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; defines a dynamic segment. Any value in this position of the URL will be captured as the productId parameter. For example, if the URL is &lt;code&gt;/products/42&lt;/code&gt;, the &lt;code&gt;useParams()&lt;/code&gt; hook in ProductDetails will return { productId: "42" }.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Programmatic Navigation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;useNavigate&lt;/code&gt; Hook
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;useNavigate&lt;/code&gt; hook allows you to programmatically navigate to different routes. This is often used after actions like form submissions or for handling authentication redirects.&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;useNavigate&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="s1"&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;Home&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;navigate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useNavigate&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&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="nf"&gt;navigate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&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="k"&gt;return&lt;/span&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;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Go to About&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;&amp;lt;Navigate /&amp;gt;&lt;/code&gt; Component
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;Navigate /&amp;gt;&lt;/code&gt; component is used to redirect the user to a different route, often based on certain conditions like user authentication&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;Navigate&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="s1"&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;ProtectedRoute&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&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="nc"&gt;Navigate&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;"/login"&lt;/span&gt; &lt;span class="na"&gt;replace&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;children&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;
&lt;code&gt;replace&lt;/code&gt; prop: Replaces the current entry in the history stack.&lt;/li&gt;
&lt;li&gt;This is useful for auth checks and redirection.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How Routing Changes Component Structure in React
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Without Router (Traditional Component Structure)&lt;/strong&gt;
Normally, in a simple React app (without a router), we structure components inside the App component like this:
&lt;/li&gt;
&lt;/ul&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="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="nc"&gt;Header&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;Main&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;Footer&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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, App acts like a parent container that holds all components, and all of them are typically rendered together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When React Router Comes Into Play&lt;/strong&gt;
When we introduce React Router, the way we structure App changes. Instead of keeping all components directly inside App, we primarily keep the &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; configurations there. The actual page content is rendered by the components matched by the routes.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Routing Controls What to Show&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before Router&lt;/strong&gt;: App always renders everything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;With Router&lt;/strong&gt;: Different pages (routes) should load only when needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Why is this better? 📌&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improved Performance&lt;/strong&gt;: Only the required components for the current route are loaded and rendered, reducing the initial load time and improving overall performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smoother User Experience&lt;/strong&gt;: Navigation between different views happens without full page reloads, providing a more seamless and faster user experience.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Understanding React Router with a Highway Analogy
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;React Router Concept&lt;/th&gt;
&lt;th&gt;Real-World Analogy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;BrowserRouter&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🛣️ The &lt;strong&gt;road network&lt;/strong&gt; that enables travel. Without it, there are no roads.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;Routes&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🚗 The &lt;strong&gt;highway&lt;/strong&gt; with multiple exits (routes).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;Route&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🛑 A &lt;strong&gt;specific exit&lt;/strong&gt; that leads to a destination (component).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;Link&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🚦 &lt;strong&gt;Signboards&lt;/strong&gt; that direct users to different routes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🛣️ &lt;strong&gt;The location on the highway&lt;/strong&gt; where nested exits merge.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Without &lt;code&gt;BrowserRouter&lt;/code&gt;, there are no roads!&lt;/li&gt;
&lt;li&gt;Without &lt;code&gt;Routes&lt;/code&gt;, there are no exits!&lt;/li&gt;
&lt;li&gt;Without &lt;code&gt;Route&lt;/code&gt;, you can’t reach your destination!&lt;/li&gt;
&lt;li&gt;Without &lt;code&gt;Outlet&lt;/code&gt;, nested routes have nowhere to render.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✨ Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Routing is the backbone of navigation in modern React applications. It allows us to create multi-page experiences without full page reloads, making our apps feel faster, more dynamic, and more like native applications.&lt;/p&gt;

&lt;p&gt;With React Router, we can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Map URLs to components easily.&lt;/li&gt;
&lt;li&gt;Enable smooth client-side navigation.&lt;/li&gt;
&lt;li&gt;Organize large applications into clean, scalable route structures.&lt;/li&gt;
&lt;li&gt;Enhance user experience with dynamic routing, nested layouts, protected routes, and programmatic navigation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you build more advanced React apps, mastering routing will unlock a whole new level of power and flexibility in your projects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In short, React Router bridges the gap between a simple app and a fully functional web experience. 🚀&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  📚 What's Next?
&lt;/h2&gt;

&lt;p&gt;This post focused on the traditional declarative routing using React Router 6.&lt;br&gt;
However, to keep things clean and digestible, I’ll cover some advanced topics separately in the next post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using Suspense with React Router (for lazy loading routes) ⚡&lt;/li&gt;
&lt;li&gt;New Features and Changes in React Router v7🆕&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay tuned for a deep dive into modern routing patterns and performance optimization techniques coming soon!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>router</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Exploring React's useReducer hook, an alternative to useState that’s perfect for managing complex state logic in your components.</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sat, 12 Apr 2025 14:12:29 +0000</pubDate>
      <link>https://forem.com/joshi16/exploring-reacts-usereducer-hook-an-alternative-to-usestate-thats-perfect-for-managing-complex-1a1m</link>
      <guid>https://forem.com/joshi16/exploring-reacts-usereducer-hook-an-alternative-to-usestate-thats-perfect-for-managing-complex-1a1m</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/joshi16/understanding-usereducer-in-react-3534" class="crayons-story__hidden-navigation-link"&gt;🧠Understanding useReducer in React&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/joshi16" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" alt="joshi16 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/joshi16" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Rishabh Joshi
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Rishabh Joshi
                
              
              &lt;div id="story-author-preview-content-2402206" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/joshi16" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Rishabh Joshi&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/joshi16/understanding-usereducer-in-react-3534" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Apr 12 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/joshi16/understanding-usereducer-in-react-3534" id="article-link-2402206"&gt;
          🧠Understanding useReducer in React
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/joshi16/understanding-usereducer-in-react-3534" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;2&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/joshi16/understanding-usereducer-in-react-3534#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            4 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>react</category>
      <category>learning</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🧠Understanding useReducer in React</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sat, 12 Apr 2025 14:11:22 +0000</pubDate>
      <link>https://forem.com/joshi16/understanding-usereducer-in-react-3534</link>
      <guid>https://forem.com/joshi16/understanding-usereducer-in-react-3534</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;code&gt;useReducer&lt;/code&gt;&lt;/strong&gt; is a React Hook that provides an alternative to &lt;code&gt;useState&lt;/code&gt; for managing complex state logic. It's particularly useful when state transitions depend on the previous state or involve multiple sub-states.&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;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&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;strong&gt;reducer:&lt;/strong&gt; A function that takes the current state and an action, then returns the new state. Based on the action.type, it returns the new state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;initialState:&lt;/strong&gt; The initial value of the state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;state:&lt;/strong&gt; holds the current state value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dispatch:&lt;/strong&gt; A function used to send actions to the reducer.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&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;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
         &lt;span class="c1"&gt;// Return a NEW object with the updated property&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;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;decrement&lt;/span&gt;&lt;span class="dl"&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;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="c1"&gt;// ❌ ❌ BAD: Returns a number, not the state object shape! AVOID&lt;/span&gt;
      &lt;span class="c1"&gt;// case "increment":&lt;/span&gt;
      &lt;span class="c1"&gt;//   return state.count + action.payload;&lt;/span&gt;

      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
         &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unknown action type&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;/code&gt;&lt;/pre&gt;

&lt;/div&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;Component&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&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;incrementCount&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="c1"&gt;//sends an action to the reducer, updating the state accordingly.&lt;/span&gt;
      &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&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;decrementCount&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="c1"&gt;//sends an action to the reducer, updating the state accordingly&lt;/span&gt;
      &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;decrement&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&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;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;incrementCount&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="nt"&gt;button&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;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&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;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&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;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;decrementCount&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="nt"&gt;button&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;📌&lt;strong&gt;The &lt;code&gt;reducer&lt;/code&gt; must be pure function and return a completely new state object.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How useReducer Works: The Flow
&lt;/h2&gt;

&lt;p&gt;Here’s the step-by-step process detailing how the components of &lt;code&gt;useReducer&lt;/code&gt; interact:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initial Setup:&lt;/strong&gt; When your component first renders, &lt;code&gt;useReducer&lt;/code&gt; is called with your &lt;strong&gt;&lt;code&gt;Reducer Function&lt;/code&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;code&gt;initialState&lt;/code&gt;&lt;/strong&gt;. It returns the current &lt;strong&gt;&lt;code&gt;State Object&lt;/code&gt;&lt;/strong&gt; (which is the &lt;code&gt;initialState&lt;/code&gt; at first) and the &lt;strong&gt;&lt;code&gt;Dispatch Function&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Trigger:&lt;/strong&gt; Something happens in your application that requires a state change (e.g., a user clicks a button, types in a field, data arrives).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dispatching an Action:&lt;/strong&gt; The event handler for that event calls the &lt;strong&gt;&lt;code&gt;Dispatch Function&lt;/code&gt;&lt;/strong&gt;. You pass an &lt;strong&gt;&lt;code&gt;Action Object&lt;/code&gt;&lt;/strong&gt; to &lt;code&gt;dispatch&lt;/code&gt;. This &lt;strong&gt;&lt;code&gt;Action Object&lt;/code&gt;&lt;/strong&gt; contains information about &lt;em&gt;what&lt;/em&gt; change needs to happen (the &lt;strong&gt;&lt;code&gt;type&lt;/code&gt;&lt;/strong&gt;) and any data needed for that change (the &lt;strong&gt;&lt;code&gt;payload&lt;/code&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Example:&lt;/em&gt; &lt;code&gt;dispatch({ type: 'INCREMENT', payload: 1 })&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Reducer Called:&lt;/strong&gt; React takes the &lt;strong&gt;&lt;code&gt;Action Object&lt;/code&gt;&lt;/strong&gt; you dispatched and calls your &lt;strong&gt;&lt;code&gt;Reducer Function&lt;/code&gt;&lt;/strong&gt;. It provides two arguments to your reducer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;current&lt;/em&gt; &lt;strong&gt;&lt;code&gt;State Object&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;Action Object&lt;/code&gt;&lt;/strong&gt; you just dispatched.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Example:&lt;/em&gt; &lt;code&gt;reducer(currentState, { type: 'INCREMENT', payload: 1 })&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Calculating the New State:&lt;/strong&gt; Inside your &lt;strong&gt;&lt;code&gt;Reducer Function&lt;/code&gt;&lt;/strong&gt;, you use the &lt;code&gt;action.type&lt;/code&gt; (and &lt;code&gt;action.payload&lt;/code&gt; if necessary) to determine how to compute the &lt;em&gt;next&lt;/em&gt; state based on the &lt;em&gt;current&lt;/em&gt; state. This is typically done using a &lt;code&gt;switch&lt;/code&gt; statement or &lt;code&gt;if/else&lt;/code&gt; conditions based on &lt;code&gt;action.type&lt;/code&gt;. &lt;strong&gt;Crucially, the reducer must be pure and return a completely &lt;em&gt;new&lt;/em&gt; state object (&lt;/strong&gt;&lt;code&gt;immutability&lt;/code&gt;&lt;strong&gt;), not modify the existing one.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Example:&lt;/em&gt; Based on &lt;code&gt;{ type: 'INCREMENT', payload: 1 }&lt;/code&gt;, the reducer might return &lt;code&gt;{ ...currentState, count: currentState.count + 1 }&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;State Update:&lt;/strong&gt; &lt;code&gt;useReducer&lt;/code&gt; receives the new state object returned by your &lt;strong&gt;&lt;code&gt;Reducer Function&lt;/code&gt;&lt;/strong&gt;. It then updates the internal state associated with your component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Re-render:&lt;/strong&gt; React detects that the state managed by &lt;code&gt;useReducer&lt;/code&gt; has changed. It schedules your component (and potentially its children) to &lt;strong&gt;re-render&lt;/strong&gt;. When the component re-renders, the &lt;code&gt;useReducer&lt;/code&gt; hook will now return the &lt;em&gt;updated&lt;/em&gt; &lt;strong&gt;&lt;code&gt;State Object&lt;/code&gt;&lt;/strong&gt; from step 6. Your UI then displays the information based on this new state.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, &lt;code&gt;useReducer&lt;/code&gt; provides a structured cycle: an &lt;strong&gt;&lt;code&gt;action&lt;/code&gt;&lt;/strong&gt; is dispatched describing an intended change -&amp;gt; the &lt;strong&gt;&lt;code&gt;reducer function&lt;/code&gt;&lt;/strong&gt; calculates the new state based on the current state and the action -&amp;gt; React updates the &lt;strong&gt;&lt;code&gt;state&lt;/code&gt;&lt;/strong&gt; -&amp;gt; the component &lt;strong&gt;re-renders&lt;/strong&gt; with the new state. The &lt;strong&gt;&lt;code&gt;dispatch function&lt;/code&gt;&lt;/strong&gt; is the trigger that kicks off this cycle.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to use &lt;code&gt;useReducer&lt;/code&gt;❓
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When state transitions are more complex (if-else chains or multiple related state updates).&lt;/li&gt;
&lt;li&gt;When you need centralized state management.&lt;/li&gt;
&lt;li&gt;You have multiple related state variables.&lt;/li&gt;
&lt;li&gt;State updates are interdependent.&lt;/li&gt;
&lt;li&gt;When the next state depends on the previous state.&lt;/li&gt;
&lt;li&gt;You want to keep your components cleaner and more readable.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why use &lt;code&gt;useReducer&lt;/code&gt;❓
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;useState&lt;/code&gt; is sufficient for managing simple state, it can become cumbersome when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The component has multiple state variables.&lt;/li&gt;
&lt;li&gt;State updates are scattered across multiple event handlers.&lt;/li&gt;
&lt;li&gt;Multiple state updates need to occur simultaneously in response to an event.&lt;/li&gt;
&lt;li&gt;State updates depend on other state variables. In these cases, &lt;code&gt;useReducer&lt;/code&gt; helps organize state updates in a more structured manner.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧭 When to Use What?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;Recommended Approach&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Simple local state (e.g. form inputs, toggles)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complex local state (e.g. nested updates, dependencies)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;useReducer&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared global state in a small to medium app&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;useReducer&lt;/code&gt; + Context API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Global state in a large, complex application&lt;/td&gt;
&lt;td&gt;Redux&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ✅ Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useReducer&lt;/code&gt; hook is a robust alternative to &lt;code&gt;useState&lt;/code&gt;, particularly well-suited for managing complex state logic and scenarios where the next state depends on the previous one. By centralizing state transitions within a pure reducer function, &lt;code&gt;useReducer&lt;/code&gt; promotes cleaner, more maintainable code and improves the predictability of state changes.&lt;/p&gt;

&lt;p&gt;It's an excellent choice when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have multiple related pieces of state.&lt;/li&gt;
&lt;li&gt;State updates are interdependent or complex.&lt;/li&gt;
&lt;li&gt;You want to separate state logic from UI logic for better readability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding &lt;code&gt;useReducer&lt;/code&gt; not only helps structure state management more effectively within components, but also lays a strong foundation for working with more advanced tools like &lt;strong&gt;&lt;code&gt;Redux&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;Likes, Comments and feedback are welcome!😅&lt;/p&gt;

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

</description>
    </item>
    <item>
      <title>🚀 Just published a deep dive on useRef in React! Understand how it works, when to use it, and common use cases — with analogies &amp; clean examples.</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sun, 06 Apr 2025 07:18:27 +0000</pubDate>
      <link>https://forem.com/joshi16/just-published-a-deep-dive-on-useref-in-react-understand-how-it-works-when-to-use-it-and-3cj1</link>
      <guid>https://forem.com/joshi16/just-published-a-deep-dive-on-useref-in-react-understand-how-it-works-when-to-use-it-and-3cj1</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/joshi16/understanding-useref-in-react-393c" class="crayons-story__hidden-navigation-link"&gt;🧠Understanding useRef in React&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/joshi16" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" alt="joshi16 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/joshi16" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Rishabh Joshi
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Rishabh Joshi
                
              
              &lt;div id="story-author-preview-content-2385455" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/joshi16" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Rishabh Joshi&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/joshi16/understanding-useref-in-react-393c" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Apr 6 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/joshi16/understanding-useref-in-react-393c" id="article-link-2385455"&gt;
          🧠Understanding useRef in React
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/react"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;react&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/frontend"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;frontend&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/joshi16/understanding-useref-in-react-393c" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;4&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/joshi16/understanding-useref-in-react-393c#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              1&lt;span class="hidden s:inline"&gt; comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            6 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>🧠Understanding useRef in React</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sun, 06 Apr 2025 07:16:23 +0000</pubDate>
      <link>https://forem.com/joshi16/understanding-useref-in-react-393c</link>
      <guid>https://forem.com/joshi16/understanding-useref-in-react-393c</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;useRef&lt;/code&gt; is a React Hook that allows you to create a mutable reference object. This reference object can hold a value that persists across renders and changing its &lt;code&gt;.current&lt;/code&gt; value does NOT trigger a re-render. It's also commonly used to get direct access to DOM elements.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is useRef?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; is a React hook returning a mutable &lt;code&gt;ref&lt;/code&gt; object with a single &lt;code&gt;.current&lt;/code&gt; property.&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;const&lt;/span&gt; &lt;span class="nx"&gt;myRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// myRef looks like: { current: initialValue }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Initialization:&lt;/em&gt;&lt;/strong&gt; The &lt;code&gt;initialValue&lt;/code&gt; you provide is used to set &lt;code&gt;myRef.current&lt;/code&gt; only during the component's initial render. It's ignored on subsequent renders. If you omit the argument, &lt;code&gt;initialValue&lt;/code&gt; defaults to &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Return Value:&lt;/em&gt;&lt;/strong&gt; &lt;code&gt;useRef&lt;/code&gt; returns the same ref object on every render of a given component instance. This is how it persists across renders.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🗝️Key Characteristics:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Mutable &lt;code&gt;.current&lt;/code&gt; Property:&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
You directly modify the &lt;code&gt;ref.current&lt;/code&gt; property (e.g., &lt;code&gt;myRef.current = newValue&lt;/code&gt;), unlike state variables from &lt;code&gt;useState&lt;/code&gt; (which you update via a setter function), &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;No Re-render on Change:&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
Assigning a new value to &lt;code&gt;ref.current&lt;/code&gt; does not trigger a component re-render. This is the most significant difference from &lt;code&gt;useState&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Persistence Across Renders:&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
The ref object itself (and the value stored in ref.current) persists for the full lifetime of the component. React ensures you get the same ref object back on every render.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚠️When Should You Mutate &lt;code&gt;ref.current&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Since mutating &lt;code&gt;ref.current&lt;/code&gt; doesn't cause a re-render, it's important when you perform the mutation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;✅Safe:&lt;/strong&gt; Inside event handlers (like &lt;code&gt;onClick&lt;/code&gt;, &lt;code&gt;onChange&lt;/code&gt;) or inside &lt;code&gt;useEffect&lt;/code&gt; hooks. These run after the render cycle is complete or as a result of user interaction, making mutations predictable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;❌Avoid&lt;/strong&gt; (&lt;em&gt;Generally&lt;/em&gt;): Mutating &lt;code&gt;ref.current&lt;/code&gt; directly during the rendering phase (i.e., right inside the main body of your functional component). While React won't stop you, reading a ref during render is fine, but writing to it can make component behavior less predictable. This is because the component body should ideally be free of side effects, and mutations might not behave consistently depending on React's rendering optimizations (like Concurrent Mode). If you need to calculate something based on render and store it mutably, &lt;code&gt;useEffect&lt;/code&gt; is usually the better place.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔧Common Use Cases
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; serves two primary purposes in React components:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Accessing DOM Nodes
&lt;/h3&gt;

&lt;p&gt;This is a very common use case. &lt;code&gt;useRef&lt;/code&gt; allows you to get a direct reference to a specific DOM element rendered by your component.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a ref, usually initialized to null: &lt;code&gt;const myInputRef = useRef(null);&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Attach this ref to a JSX element using the special ref prop: &lt;code&gt;&amp;lt;input ref={myInputRef} /&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After the component mounts and React creates the DOM node for the &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;, React will set &lt;code&gt;myInputRef.current&lt;/code&gt; to that DOM node instance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Why it's needed:&lt;/strong&gt; &lt;br&gt;
For interacting directly with the DOM, which is sometimes necessary for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Managing focus (e.g.- &lt;code&gt;myInputRef.current.focus()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Measuring element dimensions or position.&lt;/li&gt;
&lt;li&gt;Triggering imperative animations.&lt;/li&gt;
&lt;li&gt;Integrating with third-party libraries that need direct DOM access.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&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="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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="s1"&gt;react&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;TextInputWithFocusButton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. Create a ref to hold the input DOM element&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputEl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Example: Focus on initial mount &lt;/span&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="nx"&gt;inputEl&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="nf"&gt;focus&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="c1"&gt;// Empty dependency array means run once after initial mount&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;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* 2. Attach the ref to the input element */&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;input&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputEl&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&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;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Storing Mutable Values (Like Instance Variables)
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;useRef&lt;/code&gt; to keep track of any mutable value that needs to persist across renders but should not cause a re-render when it changes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing previous state or prop values.&lt;/li&gt;
&lt;li&gt;Managing timer IDs (e.g., from &lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;setInterval&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keeping track of internal component state that doesn't directly affect the rendered output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;How it works&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initialization:&lt;/strong&gt; 
You call &lt;code&gt;useRef(initialValue)&lt;/code&gt; during your component's setup. React creates a plain JavaScript object like &lt;code&gt;{ current: initialValue }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Persistence:&lt;/strong&gt; 
React internally holds onto this specific object for the entire lifetime of your component instance. On every subsequent render, the exact same object is returned by the &lt;code&gt;useRef&lt;/code&gt; hook.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessing:&lt;/strong&gt; 
You can read the stored value anytime using &lt;code&gt;yourRef.current&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updating:&lt;/strong&gt; 
You can change the stored value by directly assigning a new value to the &lt;code&gt;.current property&lt;/code&gt; (e.g., &lt;code&gt;yourRef.current = newValue&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️&lt;strong&gt;No Re-render:&lt;/strong&gt;  this assignment to &lt;code&gt;.current&lt;/code&gt; is just a standard JavaScript object property mutation. React does not monitor this change, and therefore, it does not trigger a component re-render. The value is updated silently within the persistent ref object.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Analogy: The Room, The Closet, and The Label Maker
&lt;/h2&gt;

&lt;p&gt;Imagine your React component is like a room. Every time the component &lt;em&gt;"re-renders"&lt;/em&gt; (because its state or props change), it's like the room gets quickly dismantled and rebuilt with the updated information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt;: Think of &lt;code&gt;useState&lt;/code&gt; as the &lt;strong&gt;visible furniture or decorations&lt;/strong&gt; in the room (like a couch or a painting). When you change something using the &lt;code&gt;set&lt;/code&gt; function (e.g., changing the couch color), React notices this visible change and rebuilds the room (re-renders the component) to show the update. Everyone sees the new couch color.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;useRef&lt;/code&gt;&lt;/strong&gt;: Now, &lt;code&gt;useRef&lt;/code&gt; gives you access to persistent tools that &lt;em&gt;don't&lt;/em&gt; cause a room rebuild when used or changed. It primarily provides two kinds of tools:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Hidden Storage Closet (for Mutable Values):&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Calling &lt;code&gt;useRef(initialValue)&lt;/code&gt; can create a small, &lt;strong&gt;hidden storage closet&lt;/strong&gt; inside the room, with an initial item placed on its single shelf, labelled &lt;code&gt;.current&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This closet (&lt;code&gt;ref&lt;/code&gt; object) and its contents persist across room rebuilds (re-renders). Even when the room is dismantled and rebuilt, the closet remains exactly where it is, untouched.&lt;/li&gt;
&lt;li&gt;You can open the closet anytime (&lt;strong&gt;&lt;em&gt;preferably in effects or handlers&lt;/em&gt;&lt;/strong&gt;) and change the item on the &lt;code&gt;.current&lt;/code&gt; shelf (&lt;code&gt;myRef.current = newValue&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Crucially: Changing the item &lt;em&gt;inside&lt;/em&gt; the hidden closet does &lt;strong&gt;not&lt;/strong&gt; cause the room to be rebuilt (it does not trigger a re-render). It's a silent change, hidden from the main rendering process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Persistent Label Maker (for DOM Access):&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Calling &lt;code&gt;useRef(null)&lt;/code&gt; can also give you a &lt;strong&gt;persistent label maker&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;When you build the room, you can use the special &lt;code&gt;ref&lt;/code&gt; prop on a specific part of the room's structure (like a window, a door, or a light switch – representing a DOM element like &lt;code&gt;&amp;lt;input ref={myRef} /&amp;gt;&lt;/code&gt;). This tells React: "Use the &lt;code&gt;myRef&lt;/code&gt; label maker to put a unique, persistent label on this specific window when you build it."&lt;/li&gt;
&lt;li&gt;After the room is built (the component mounts and renders the element), the label maker's display (&lt;code&gt;myRef.current&lt;/code&gt;) now points directly to that specific, labeled window (the actual DOM node).&lt;/li&gt;
&lt;li&gt;You can then use this reference (&lt;code&gt;myRef.current&lt;/code&gt;) to interact &lt;em&gt;directly&lt;/em&gt; with that specific part of the room (e.g., &lt;code&gt;myRef.current.focus()&lt;/code&gt; to look through the window, or &lt;code&gt;myRef.current.style.display = 'none'&lt;/code&gt; to cover it) without needing to rebuild the entire room (no re-render is triggered by accessing or manipulating the element via the ref).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In essence, &lt;code&gt;useRef&lt;/code&gt; provides ways to either hold onto information silently (&lt;code&gt;closet&lt;/code&gt;) or get a direct, persistent handle (&lt;code&gt;label maker&lt;/code&gt;) to parts of the rendered output, all without interfering with React's normal state-driven rendering cycle.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 &lt;code&gt;useState&lt;/code&gt; vs &lt;code&gt;useRef&lt;/code&gt; – Quick Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;useRef&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Triggers re-render on change?&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Persists across renders?&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used for DOM access?&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ideal for&lt;/td&gt;
&lt;td&gt;UI-related state that affects rendering&lt;/td&gt;
&lt;td&gt;Mutable values or DOM refs that don’t affect UI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How to update value&lt;/td&gt;
&lt;td&gt;Using setter function (e.g., &lt;code&gt;setValue()&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Direct mutation (&lt;code&gt;myRef.current = newValue&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be used in render phase?&lt;/td&gt;
&lt;td&gt;✅ Yes (safe)&lt;/td&gt;
&lt;td&gt;⚠️ Reading: Yes / Writing: Avoid in render phase&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;useRef&lt;/code&gt;&lt;/strong&gt; is a powerful tool in your React toolkit, essential for interacting with the DOM and managing persistent mutable values that shouldn't trigger re-renders. Understanding when and why to use it over &lt;code&gt;useState&lt;/code&gt; is key to writing efficient and effective React components.&lt;/p&gt;

&lt;h3&gt;
  
  
  when and why to use &lt;code&gt;useRef&lt;/code&gt;:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need direct access to a DOM element for imperative actions like focusing, measuring, or integrating with external libraries. &lt;code&gt;useState&lt;/code&gt; cannot do this.&lt;/li&gt;
&lt;li&gt;You need to store mutable data that persists across renders, but changing this data should NOT cause a re-render. This is useful for performance or specific logic needs.&lt;/li&gt;
&lt;li&gt;Think: Timer IDs (setTimeout/setInterval), tracking previous state/props, flags not directly rendered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; &lt;code&gt;useState&lt;/code&gt; is for state that drives your UI updates. &lt;code&gt;useRef&lt;/code&gt; is for references to DOM nodes or for holding mutable data silently behind the scenes.&lt;/p&gt;




&lt;p&gt;Likes, Comments and feedback are welcome!😅&lt;/p&gt;

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

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Master side effects, dependencies, and cleanup like a pro! 🚀 Learn how to fetch data, manage event listeners, and avoid common mistakes. #React #WebDev #JavaScript #reacthooks</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sun, 30 Mar 2025 03:18:43 +0000</pubDate>
      <link>https://forem.com/joshi16/master-side-effects-dependencies-and-cleanup-like-a-pro-learn-how-to-fetch-data-manage-event-1e6m</link>
      <guid>https://forem.com/joshi16/master-side-effects-dependencies-and-cleanup-like-a-pro-learn-how-to-fetch-data-manage-event-1e6m</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/joshi16/understanding-useeffect-in-react-1geb" class="crayons-story__hidden-navigation-link"&gt;🧠 Understanding useEffect in React&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/joshi16" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" alt="joshi16 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/joshi16" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Rishabh Joshi
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Rishabh Joshi
                
              
              &lt;div id="story-author-preview-content-2365220" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/joshi16" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Rishabh Joshi&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/joshi16/understanding-useeffect-in-react-1geb" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Mar 30 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/joshi16/understanding-useeffect-in-react-1geb" id="article-link-2365220"&gt;
          🧠 Understanding useEffect in React
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/react"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;react&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/joshi16/understanding-useeffect-in-react-1geb" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;3&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/joshi16/understanding-useeffect-in-react-1geb#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              5&lt;span class="hidden s:inline"&gt; comments&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>🧠 Understanding useEffect in React</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Sun, 30 Mar 2025 03:14:01 +0000</pubDate>
      <link>https://forem.com/joshi16/understanding-useeffect-in-react-1geb</link>
      <guid>https://forem.com/joshi16/understanding-useeffect-in-react-1geb</guid>
      <description>&lt;h2&gt;
  
  
  Why useEffect Exists
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;React's rendering must be pure—meaning it shouldn't modify the DOM, fetch data, or perform side effects during render. Instead, useEffect lets us handle side effects after the component has been painted.&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  What are Side Effects?
&lt;/h3&gt;

&lt;p&gt;Side effects are operations that interact with outside elements (API calls, event listeners, timers, etc.). Without useEffect, we'd struggle to perform these tasks in function components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Idea
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;useEffect runs after the render is committed to the DOM.&lt;/li&gt;
&lt;li&gt;It helps manage side effects in React.&lt;/li&gt;
&lt;li&gt;We can control when the effect runs using dependencies.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1️⃣ Basic Syntax of useEffect
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;// Side effect logic (e.g., API call, event listener)&lt;/span&gt;

  &lt;span class="k"&gt;return &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;// Cleanup function (optional)&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;dependencies&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Dependency array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Breakdown
&lt;/h3&gt;

&lt;p&gt;✅ First argument → Function containing the effect.&lt;br&gt;
✅ Second argument → Dependency array (controls when it runs).&lt;br&gt;
✅ Return function → Cleanup function (runs before next effect execution or unmounting).&lt;/p&gt;


&lt;h2&gt;
  
  
  2️⃣ Different Ways to Use useEffect
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1️⃣ No Dependency Array (Runs on Every Render)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="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;Runs after every render&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Re-runs after every render (not recommended in most cases).&lt;/li&gt;
&lt;li&gt;Can cause infinite loops if it modifies state inside.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  2️⃣ Empty Dependency Array (Runs Only Once)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="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;Runs only on mount!&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="c1"&gt;// Empty dependency array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Runs only once when the component mounts.&lt;/li&gt;
&lt;li&gt;Similar to componentDidMount in class components.&lt;/li&gt;
&lt;li&gt;Best for fetching data or setting up subscriptions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  3️⃣ Dependency Array (Runs on State or Prop Changes)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&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="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="s2"&gt;`Count changed: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Runs whenever `count` changes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The effect re-runs whenever any dependency in the array changes.&lt;/li&gt;
&lt;li&gt;Essential for reactive behaviors based on state/props.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  4️⃣ Cleanup Function (For Unmounting &amp;amp; Re-running Effects)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&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;AbortController&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://url&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="na"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Cleanup: Abort fetch on unmount&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;h2&gt;
  
  
  3️⃣ Cleanup Matters
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Cleanup function runs:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Before running the effect again (if dependencies change).

&lt;ul&gt;
&lt;li&gt;Whenever the dependencies in the dependency array change, React first cleans up the previous effect before running the new one.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;When the component unmounts (to prevent memory leaks).

&lt;ul&gt;
&lt;li&gt;If an effect sets up an event listener or subscribes to a service, it should clean up when the component unmounts, or it could cause memory leaks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If an effect isn't cleaned up properly, React may:&lt;br&gt;
❌ Leak memory (e.g., event listeners that are never removed).&lt;br&gt;
❌ Cause race conditions (outdated API requests could still resolve, updating unmounted components and causing errors.).&lt;br&gt;
❌ Trigger unexpected behavior when dependencies change.&lt;/p&gt;


&lt;h2&gt;
  
  
  4️⃣ ⚠️ Common Mistakes &amp;amp; Gotchas
&lt;/h2&gt;

&lt;p&gt;❌ Async Functions Directly in useEffect&lt;br&gt;
React expects useEffect to return either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nothing (undefined)&lt;/li&gt;
&lt;li&gt;A cleanup function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But if you use async directly, it always returns a Promise → React doesn’t expect 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="c1"&gt;// ❌ Incorrect way&lt;/span&gt;
&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchData&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🚨 This will cause a warning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Effect callbacks are synchronous to prevent race conditions. Put the async function inside the effect."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ Proper Way&lt;br&gt;
Wrap the async function inside useEffect:&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;
    &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;fetchData&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5️⃣ Rules of useEffect (and all Hooks) 📌
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Always call hooks at the top level of your component.&lt;/li&gt;
&lt;li&gt;Do NOT call hooks inside loops, conditions, or nested functions.&lt;/li&gt;
&lt;li&gt;Every variable used inside useEffect must be in the dependency array (or use useRef to persist values without re-running effects).&lt;/li&gt;
&lt;li&gt;Effects should be as specific as possible (avoid unnecessary re-renders).&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🔥 Final Takeaway
&lt;/h2&gt;

&lt;p&gt;🚀 useEffect is one of the most powerful hooks in React.&lt;br&gt;
📌 It helps manage side effects (API calls, subscriptions, DOM interactions).&lt;br&gt;
⚡ Control when it runs using the dependency array.&lt;br&gt;
🧹 Always clean up effects to avoid memory leaks.&lt;/p&gt;

&lt;p&gt;💬 Have questions or tips on using useEffect? Drop them in the comments below! 👇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>React is all about building dynamic UIs and to do that, we need state. This post is a simplified and structured guide created from my personal notes, designed to help beginners understand how useState works and when to use it.</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Thu, 20 Mar 2025 09:20:22 +0000</pubDate>
      <link>https://forem.com/joshi16/react-is-all-about-building-dynamic-uis-and-to-do-that-we-need-state-this-post-is-a-simplified-3c0k</link>
      <guid>https://forem.com/joshi16/react-is-all-about-building-dynamic-uis-and-to-do-that-we-need-state-this-post-is-a-simplified-3c0k</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/joshi16" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F722573%2F658efe6e-92ed-481a-9d91-c48ec9754890.png" alt="joshi16"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/joshi16/understanding-usestate-in-react-31li" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;🧠 Understanding useState in React&lt;/h2&gt;
      &lt;h3&gt;Rishabh Joshi ・ Mar 20 '25&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#reactjsdevelopment&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>🧠 Understanding useState in React</title>
      <dc:creator>Rishabh Joshi</dc:creator>
      <pubDate>Thu, 20 Mar 2025 09:17:52 +0000</pubDate>
      <link>https://forem.com/joshi16/understanding-usestate-in-react-31li</link>
      <guid>https://forem.com/joshi16/understanding-usestate-in-react-31li</guid>
      <description>&lt;p&gt;React is all about building dynamic UIs — and to do that, we need state. One of the most fundamental hooks in React is &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This post is a simplified and structured guide created from my personal notes, designed to help beginners understand how useState works, when to use it, and follow best practices.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What is &lt;code&gt;useState&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;useState&lt;/code&gt; is a Hook in React that lets you &lt;strong&gt;add state&lt;/strong&gt; to functional components. It allows your component to remember values between renders and update the UI when that value changes.&lt;/em&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&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="nx"&gt;initialValue&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;strong&gt;state&lt;/strong&gt; → The current state value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;setState&lt;/strong&gt; → A function to update the &lt;em&gt;state&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;initialValue&lt;/strong&gt; → The initial value of the state (can be a primitive, object, array, or function)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🛠 Basic Example&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;useState&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&lt;/span&gt;&lt;span class="dl"&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setStep&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/*here step stores the default value i.e. 1 and 2nd parameter
  is the function which will help us updating the value of state.*/&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;handlePrevious&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&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="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* NEVER update state based on current state like this
    (use callback function)*/&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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"buttons"&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;p&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Step &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;step&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;p&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;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handlePrevious&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Previous&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;📝 Note&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;strong&gt;useState is a hook&lt;/strong&gt;&lt;/u&gt; — &lt;em&gt;and hooks must be used at the top level of your component.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Never update state directly — always use the setter function&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;React REACTS to state changes by re-rendering the UI&lt;/code&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🔍 The Mechanics of State
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Whenever the state updates, React re-renders the component.&lt;/li&gt;
&lt;li&gt;State is preserved between re-renders — unless the component unmounts (i.e. disappears from UI).&lt;/li&gt;
&lt;li&gt;React calls the function of your component again during re-render, but the state remains intact.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚡ Tips for Using State Effectively
&lt;/h2&gt;

&lt;p&gt;✅ Make sure to update state only when needed, to avoid performance issues.&lt;br&gt;
✅ Use callback functions when your new state depends on the current one.&lt;br&gt;
❌ Avoid using state to store values that can be calculated from props.&lt;br&gt;
✅ Use state to store data specific to the component.&lt;br&gt;
❌ Never mutate the state directly.&lt;/p&gt;



&lt;p&gt;❌ Don’t update based on current 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="nf"&gt;setStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&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;p&gt;✅ Use callback function when state depends on current value:&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="nf"&gt;setStep&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;curStep&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;curStep&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;blockquote&gt;
&lt;p&gt;If updating state doesn’t depend on the current state, you can use the direct method.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧩 State Management in React
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;State management is about deciding:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What state is needed?&lt;/li&gt;
&lt;li&gt;Where it should live?&lt;/li&gt;
&lt;li&gt;How data flows through the app?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Local State:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Needed in one or a few components.&lt;/li&gt;
&lt;li&gt;Passed via props if needed by child components.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Global State:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared across many components.&lt;/li&gt;
&lt;li&gt;Use tools like Context API or Redux.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;⬆️ Lifting the State Up&lt;/strong&gt;&lt;br&gt;
If sibling components need the same piece of state, lift it to the nearest common parent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📐 Derived State&lt;/strong&gt;&lt;br&gt;
Derived state is calculated based on another piece of state or prop — and recalculated during every render.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎮 Controlled Elements in React
&lt;/h2&gt;

&lt;p&gt;By default, input fields manage their own state in the DOM. In React, we like to centralize state inside our app. This is done using controlled elements.&lt;/p&gt;

&lt;p&gt;Steps to implement:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create state for the input.&lt;/li&gt;
&lt;li&gt;Set input's value to that state.&lt;/li&gt;
&lt;li&gt;Use onChange to update the state.
&lt;/li&gt;
&lt;/ol&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;Form&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDescription&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="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&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;form&lt;/span&gt; &lt;span class="na"&gt;onSubmit&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSubmit&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="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
        &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Item.."&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setDescription&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;
   &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* setting the value of item as state and then 
   updating the value of state by using setter function */&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;form&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🔚 Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;**useState**&lt;/code&gt; is your first and most important hook in React. Understanding how it works, when to use callbacks, and how to structure state effectively will set you up for success in building dynamic, interactive UIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  🙌 Let’s Connect
&lt;/h4&gt;

&lt;p&gt;I’d love to hear your thoughts, questions, or feedback.&lt;br&gt;
You can reach me on &lt;a href="https://github.com/joshir16" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; or leave a comment below.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>reactjsdevelopment</category>
    </item>
  </channel>
</rss>
