<?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>DEV Community: Siddhartha Mishra</title>
    <description>The latest articles on DEV Community by Siddhartha Mishra (@sidddharthamishra).</description>
    <link>https://dev.to/sidddharthamishra</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%2F857057%2F890ccffe-ba22-47c5-b0c6-f48f04f7cfef.jpeg</url>
      <title>DEV Community: Siddhartha Mishra</title>
      <link>https://dev.to/sidddharthamishra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sidddharthamishra"/>
    <language>en</language>
    <item>
      <title>Check this out</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Thu, 05 Mar 2026 18:50:53 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/check-this-out-871</link>
      <guid>https://dev.to/sidddharthamishra/check-this-out-871</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/sidddharthamishra" 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%2F857057%2F890ccffe-ba22-47c5-b0c6-f48f04f7cfef.jpeg" alt="sidddharthamishra"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/sidddharthamishra/introducing-snapbittools-3nho" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Introducing SnapbitTools&lt;/h2&gt;
      &lt;h3&gt;Siddhartha Mishra ・ Mar 5&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;#devtools&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#opensource&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#privacy&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>devtools</category>
      <category>opensource</category>
      <category>privacy</category>
    </item>
    <item>
      <title>Introducing SnapbitTools</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Thu, 05 Mar 2026 18:50:12 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/introducing-snapbittools-3nho</link>
      <guid>https://dev.to/sidddharthamishra/introducing-snapbittools-3nho</guid>
      <description>&lt;p&gt;In the modern digital workflow, we are constantly moving between tasks that require small but essential data transformations. Whether it’s a developer needing to prettify a JSON object, a social media manager compressing a batch of images for a quick upload, or an office administrator converting a CSV report into an Excel sheet, these "micro-tasks" can often become major bottlenecks.&lt;/p&gt;

&lt;p&gt;Enter &lt;a href="https://snapbittools.com" rel="noopener noreferrer"&gt;Snapbittools&lt;/a&gt;, a comprehensive collection of browser-based utilities designed to handle these exact scenarios. Built with a focus on speed, simplicity, and uncompromising privacy, SnapbitTools provides a robust toolkit for developers, creators, and everyday users who need to process files and data without the friction of heavy software or risky uploads.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem With Traditional Online Tools
&lt;/h2&gt;

&lt;p&gt;For years, the go-to solution for quick file conversions or data formatting has been a quick Google search followed by a visit to a random "free online converter." While these sites serve a purpose, they often come with significant drawbacks that can compromise both your productivity and your security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Privacy and Security Concerns&lt;/strong&gt;&lt;br&gt;
The biggest red flag with traditional online utilities is the requirement to upload your files to a third-party server. When you upload a document, an image, or a dataset to "Convert-My-File.com," you are essentially handing over your data to an unknown entity. For professionals handling sensitive client information or proprietary code, this is often a non-starter and a major compliance risk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Latency and Slow Processing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Traditional tools rely on a server-side round trip. You upload the file, the server processes it, and then you download the result. If your internet connection is slow or the file is large, this process can take minutes. Furthermore, if the site’s server is under heavy load, your "quick" task can grind to a halt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Cluttered, Ad-Heavy Interfaces&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many free tool sites are subsidized by aggressive advertising. This results in interfaces cluttered with "Download" buttons that are actually ads, pop-ups that distract from the task at hand, and a generally poor user experience that slows down your workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Makes SnapbitTools Different?
&lt;/h2&gt;

&lt;p&gt;SnapbitTools was built from the ground up to solve these specific frustrations. By leveraging modern web technologies and the power of the React ecosystem, we have created a platform that prioritizes the user over the infrastructure.&lt;/p&gt;

&lt;p&gt;The core philosophy of SnapbitTools is that your data belongs to you. Unlike other platforms, SnapbitTools performs all computations directly within your browser. When you "upload" a file to our Image Compressor or CSV Converter, it never leaves your computer. The browser handles the logic, and the output is generated locally.&lt;/p&gt;

&lt;p&gt;Because no data is ever sent to a server, SnapbitTools is inherently secure. You can process sensitive JSON files, private photos, or internal company spreadsheets with the peace of mind that no logs are being kept and no third party is seeing your content.&lt;/p&gt;

&lt;p&gt;By eliminating the need for uploads and downloads, processing becomes near-instantaneous. The speed is limited only by your computer’s hardware, not your internet bandwidth. This makes SnapbitTools feel like a native desktop application rather than a slow web page.&lt;/p&gt;

&lt;p&gt;We believe that tools should be functional, not flashy. SnapbitTools features a minimalist, professional interface designed for productivity. No misleading "Download" ads, no intrusive pop-ups—just the tools you need, exactly when you need them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;p&gt;SnapbitTools offers a diverse array of utilities categorized to help you find the right solution for your specific problem.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/image-to-base64" rel="noopener noreferrer"&gt;Format Converter &amp;amp; Base64 Converter&lt;/a&gt;: Quickly switch between formats like PNG, JPG, and WebP, or encode images into Base64 strings for direct embedding in CSS or HTML.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/image-compressor" rel="noopener noreferrer"&gt;Image Compressor &amp;amp; Cropper&lt;/a&gt;: Optimize images for web performance without losing quality, or use the intuitive cropper to get the perfect framing for your social media posts.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/image-to-pdf" rel="noopener noreferrer"&gt;Image to PDF&lt;/a&gt;: Effortlessly bundle multiple images into a single, professional PDF document.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/json-formatter" rel="noopener noreferrer"&gt;JSON Formatter&lt;/a&gt;: Instantly turn "minified" or messy JSON code into a readable, properly indented structure.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/diff-checker" rel="noopener noreferrer"&gt;Diff Checker&lt;/a&gt;: Compare two snippets of code or text to identify changes, additions, or deletions—essential for debugging or reviewing document revisions.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/csv-xlsx-converter" rel="noopener noreferrer"&gt;CSV ↔ XLSX Converter&lt;/a&gt;: Seamlessly move data between spreadsheet formats.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/json-to-csv" rel="noopener noreferrer"&gt;JSON to CSV&lt;/a&gt;: Transform complex data structures into a flat format ready for analysis in Excel or Google Sheets.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/word-counter" rel="noopener noreferrer"&gt;Word Counter&lt;/a&gt;: Get instant statistics on your writing, including character counts and reading time.&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://snapbittools.com/lorem-ipsum-generator" rel="noopener noreferrer"&gt;Lorem Ipsum Generator&lt;/a&gt;: Generate placeholder text for design mockups in seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Bulk File Renamer(New)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most powerful features in the SnapbitTools arsenal is the Bulk File Renamer. Anyone who has ever tried to manually rename 50 photos from a digital camera or 20 disorganized PDF receipts knows how tedious the task can be.&lt;br&gt;
SnapbitTools allows you to drop a massive list of files into the browser and apply logic-based renaming rules instantly&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefix and Suffix:&lt;/strong&gt; Quickly add dates, project names, or categories to the beginning or end of your filenames.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pattern-Based Renaming:&lt;/strong&gt; Create sequential numbering or standardized naming conventions across hundreds of files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Find and Replace:&lt;/strong&gt; Strip out unwanted characters, replace underscores with spaces, or update version numbers globally across your selection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check it out - &lt;a href="https://snapbittools.com/bulk-file-renamer" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine a photographer who has just finished a shoot. Instead of having files named &lt;code&gt;IMG_4821.jpg&lt;/code&gt;, &lt;code&gt;IMG_4822.jpg&lt;/code&gt;, etc., they can use SnapbitTools to instantly rename them to &lt;code&gt;Wedding_Smith_Oct2023_001.jpg&lt;/code&gt; in a single click. It’s local, it’s fast, and it’s free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why You Should Bookmark SnapbitTools
&lt;/h2&gt;

&lt;p&gt;Whether you are a professional or a student, SnapbitTools is designed to be your digital Swiss Army knife.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For Developers:&lt;/strong&gt; Format data and compare code snippets without leaving your browser or installing heavy IDE extensions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For Students:&lt;/strong&gt; Quickly convert document formats for assignments and use the word counter for essays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For Content Creators:&lt;/strong&gt; Optimize images for blogs and rename massive sets of creative assets in seconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For Office Workers:&lt;/strong&gt; Convert reports between CSV and Excel formats without worrying about the privacy of sensitive company data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By operating entirely within the browser, SnapbitTools removes the barrier of software installation. There are no accounts to create and no subscriptions to manage. It is simply a reliable, private, and incredibly fast way to get things done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://snapbittools.com" rel="noopener noreferrer"&gt;Ready to streamline your workflow?&lt;/a&gt; Explore the full collection at SnapbitTools.&lt;/p&gt;

&lt;p&gt;Also Comment which new tool you want to see in Snapbit.&lt;br&gt;
Stay Tuned for new features.&lt;br&gt;
Thank you&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>devtools</category>
      <category>opensource</category>
      <category>privacy</category>
    </item>
    <item>
      <title>Mutating State in React</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Tue, 01 Jul 2025 15:36:22 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/mutating-state-in-react-2ij8</link>
      <guid>https://dev.to/sidddharthamishra/mutating-state-in-react-2ij8</guid>
      <description>&lt;p&gt;Recently in my job, I was working on a project. I have to build a component that searches particular product from the json based on SKU code and fill all the details.&lt;/p&gt;

&lt;p&gt;Here is the function.&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="nx"&gt;searchProduct&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;index&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;matchedProduct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matchedProduct&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;billProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;desc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;sku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;hsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hsn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;gst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gst&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;billProducts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;cess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;matchedProduct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cess&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;qtyInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`qty-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/2`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;qtyInput&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now at first glance, It was looking perfect and also working as expected. But when I was reviewing my code, I find that I am not setting the state after updating it. I was shocked, How the hell all functionality is working. &lt;/p&gt;

&lt;p&gt;I dig deeper in react and Js theory. After researching a bit I found some amazing facts. &lt;/p&gt;

&lt;p&gt;Lets look at them one by one&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Arrays and Objects are passed by reference in JS
&lt;/h2&gt;

&lt;p&gt;This is the root cause of this unexpected behaviour.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const billProducts = this.state.billProducts;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Although in this line it looks like I am creating another Array billProducts, but since &lt;code&gt;this.state.billProducts&lt;/code&gt; is array, it is just a reference. I am directly mutating the state. So even if I am not setting the state, all changes are visible in component.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. How does React work with this?
&lt;/h2&gt;

&lt;p&gt;React works with virtual DOM, in background it checks if the previous state and current state is same or not. If it differs then, It re renders the component. But in this case it is not re rendering, because I am directly mutating the original Array, reference is same and I am not calling &lt;code&gt;this.setState&lt;/code&gt;, hence React does not re render. &lt;/p&gt;

&lt;p&gt;Although It seems good, because we are saving the renders. But it might result in unexpected behavior. Since React does not re renders, other state might not change which might affect the UI. &lt;/p&gt;

&lt;p&gt;React is build on the concept of Immutability. You should not mutate the state directly. Use setter function to always change the state. &lt;/p&gt;

&lt;p&gt;In my case, It doesn't cause any issue, but still I used &lt;code&gt;this.setState()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I never knew about this concept in JS and React. So thought of worth sharing with my network. &lt;/p&gt;

&lt;h3&gt;
  
  
  Thank you
&lt;/h3&gt;

&lt;p&gt;Follow me &lt;br&gt;
&lt;a href="https://twitter.com/raiderfreed" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/siddharthamishra-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>learning</category>
    </item>
    <item>
      <title>Parcel with React</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Sun, 18 Feb 2024 08:00:00 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/parcel-with-react-3c84</link>
      <guid>https://dev.to/sidddharthamishra/parcel-with-react-3c84</guid>
      <description>&lt;h2&gt;
  
  
  Basics
&lt;/h2&gt;

&lt;p&gt;If you have worked with Javascript, you know about Module bundlers, right? &lt;br&gt;
In simple term, Module bundler , bundles your code, spread across multiple files (HTML,CSS,JS...) into smaller chunks. Now you might be wondering , why to build bundler if you can send js, html and css files directly from the server. You are right. But remember we are not living in early 2000's, where websites are crap,slow and not to mention their UI. &lt;/p&gt;

&lt;p&gt;As different technologies are evolving, for a simple website build using any framework it is common to have files in megabytes.This might seem small, but server and internet is dealing with millions if not billions of such files. If server send files in their raw format just think about internet usage and bandwidth. And we are not even talking about client side. If for every page mb's of files are downloaded and cached in browser. Then you will not be able to open more than 5 pages, before your browser memory ran out.&lt;/p&gt;

&lt;p&gt;To tackle this software engineers, build bundlers, they combine your files, minify them and divide them into chunks, to have smaller files to manage for the browser.&lt;/p&gt;

&lt;p&gt;I am a front end developer especially work with react.I started with create-react-app and now I move on to Vite. If you don't know these are build tools to bootstrap your react project. Now these tools come with their own module bundlers integrated. CRA uses webpack and Vite uses esBuild. Now these have their own pros and cons.&lt;/p&gt;

&lt;p&gt;Now apart from these two, there is third option "Parcel". But sadly there is no build tool with Parcel bundler. &lt;br&gt;
But I was curious how can we use Parcel with React.&lt;/p&gt;

&lt;p&gt;So I decided why not give it a try.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Parceled Journey
&lt;/h2&gt;

&lt;p&gt;I have build my &lt;a href="https://sidme.tech/" rel="noopener noreferrer"&gt;portfolio&lt;/a&gt; using Vite. I decided to build same but with parcel. Now to be honest, I have never play with bundler or setup project from scratch. So this overwhelmed me a lot. But surprisingly, it was not that bumpy ride as I thought. Parcel have documented how to setup react project. I follow that and I setup my base. &lt;/p&gt;

&lt;p&gt;Now it's time to build the website. I have used tailwindCSS and typescript in my portfolio.&lt;/p&gt;

&lt;p&gt;Amazingly Typescript works out of the box with Parcel. No need for additional compiler, tsconfig , nothing. So writing or more frankly copying tsx was a breeze with no compiler error. For TailwindCSS, I have struggled a bit. especially handling the folder structure. But it worked out at last. &lt;/p&gt;

&lt;p&gt;My app was running fine, lazy loading , optimization works as expected, CSS was perfect. &lt;/p&gt;

&lt;p&gt;But , as always, how can this be so easy.After testing my app, I found out that my Atropos container/styling was not working at all. If you don't know, Atropos is the package to provide 3D parallax effect on hover. In about me page, if you hover over showcase container, there is a beautiful parallax affect. It was not working at all.&lt;/p&gt;

&lt;p&gt;I tried to re-install the package, but it was of no use. &lt;br&gt;
Upon closure inspection, I found that, regular import was not working with Atropos.&lt;/p&gt;

&lt;p&gt;Let me demonstrate you &lt;br&gt;
In my Vite app, I use - &lt;br&gt;
&lt;code&gt;import Atropos from "atropos/react";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;but, in Parcel app it was not importing anything from this path&lt;/p&gt;

&lt;p&gt;After some debugging, I find the solution, I have to use -&lt;br&gt;
&lt;code&gt;import Atropos from "atropos/atropos-react";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But still no change in the UI. &lt;br&gt;
I thought , if I have import is working fine. Then issue might be with the CSS of Atropos.&lt;br&gt;
And here also , normal import was not working in Parcel. But unlike above problem , CSS was not auto import was not working either.&lt;/p&gt;

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

&lt;p&gt;whereas in Vite app , it was working perfect -&lt;/p&gt;

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

&lt;p&gt;I don't know whether it was VScode bug, or Parcel bug, but this bug "bug" me whole day&lt;/p&gt;

&lt;p&gt;Atlast when I simply add &lt;code&gt;import "atropos/atropos.css";&lt;/code&gt; without suggestion or auto import, it works. &lt;/p&gt;

&lt;p&gt;Apart from these major fixes, few minor tweaks was required but you can infer it.&lt;/p&gt;

&lt;p&gt;And my portfolio was ready with Parcel. 🎉🎉🎉&lt;/p&gt;

&lt;p&gt;I was literally amazed how, easy is Parcel to set up and work with&lt;/p&gt;

&lt;p&gt;. &lt;/p&gt;

&lt;p&gt;If you want to check my Parcel Project visit &lt;a href="https://github.com/SiddharthaMishra-dev/React-parcel" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank You
&lt;/h3&gt;

&lt;p&gt;Visit &lt;br&gt;
&lt;a href="https://sidme.tech" rel="noopener noreferrer"&gt;My Portfolio&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Follow me &lt;br&gt;
&lt;a href="https://twitter.com/raiderfreed" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/siddharthamishra-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>bundler</category>
      <category>react</category>
    </item>
    <item>
      <title>Lazy Loading in React</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Mon, 28 Aug 2023 10:46:03 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/lazy-loading-in-react-412i</link>
      <guid>https://dev.to/sidddharthamishra/lazy-loading-in-react-412i</guid>
      <description>&lt;p&gt;If you know about web performance, then you might be familiar with the concept of Lazy Loading.As the name suggest, this technique is used to load website data lazily or with some sort of delay. &lt;br&gt;
According to mdn web docs - "Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the length of the critical rendering path, which translates into reduced page load times."&lt;/p&gt;

&lt;p&gt;I know , the definition is bit hard to understand. In simple terms it says that only those resources are loaded to browser that are needed to render the page, all other resources are blocked away. It gives two major advantages, first , it reduces the loading time and storage of the web browser and second, it also reduces the bandwidth of your internet.&lt;/p&gt;

&lt;p&gt;Now that  you are conscious with the primary understanding of the Lazy Loading, we will explore how this concept is utilised in React. As you are familiar, React uses component approach to render the webpage. For smaller websites, loading all the components at once may not affect the performance, but for large websites, loading all the resources at once can be a  nightmare. So in react we use &lt;code&gt;lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt; to apply  this strategy.&lt;/p&gt;

&lt;p&gt;Lets learn how to apply in our app. But wait, first I'll show you what does it mean to load resources at once. &lt;/p&gt;

&lt;p&gt;I have created 3 routes for my app using &lt;code&gt;react-router-dom&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  App.jsx
&lt;/h3&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="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;./pages/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;Angular&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;./pages/Angular&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;Vue&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;./pages/Vue&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App.css&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;&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/angular"&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;Angular&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;"/vue"&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;Vue&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;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;I have create three routes for my app. Each webpage has heading and a image. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- Run the React server and open your browser.&lt;/li&gt;
&lt;li&gt;- On your server port , open &lt;code&gt;chrome-dev-tools&lt;/code&gt; by inspecting the page or press &lt;code&gt;ctrl+shift+i&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;- Navigate to &lt;code&gt;sources&lt;/code&gt; tab .&lt;/li&gt;
&lt;li&gt;- In your &lt;code&gt;localhost&lt;/code&gt; folder you can verify that, although we  have no use of other routes data to display &lt;code&gt;Home&lt;/code&gt; component. But still it is loading all the resources. &lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Now you can see that all extra resources are using web storage and bandwidth that can be reduced.&lt;/p&gt;

&lt;p&gt;Lets try to implement Lazy Loading Strategy in our app.We will create a 'loading' screen for our app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loader.jsx
&lt;/h3&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Loader&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;&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;&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="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="nt"&gt;div&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Loader&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For developers sake , React provide inbuilt tool &lt;code&gt;lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt;&lt;br&gt;
We have to make some changes in the &lt;code&gt;App.jsx&lt;/code&gt; where we define our routes. &lt;/p&gt;

&lt;h3&gt;
  
  
  App.jsx
&lt;/h3&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="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="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="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;import&lt;/span&gt; &lt;span class="nx"&gt;Loader&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;./Loader&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App.css&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;Home&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="s2"&gt;./pages/Home&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;Angular&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="s2"&gt;./pages/Angular&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;Vue&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="s2"&gt;./pages/Vue&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;&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;&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;Loader&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;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;"/angular"&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;Angular&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;"/vue"&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;Vue&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;&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;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And that's it. We have added lazy loading in our react app.&lt;br&gt;
Voila!.&lt;/p&gt;

&lt;p&gt;To verify that our lazy loading is working properly, lets again check our &lt;code&gt;sources&lt;/code&gt; tab in &lt;code&gt;chrome dev tools&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;As it is visible, our app load only those resources that are needed to render Home page.&lt;/p&gt;

&lt;p&gt;This is just a small app, so this doesn't seem to effect much performance, but as your app grows managing resources become crucial. &lt;br&gt;
We can apply lazy-loading concept to any component , not just on routes.Also, for &lt;code&gt;Loader&lt;/code&gt; we can use any spinner, progress, text, image as per our requirement. &lt;/p&gt;

&lt;p&gt;For this tutorial I have used Vite for building my react app , but it works similar to &lt;code&gt;create-react-app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I hope you got get the better understanding of lazy loading and how we can use it in out react app.&lt;/p&gt;

&lt;p&gt;Feel free to comment if you have any doubt.&lt;/p&gt;

&lt;p&gt;References - &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading" rel="noopener noreferrer"&gt;mdn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://react.dev/reference/react/lazy" rel="noopener noreferrer"&gt;react docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;br&gt;
Connect me on &lt;a href="https://sidme.tech/" rel="noopener noreferrer"&gt;sidme.tech&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>reactjsdevelopment</category>
      <category>webperf</category>
    </item>
    <item>
      <title>Class Based React</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Mon, 24 Jul 2023 08:11:39 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/class-based-react-4ncm</link>
      <guid>https://dev.to/sidddharthamishra/class-based-react-4ncm</guid>
      <description>&lt;p&gt;If you are a new React developer then most probably you will be using functional based Approach for React components. But if you are an old React Developer then most likely you have used Class based Approach for React Components or might be still using. &lt;br&gt;
Now there are many debates that functional based approach is better than class based approach and vice-versa. But it's not as simple as that. Different scenarios need different approaches. It is not a rule of line that you have to only use one approach and denounce other one rather it is better to have understanding of both approaches. &lt;/p&gt;
&lt;h2&gt;
  
  
  Class based approach
&lt;/h2&gt;

&lt;p&gt;It uses ES6 Classes for defining components. If you are familiar with functional based approach then, you know that, it uses functions to define the component. In case of class based approach, all class components are extended form of &lt;code&gt;React.Component&lt;/code&gt;. Extending &lt;code&gt;React.Component&lt;/code&gt; class allows user to use prebuild API's. Basic &lt;code&gt;App&lt;/code&gt; component in Class based React looks like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World!&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;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What about Hooks
&lt;/h2&gt;

&lt;p&gt;Technically you can use hooks with classes. But it takes away the features of Class Based Approach. Nearly all functionality of hooks can be achieved using classes.&lt;br&gt;
In rare scenarios you are bound to use hooks for achieving a functionality with ease. If you are familiar with basic hooks in React then, you will not find any difficulty in grasping the Class implementation. For Example - instead of &lt;code&gt;useState()&lt;/code&gt; we use &lt;code&gt;this.state&lt;/code&gt; and &lt;code&gt;setState()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now , I'll try to explain how simple functionalities of &lt;code&gt;useState()&lt;/code&gt; and &lt;code&gt;useEffect()&lt;/code&gt; hooks are achieved using classes.&lt;/p&gt;
&lt;h2&gt;
  
  
  constructor()
&lt;/h2&gt;

&lt;p&gt;Before learning about state management, it is very crucial to get the gist of &lt;code&gt;constructor()&lt;/code&gt;. Basically &lt;code&gt;constructor()&lt;/code&gt; are the methods that are called when object of Class is initialized. This method is used to setup state of the Component with initial values.&lt;/p&gt;

&lt;p&gt;In the constructor another method that is important is &lt;code&gt;super&lt;/code&gt; method. By calling this method we get access to methods and states of parent classes in which this is called. This method is useful for accessing &lt;code&gt;props&lt;/code&gt; in the Component.&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="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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World!&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;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Counter App
&lt;/h2&gt;

&lt;p&gt;Now lets try to implement counter in Class based Approach. If you are familiar with hooks, then implementing this functionality will cost no effort.&lt;/p&gt;

&lt;p&gt;We will start by defining state. In the Component&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;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For changing the value of &lt;code&gt;count&lt;/code&gt;,we will be using &lt;code&gt;setState()&lt;/code&gt; method . As&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.setState({count:this.state.count+1})&lt;/code&gt;&lt;br&gt;
&lt;code&gt;this.setState({count:this.state.count-1})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our basic Counter App is ready. &lt;br&gt;
Complete code&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="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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&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="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;&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;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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;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="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="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&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="k"&gt;this&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="mi"&gt;1&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="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;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="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&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="k"&gt;this&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="mi"&gt;1&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="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;/&amp;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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code we initialize &lt;code&gt;count&lt;/code&gt; state and change its value using &lt;code&gt;setState()&lt;/code&gt; method. It is very similar to &lt;code&gt;useState()&lt;/code&gt; hook.  &lt;code&gt;this&lt;/code&gt; keyword is typically used here to bind method with its corresponding class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data fetching
&lt;/h2&gt;

&lt;p&gt;In functional based approach, we use &lt;code&gt;useEffect()&lt;/code&gt; hook to fetch data from the API. Typically this hook is used for side effects in the web application. Because we don't want to stop the rendering of page, therefore we fetch data as a side effect. &lt;br&gt;
In class based approach we use &lt;code&gt;componentDidMount()&lt;/code&gt; method . It allows user to perform actions when component is rendered on the webpage. If you deep dive into both the methods then, they are very different to their core, but for achieving these types of functionality, they work pretty much identical.&lt;br&gt;
Now let's learn it by the code- &lt;br&gt;
In this example , I will be using DummyJSON product API. You can get it &lt;a href="https://dummyjson.com/docs/products" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First we will create a fetching function as&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="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;response&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://dummyjson.com/products&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;result&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;In this example, I'll be storing the result in &lt;code&gt;products&lt;/code&gt; state.&lt;/p&gt;

&lt;p&gt;Now it's time to call &lt;code&gt;fetchData()&lt;/code&gt; method so that it doesn't block rendering of the page.&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;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchData&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;For the &lt;code&gt;products&lt;/code&gt; state&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;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="o"&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;h2&gt;
  
  
  &lt;code&gt;App.js&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;products&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="p"&gt;}&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;response&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://dummyjson.com/products&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;products&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;&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;Data fetching&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  console
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz9ef0mhw3mmbu391kmj2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz9ef0mhw3mmbu391kmj2.png" alt="console log of the products state" width="765" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it, now you know the basic concept of class based approach to react. I tried my best to explain the concept. Hope you learnt a lot.&lt;/p&gt;

&lt;p&gt;References -&lt;br&gt;
&lt;a href="https://react.dev/reference/react/Component#componentdidmount" rel="noopener noreferrer"&gt;react.dev&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/whats-the-difference-between-super-and-superprops-in-react/" rel="noopener noreferrer"&gt;super and props&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For any queries , connect with me &lt;a href="https://siddharthamishra.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>es6</category>
    </item>
    <item>
      <title>React-Redux</title>
      <dc:creator>Siddhartha Mishra</dc:creator>
      <pubDate>Fri, 07 Jul 2023 10:20:27 +0000</pubDate>
      <link>https://dev.to/sidddharthamishra/react-redux-181</link>
      <guid>https://dev.to/sidddharthamishra/react-redux-181</guid>
      <description>&lt;p&gt;Before directly jumping to &lt;code&gt;react-redux&lt;/code&gt;. It is crucial to be aware about React first. I will be very concise.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is React
&lt;/h2&gt;

&lt;p&gt;React is the front-end JavaScript library that is used to design and build web apps. Since it is the library, it means there are multiple packages/modules that are used to perform different tasks. Earlier it was mainly dealt in class component but since the introduction of "hooks" class based components nearly became obsolete. &lt;/p&gt;

&lt;p&gt;Now for react hooks, they provide function based components instead of class based components. &lt;br&gt;
In React we build components and then you integrate them to build a complete app. For Example, in a particular website- navbar, sidebar, search, footer etc. are different components. Hooks provide a way to store and reuse logic between states of the application.&lt;/p&gt;

&lt;p&gt;Here is the basic structure of a react component.&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="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;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;React, Redux and Redux-toolkit&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="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;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most common hook in React is &lt;code&gt;useState&lt;/code&gt;. This hook stores the state  of the application and provide a method to change it. While building an application, sometimes number of states can reach to &lt;br&gt;
overwhelming number. In that case, it can be very exhaustive to properly manage states. To solve this problem, we use Redux.&lt;/p&gt;
&lt;h2&gt;
  
  
  redux
&lt;/h2&gt;

&lt;p&gt;First Redux is the state management library for any JavaScript application. Redux provides a centralized storage to store state for the application. It means, instead of creating state for every single component, we create states that are accessible to all the components of the application.&lt;/p&gt;

&lt;p&gt;Before moving on to react-redux, there are few important terminologies that crucial to learn in redux.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;store:&lt;/strong&gt; As the name suggests, it is the centralized location to store and manage the states of the application. There are two important methods that are used in store. First one &lt;code&gt;getState()&lt;/code&gt;is used to retrieve the current state of the application and second one &lt;code&gt;dispatch(action)&lt;/code&gt;is used to initiate the state change. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actions:&lt;/strong&gt; are the JavaScript Object that signals the store to that state is needed to change. It describes the &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;payload&lt;/code&gt;. &lt;code&gt;payload&lt;/code&gt; can be considered as the data or argument that can be passed to the store.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducers:&lt;/strong&gt; These are pure JavaScript functions that basically changes the state. These functions take &lt;code&gt;actions&lt;/code&gt; and current state and then return the updated state. &lt;code&gt;actions&lt;/code&gt; are the operations that are performed on the state.&lt;/p&gt;

&lt;p&gt;Note: &lt;code&gt;state&lt;/code&gt; in Redux are immutable i.e., they cannot be changed. Therefore &lt;code&gt;reducers&lt;/code&gt; doesn't actually changes the state rather they create an updated copy of &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  react-redux
&lt;/h2&gt;

&lt;p&gt;react-redux is the library that provides linkage between React and Redux.&lt;/p&gt;

&lt;p&gt;Now let's try to understand redux with simple counter app.&lt;/p&gt;

&lt;p&gt;There are few packages that we need to use &lt;code&gt;redux&lt;/code&gt; in our &lt;code&gt;react&lt;/code&gt; application.&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="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="nx"&gt;redux&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;redux&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly we will create &lt;code&gt;store&lt;/code&gt;, where we will store our states for the application. To keep example easy, I am writing &lt;code&gt;reducer&lt;/code&gt; in the same file where we store our state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;store.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;createStore&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;redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducerFxn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;counter&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="nx"&gt;action&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;if&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="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;increase&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="na"&gt;counter&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;counter&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="k"&gt;if&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="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;decrease&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="na"&gt;counter&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;counter&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&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;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducerFxn&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;store&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above code segment, there is &lt;code&gt;reducerFxn&lt;/code&gt; which takes current state and action as its input. Then according to &lt;code&gt;action.type&lt;/code&gt; it returns new &lt;code&gt;state&lt;/code&gt; with updated value. In this case there are increase and decrease action type. and in the last there is default return state.(specifically for initial render)&lt;/p&gt;

&lt;p&gt;For the application, to access the store, wraps whole app in the &lt;code&gt;&amp;lt;Provider&amp;gt;&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;React&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ReactDOM&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-dom/client&lt;/span&gt;&lt;span class="dl"&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;App&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;./App.jsx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./index.css&lt;/span&gt;&lt;span class="dl"&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;Provider&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-redux&lt;/span&gt;&lt;span class="dl"&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;store&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;./store/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;render&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;Provider&lt;/span&gt; &lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StrictMode&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;App&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StrictMode&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;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We pass the &lt;code&gt;store&lt;/code&gt; props so that each component inside &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt; can access the store. We import the &lt;code&gt;store&lt;/code&gt; from &lt;code&gt;store.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At last, we will build our main application component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;useDispatch&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-redux&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;useSelector&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="o"&gt;=&amp;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;counter&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;dispatch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;useDispatch&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;increment&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;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="s1"&gt;increase&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrement&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;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="s1"&gt;decrease&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;(&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;Counter 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="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&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;increment&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;increase&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;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;decrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;decrease&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useSelector&lt;/code&gt; hook is used to access the state from the &lt;code&gt;store&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;useDispatch&lt;/code&gt; hook is used to initiate or dispatch the action for the &lt;code&gt;reducer&lt;/code&gt;(in this case &lt;code&gt;reducerFxn&lt;/code&gt;).We cannot directly use dispatch hook so we have to initialize it. &lt;/p&gt;

&lt;p&gt;Our Counter application is ready🥳🥳&lt;/p&gt;

&lt;p&gt;I hope you get the basic gist of the 'redux' and 'react-redux'.&lt;/p&gt;

&lt;p&gt;Here are some references that can be helpful-&lt;br&gt;
&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;react&lt;/a&gt; &lt;a href="https://redux.js.org/introduction/getting-started" rel="noopener noreferrer"&gt;redux&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can connect with me &lt;a href="https://siddharthamishra.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

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