<?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: RiyaNegi</title>
    <description>The latest articles on Forem by RiyaNegi (@riyanegi).</description>
    <link>https://forem.com/riyanegi</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%2F600728%2Fa408c935-e9b2-452e-84cb-70f36aad9aa9.jpeg</url>
      <title>Forem: RiyaNegi</title>
      <link>https://forem.com/riyanegi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/riyanegi"/>
    <language>en</language>
    <item>
      <title>Web Page Not updating on back navigation? The bfcache Problem and Fix</title>
      <dc:creator>RiyaNegi</dc:creator>
      <pubDate>Fri, 21 Feb 2025 17:06:04 +0000</pubDate>
      <link>https://forem.com/riyanegi/web-page-not-updating-on-back-navigation-the-bfcache-problem-and-fix-954</link>
      <guid>https://forem.com/riyanegi/web-page-not-updating-on-back-navigation-the-bfcache-problem-and-fix-954</guid>
      <description>&lt;p&gt;TLDR -&amp;gt; For developers looking to directly jump to the solution &lt;br&gt;
➡️ click here&lt;/p&gt;



&lt;p&gt;Imagine you're building a simple To-Do List app. Your app has two pages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To-Do List Page (/todos) – Displays all tasks and has a "Create Task" button.&lt;/li&gt;
&lt;li&gt;Task Details Page (/todos/:id) – Shows details of a specific task.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A user &lt;strong&gt;creates a new task&lt;/strong&gt; on the To-Do List page. ✅ It appears in the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The user &lt;strong&gt;clicks the new task&lt;/strong&gt;, goes to its details page, and updates something.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The user &lt;strong&gt;clicks the browser back button&lt;/strong&gt; to return to the To-Do List.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;💥 &lt;strong&gt;The newly created task is missing from the list!&lt;/strong&gt; But when they manually refresh, it reappears.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At first, I thought it was a react state update issue and tried to debug in that direction. However, on deeper investigation, I noticed that the API calls were made as expected on the initial render, but were getting old data as the response. This was weird because the API was working perfectly fine in every other case except for this backwards-navigation case. &lt;br&gt;
After searching for this issue I found out there's something called as Back/Forward Cache (bfcache) in modern browsers which is an optimization technique used to reduce load times. However, in pages that require updated data, this can prove to be an issue. &lt;/p&gt;

&lt;p&gt;Here’s a technical breakdown of how it works:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Freezing the Page (Page Lifecycle Management)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a user leaves a page (e.g., clicking a link or using the back button), the browser pauses all JavaScript execution and freezes the page state.&lt;/li&gt;
&lt;li&gt;Instead of destroying the page (like a normal navigation would), it is kept in memory, including the DOM, JavaScript heap, and event listeners.&lt;/li&gt;
&lt;li&gt;The page is put into a "suspended" state where it stops running scripts but keeps all its data intact.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Storing in bfcache&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unlike normal caching (which only stores fetched resources like images, CSS, and HTML), bfcache stores the entire page, including JavaScript memory.&lt;/li&gt;
&lt;li&gt;This means when you return, there’s no need to re-run scripts, make API calls, or reload assets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Restoring the Page (pageshow Event)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the user navigates back or forward, the browser instantly restores the frozen page, unfreezing JavaScript execution from where it left off.&lt;/li&gt;
&lt;li&gt;The pageshow event fires, with &lt;code&gt;event.persisted === true&lt;/code&gt;, indicating that the page was loaded from bfcache instead of reloading from the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. When Does bfcache Not Work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;bfcache won't work if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The page has unload event listeners (old websites used this).&lt;/li&gt;
&lt;li&gt;Certain security restrictions apply (e.g., authentication headers, no-store cache).&lt;/li&gt;
&lt;li&gt;The page uses window.opener or specific JavaScript APIs like IndexedDB in ways that prevent caching.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;The Solution&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When the page is loaded from the BFCache, the &lt;code&gt;onload&lt;/code&gt; event won't be triggered. So, in order to determine if the page was restored from the bfcache, we must instead listen to the &lt;code&gt;pageshow&lt;/code&gt; event and examine the &lt;code&gt;event.persisted&lt;/code&gt; attribute. This can be useful for determining whether data is outdated or not. If it is restored from bfcache, it will be stale so then update it accordingly using an API re-fetch or update function.&lt;/p&gt;

&lt;p&gt;You can use the following event listener to see whether or not the user came from the BFCache, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener('pageshow', function(event) {
  if (event.persisted) {
    // The page was restored from the bfcache
    // Re-fetch data
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Alternate solution&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Adding headers to your API call like the "Cache-Control": "no-store" and "Pragma": "no-cache" headers ensure that the browser and intermediate caches do not store any response, forcing a fresh API call on every request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time data that must always be up-to-date.&lt;/li&gt;
&lt;li&gt;Financial transactions, live dashboards, or frequently changing content.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Avoid for:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static or rarely updated data (e.g., images, documents, or user profiles).&lt;/li&gt;
&lt;li&gt;Situations where reducing server load and improving performance is a priority.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const response = await fetch(
      `api-url-call`,
      {
        headers: {
          "Cache-Control": "no-store",
          Pragma: "no-cache"
        },
        cache: "no-store"
      }
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Essential vs. Optional&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Cache-Control: "no-store" → Most important&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures the browser and intermediate caches do not store the response at all.&lt;/li&gt;
&lt;li&gt;Works in modern browsers and is the recommended approach.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;⚠️ Pragma: "no-cache" → Optional&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A legacy header from HTTP/1.0; mainly useful for compatibility with older systems.&lt;/li&gt;
&lt;li&gt;Can be omitted if you're dealing only with modern browsers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ cache: "no-store" (fetch option) → Recommended for fetch calls&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures fetch() bypasses the cache entirely.&lt;/li&gt;
&lt;li&gt;This is only for fetch requests and does not affect browser cache behavior for other requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Doesn't bfcache Work in Local but Works in Staging/Production?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Localhost Behaves Differently 🏠&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some browsers disable bfcache by default for localhost or treat it differently to avoid issues during development.&lt;br&gt;
In local, navigation may always trigger a full page reload instead of restoring from memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hot Reloading &amp;amp; Code Changes 🔄&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In local, tools like React Fast Refresh or Webpack invalidate page states when code updates.&lt;br&gt;
Even without refreshing, this can prevent bfcache from storing the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Missing Production Headers &amp;amp; Settings ⚙️&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Staging/production environments often have caching headers that enable bfcache.&lt;br&gt;
Local development servers might lack these, preventing pages from being stored in memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Service Workers &amp;amp; API Behavior 🌐&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In production, service workers or caching strategies might allow bfcache to work properly.&lt;br&gt;
Locally, APIs might behave differently (e.g., not using caching rules), leading to different navigation behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep learning!&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkepk8golkbzf2zt7hbpp.gif" 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%2Fkepk8golkbzf2zt7hbpp.gif" alt="Image description" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Solving Issues with og: Meta Tags: A Comprehensive Guide</title>
      <dc:creator>RiyaNegi</dc:creator>
      <pubDate>Sat, 25 May 2024 07:46:47 +0000</pubDate>
      <link>https://forem.com/riyanegi/solving-issues-with-og-meta-tags-a-comprehensive-guide-22c2</link>
      <guid>https://forem.com/riyanegi/solving-issues-with-og-meta-tags-a-comprehensive-guide-22c2</guid>
      <description>&lt;p&gt;Meta tags are snippets of text that describe a page's content; the og: meta tags, in particular, are essential for social media sharing. They control how your content appears when shared on platforms like Facebook, LinkedIn or Twitter, playing a crucial role in attracting clicks and engagement for your website. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Issue
&lt;/h2&gt;

&lt;p&gt;Despite setting up my og: meta tags correctly, I faced a frustrating problem: the shared links on Facebook or linkedin were not displaying the correct thumbnail of the post. It showed the general thumbnail of my website, but not the specific post thumbnail I wanted from within the website. This inconsistency not only marred the appearance of my website's link but also potentially reducing traffic and engagement. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby version&lt;/strong&gt;&lt;/p&gt;

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

&lt;span class="n"&gt;set_meta_tags&lt;/span&gt; &lt;span class="ss"&gt;:og&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="ss"&gt;:title&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;:description&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;:site_name&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'XYZ'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;:image&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cover_image_url&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;JavaScript version&lt;/strong&gt;&lt;/p&gt;

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

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${template.name}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${template.description}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:site_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${template.cover_image_url}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Troubleshooting Steps
&lt;/h2&gt;

&lt;p&gt;When troubleshooting issues with og: meta tags, it's essential to follow a systematic approach to identify and resolve the problem. Here are the steps I took:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Initial Checks&lt;/strong&gt;&lt;br&gt;
First, ensure that the meta tags are correctly placed within the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section of your HTML document. Incorrect syntax, placement or indentation can cause social media platforms to misinterpret the tags.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nx"&gt;DOCTYPE&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sample Title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sample Description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:site_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/image.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/html&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. Check the URL&lt;/strong&gt;&lt;br&gt;
Ensure that the URLs used in your meta tags are absolute and correct. Relative URLs might not be interpreted correctly by social media platforms.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;3. Use Debugging Tools&lt;/strong&gt;&lt;br&gt;
Each major social media platform provides tools to help debug and validate your meta tags. These tools can be immensely helpful in identifying issues.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Facebook Debugging Tool&lt;/strong&gt;: Sharing Debugger&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Twitter Debugging Tool&lt;/strong&gt;: Card Validator&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LinkedIn Debugging Tool&lt;/strong&gt;: Post Inspector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enter your URL in these debugger tools. Review the link preview and make necessary adjustments based on the feedback.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Common Pitfalls&lt;/strong&gt;&lt;br&gt;
Ensure that your images meet the size and format requirements of each platform. For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Facebook&lt;/strong&gt;: Minimum 200x200 pixels. Recommended size is 1200x630 pixels.&lt;br&gt;
&lt;strong&gt;Twitter&lt;/strong&gt;: Minimum 120x120 pixels. Recommended size is 1200x628 pixels.&lt;br&gt;
&lt;strong&gt;LinkedIn&lt;/strong&gt;: Minimum 1200x627 pixels. Recommended size is 1200x627 pixels.&lt;/p&gt;

&lt;p&gt;Ensure the images are in the correct format (JPEG, PNG, etc.) and are accessible via the URL provided.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.Cache Issues&lt;/strong&gt;&lt;br&gt;
Sometimes, even after fixing the meta tags, social media platforms might still show old data due to caching. Use the debugging tools mentioned above to refresh the cache. For Facebook, you can force a rescrape using the Sharing Debugger.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;After extensive testing, I discovered that the issue was due to the absence of the &lt;code&gt;og:url&lt;/code&gt; meta tag. Social media platforms sometimes rely on this tag to properly link the meta information to the specific page. Here's how I fixed it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding the og:url Meta Tag&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby Version&lt;/strong&gt;&lt;/p&gt;

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

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply_meta_tag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;set_meta_tags&lt;/span&gt; &lt;span class="ss"&gt;og: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;title: &lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;url: &lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;original_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;##Add url explicitly&lt;/span&gt;
    &lt;span class="ss"&gt;description: &lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;site_name: &lt;/span&gt;&lt;span class="s1"&gt;'XYZ'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;image: &lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cover_image_url&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;JavaScript version&lt;/strong&gt;&lt;/p&gt;

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

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${currentUrl}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// Add url explicitly&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${template.name}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${template.description}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:site_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;${template.cover_image_url}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;template&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

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

&lt;/div&gt;

&lt;p&gt;After making these changes, I verified the updated link preview on all platforms. The link previews now displayed the correct image consistently across Facebook, Twitter, and LinkedIn.&lt;/p&gt;

&lt;p&gt;If you've encountered similar issues with og: meta tags, share your experiences and solutions in the comments below!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.gifer.com%2F7Uw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.gifer.com%2F7Uw.gif" alt="sad man"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>metatags</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Dummy guide to building scalable react web architecture</title>
      <dc:creator>RiyaNegi</dc:creator>
      <pubDate>Tue, 30 Jan 2024 07:30:38 +0000</pubDate>
      <link>https://forem.com/riyanegi/dummy-guide-to-building-scalable-react-web-architecture-53dc</link>
      <guid>https://forem.com/riyanegi/dummy-guide-to-building-scalable-react-web-architecture-53dc</guid>
      <description>&lt;p&gt;Over the years, working as a frontend engineer, I've been involved in multiple React projects. Every time I start a new project, the question that comes in my mind is, "How can I establish a stable, scalable, yet quick foundation to support the app?" The answer varies depending on the project's size, but the core question remains the same. In this article, we will delve into this question, providing a straightforward, no-nonsense insight into building a robust foundation for your project so that you can quickly get started!&lt;/p&gt;

&lt;p&gt;Building a scalable architecture from the outset and conducting necessary research is crucial. A strong foundation allows you to make various edits and adjustments to your projects later on. React is unopinionated, meaning you can flexibly use it according to your convenience.&lt;br&gt;
However, over-engineering your codebase for small, one-page apps, MVP demos or feature modules can be counterproductive. In these cases, opting for a basic file structure like the one shown below is preferable and saves you from spending unnecessary extra time maintaining it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; src
   &amp;gt; Components
      &amp;gt; Module.js
      &amp;gt; ModuleContainer.js
      &amp;gt; styles.css
   &amp;gt; App.js
   &amp;gt; App.css
   &amp;gt; Index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But on the other hand, for more complex structures, involving a full-fledged backend, frontend, reducers, and context, you should ask yourself three important questions when building its foundation.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;- How to design a loosely coupled React architecture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If React components are too interdependent, it can lead to the over-rendering of unnecessary components, slowing down the app. Aim to separate the concerns of components as much as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How to decompose your application UI into components?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
 Decomposing React components is essential for code readability and maintainability, especially when working in a team. Aim for a file with a maximum of 150-200 lines of code, ideally keeping it around 100 lines for better clarity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How to Implement data fetching with Redux/Flux?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
 Middleware/global state store can add extra complexity to your app. Ensure your folder structure is neatly laid out, as it requires some boilerplate code setup. Once in place, it becomes a valuable tool for handling complex interactions and dynamic updates in larger, more complex apps. It's an extremely good investment for larger complex apps.&lt;/p&gt;

&lt;p&gt;Lay out a plan addressing these questions according to your project, and you'll end up with a structure similar to the one below.&lt;/p&gt;

&lt;p&gt;Ps - If you want a working example here's a repo link to one of my projects to understand the structure better &lt;br&gt;
&lt;a href="https://github.com/RiyaNegi/Social-Monitoring"&gt;https://github.com/RiyaNegi/Social-Monitoring&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Backend(server)
&amp;gt; Frontend(client)
  &amp;gt; assets (comprising of media files)
  &amp;gt; components
    &amp;gt; Module 
      &amp;gt; index.js
      &amp;gt; index.css
    &amp;gt; Module2
      &amp;gt; index2.js
      &amp;gt; index2.css
  &amp;gt; context
    &amp;gt; actions
    &amp;gt; reducers
    &amp;gt; InitialState.js
    &amp;gt; Provider.js
  &amp;gt; helpers 
    &amp;gt; AxiosInstance.js (an instance of Axios with customized settings that are resuable throughout the application. )
    &amp;gt; Constants (variables that can be reused throughout the app)
  &amp;gt; routes
  &amp;gt; utils (Comprising of functions that will be repeatedly used throughout the app)
  &amp;gt; App.css
  &amp;gt; App.js
  &amp;gt; App.test.js
  &amp;gt; index.css
  &amp;gt; index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Starting a new project can be overwhelming. The easiest way to navigate this challenge is to refer to good GitHub repositories and study their folder structure. Don't spend too much time worrying about building the perfect foundation structure, as changes and adjustments are part of the learning process. There will always be unforeseen changes, no matter how hard you try to plan. Just do your best to be curious about everything and get started with it as soon as possible!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jOQxNZJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media.tenor.com/GT5_1-5ftYkAAAAM/dance-fun.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jOQxNZJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media.tenor.com/GT5_1-5ftYkAAAAM/dance-fun.gif" alt="Cheer" width="220" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting started with Typescript with React Hooks</title>
      <dc:creator>RiyaNegi</dc:creator>
      <pubDate>Wed, 23 Jun 2021 17:26:43 +0000</pubDate>
      <link>https://forem.com/riyanegi/getting-started-with-typescript-with-react-hooks-2021-5cfd</link>
      <guid>https://forem.com/riyanegi/getting-started-with-typescript-with-react-hooks-2021-5cfd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Typescript&lt;/strong&gt; is the next big thing in the &lt;strong&gt;&lt;em&gt;Front End Development&lt;/em&gt;&lt;/strong&gt; domain and if you are looking to upgrade your skills from a junior to an intermediate frontend developer, then it is a must have skill.&lt;br&gt;
              Typescript is a a superscript of javascript, so if you know javascript you are already half way there! What Typescript brings to the table is more error-free code with type checking during run time and a code which can be easily documented.&lt;/p&gt;

&lt;p&gt;This article is for developers who already know react and javascript and want to shift to typescript, so I won't be focusing on any react concepts. This is a crash course on understanding the basics of using &lt;strong&gt;Typescript with React.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FXyaQAnihoZBU3GmFPl%2Fgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FXyaQAnihoZBU3GmFPl%2Fgiphy.gif" alt="random gif"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Index
&lt;/h2&gt;

&lt;p&gt;We will be covering all the topics necessary for understanding the basics of using react with typescript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Info&lt;/li&gt;
&lt;li&gt;Setup&lt;/li&gt;
&lt;li&gt;Handling State&lt;/li&gt;
&lt;li&gt;Handling Props&lt;/li&gt;
&lt;li&gt;Handling Functions&lt;/li&gt;
&lt;li&gt;Handling Events&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this article we will be building a simple personal watch list that records the movies you input by taking it's name, rating and review. It looks something like.(It is a little stylised but the underlying code is basic)&lt;br&gt;
&lt;a href="https://media.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%2Ftzharpw078z3ltq533cq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ftzharpw078z3ltq533cq.jpg" alt="App Image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a id="info"&gt;&lt;/a&gt;Info
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/RiyaNegi/Typescript-with-react" rel="noopener noreferrer"&gt;Github repo for the project&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://riyanegi.github.io/Typescript-with-react/" rel="noopener noreferrer"&gt;Online Demo of the Project&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a id="setup"&gt;&lt;/a&gt;Setup
&lt;/h2&gt;

&lt;p&gt;Let's start with initializing our project! I am naming my project typescript-with-react but you can go with anything you like. &lt;br&gt;
&lt;code&gt;npx create-react-app --template typescript typescript-with-react&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Okay now change directory to get into your project folder and proceed usually as you do with any react project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd typescript-with-react/
code .
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your code structure should be looking something like this&lt;br&gt;
 &lt;a href="https://media.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%2F8w5e7ntt60c04ir5et59.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F8w5e7ntt60c04ir5et59.png" alt="COde structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how the files have &lt;code&gt;.ts&lt;/code&gt; or &lt;code&gt;.tsx&lt;/code&gt; extension. That denotes that those files are transpiled in typescript.&lt;br&gt;
Okay now let's get into the Typescript nitty gritty!&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a id="state"&gt;&lt;/a&gt;Handling state
&lt;/h2&gt;

&lt;p&gt;In typescript it's necessary to mention type definitions of all variables and functions and what they return. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For handling state in react you need to first create an interface where you mention the data type of the variables. &lt;/li&gt;
&lt;li&gt;In the example below, we have created an interface called &lt;code&gt;IState&lt;/code&gt; (You can name it whatever you like). &lt;/li&gt;
&lt;li&gt;The interface &lt;code&gt;IState&lt;/code&gt; is where we will write our type definition of how we want the state variables to be, which in this case is an array of objects. To denote that we add square bracket after the type definitions. And then while using useState, add &lt;code&gt;&amp;lt;IState["form"]&amp;gt;&lt;/code&gt; which denotes that the state should be accepting values in the specified format only(IState format in this case which is taking the object 'form' as input format)
&lt;img src="https://media.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%2Fw817yp7opfpd5i8c1wik.jpg" alt="State image"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have exported IState so that we can use it in another file later on.&lt;br&gt;
An alternate inline method of adding state would be as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [counter, setCounter] = useState&amp;lt;{name:string,rate:number,review?:string}[]&amp;gt;([])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In our case project, we want review to be an optional field while name of the movie and rating of the movie to be compulsory field. &lt;/li&gt;
&lt;li&gt;Thus for review we have done &lt;code&gt;review?:string&lt;/code&gt; where the question mark denotes the value of review could either be a string or undefined. However for &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;rate&lt;/code&gt; we have strict type definitions which won't accept anything apart from the assigned type definitions.&lt;/li&gt;
&lt;li&gt;You can add more than one type definitions to a variable in the following way:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inputValue:number | string | null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the variable &lt;code&gt;inputValue&lt;/code&gt; can either be a data type of number, string or even a null value &lt;br&gt;
Note: null and undefined are not the same data types.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a id="props"&gt;&lt;/a&gt;Handling Props
&lt;/h2&gt;

&lt;p&gt;For handling props in react, both the sending and recieving side of the component should make a clear declaration of the type and number of variables or functions involved.Typescript will give an error if anything is missing either on the sending or receiving side&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the sending side.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;List form={form} /&amp;gt;
&amp;lt;Form form={form} setForm={setForm} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From &lt;code&gt;App.tsx&lt;/code&gt; we are sending one object ie. &lt;code&gt;form&lt;/code&gt; to &lt;code&gt;List.tsx&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's take a look at the &lt;code&gt;List&lt;/code&gt; component's recieving side now.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { IState as IProps } from "../App"

const List: React.FC&amp;lt;IProps&amp;gt; = ({ form }) =&amp;gt; {
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;List is a react functional component that accepts props. In typescript to show that we add&lt;code&gt;React.FC&amp;lt;IProps&amp;gt;&lt;/code&gt; after the &lt;code&gt;List&lt;/code&gt; component declaration.&lt;/li&gt;
&lt;li&gt;We can import the &lt;code&gt;IState&lt;/code&gt; under the alias &lt;code&gt;IProps&lt;/code&gt; since we know that the type definitions of the object &lt;code&gt;form&lt;/code&gt; are exactly the same as the &lt;code&gt;IState&lt;/code&gt; object. &lt;/li&gt;
&lt;li&gt;We can then destructure &lt;code&gt;form&lt;/code&gt; in the parameters and use it in the function component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the second example, from &lt;code&gt;App.tsx&lt;/code&gt; we are sending one object ie. &lt;code&gt;form&lt;/code&gt; and one function ie.&lt;code&gt;setForm&lt;/code&gt; to &lt;code&gt;Form.tsx&lt;/code&gt;&lt;br&gt;
Let's take a look at the &lt;code&gt;Form&lt;/code&gt; component's recieving side now.&lt;br&gt;
&lt;a href="https://media.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%2Fku6td7fmh0dwob3ol48m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fku6td7fmh0dwob3ol48m.png" alt="Form component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see here in this component as well we imported &lt;code&gt;IState&lt;/code&gt; under the alias &lt;code&gt;Props&lt;/code&gt;, however we have made some customized changes here. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Here we created a new interface called &lt;code&gt;IProps&lt;/code&gt; that specifies the type defintion of props incoming since we had to specify the type of &lt;code&gt;setForm&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We mention &lt;code&gt;form: Props["form"]&lt;/code&gt; which means form should be assigned the type definition of &lt;code&gt;IState&lt;/code&gt; which is imported under the alias &lt;code&gt;Props&lt;/code&gt;&lt;br&gt;
And then similarly we will now do it for &lt;code&gt;setForm&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Protip : to know the type definitions of something you don't have a clue about, just hover over that element like this and copy the type definitions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ffqik41c2r1jwfg27pqst.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ffqik41c2r1jwfg27pqst.jpg" alt="hover image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since we have already declared the type definitions of props as &lt;code&gt;Props["form"]&lt;/code&gt;, we can cut short the type definition of &lt;code&gt;setForm&lt;/code&gt; and write it this way instead
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; setForm: React.Dispatch&amp;lt;React.SetStateAction&amp;lt;Props["form"]&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Then simply destructure &lt;code&gt;form&lt;/code&gt; and &lt;code&gt;setForm&lt;/code&gt; in the parameters of the &lt;code&gt;Form&lt;/code&gt; function and use it in the component.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;a id="functions"&gt;&lt;/a&gt;Handling Functions
&lt;/h2&gt;

&lt;p&gt;In react-typescript, you need to mention the type of output that function is giving.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here in this example we have called &lt;code&gt;mapList()&lt;/code&gt; function to map through the array of list and give table row as an output, which is a JSX element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fhmce8pxggkr0he0lxiom.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fhmce8pxggkr0he0lxiom.jpg" alt="function image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To mention the output type of this function, add &lt;code&gt;: JSX.Element[]&lt;/code&gt; after the parameters, which denotes that the function is supposed to return an array of JSX elements.&lt;/li&gt;
&lt;li&gt;An interesting thing to note is that we have written a nested return statement because the first return points towards the mapping function.&lt;/li&gt;
&lt;li&gt;However we aren't supposed to return the mapping function and thus typescript would give an error if we had only one return statement since we have mentioned our return type as &lt;code&gt;JSX.Element[]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We did a nested return statement inside the map function so that it specifically returns a pure JSX element ie. a table row in this case.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Protip: If you aren't sure what the return type is,hover over the function to know it's return type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Alternatively if a function isn't returning anything, mention it's null return type as &lt;code&gt;:void&lt;/code&gt; after parameters in this way :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const randomFunction = (): void =&amp;gt; {
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a id="events"&gt;&lt;/a&gt;Handling Events
&lt;/h2&gt;

&lt;p&gt;For handling events with react typescript we will take a look at the following DOM events called by the following JSX elements in &lt;code&gt;Form&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input className="inputBox" type='text' name="name" value={input.name} onChange={(e) =&amp;gt; handleChange(e)} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;textarea className="inputBox" name="review" value={input.review} onChange={(e) =&amp;gt; handleChange(e)}&amp;gt;&amp;lt;/textarea&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;input&lt;/code&gt; tag has a DOM property called &lt;code&gt;onChange&lt;/code&gt; which calls &lt;code&gt;handleChange&lt;/code&gt; when an event is triggered.&lt;br&gt;
For this we create a function which knows that it will be recieving an HTML element in parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const handleChange = (e: React.ChangeEvent&amp;lt;HTMLInputElement | HTMLTextAreaElement&amp;gt;): void =&amp;gt; {
        setInput({
            ...input,
            [e.target.name]: e.target.value
        })
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we are declaring that &lt;code&gt;e&lt;/code&gt; will either be of type &lt;code&gt;React.ChangeEvent&amp;lt;HTMLInputElement&amp;gt;&lt;/code&gt; which is what the &lt;code&gt;input&lt;/code&gt; tag will send. &lt;/li&gt;
&lt;li&gt;And since for the movie review field we are using a textarea tag instead of an input tag the &lt;code&gt;e&lt;/code&gt; could also be &lt;code&gt;React.ChangeEvent&amp;lt;HTMLTextAreaElement&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thus the entire type definition of &lt;code&gt;e&lt;/code&gt; can be written as &lt;code&gt;e: React.ChangeEvent&amp;lt;HTMLInputElement | HTMLTextAreaElement&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We need to add &lt;code&gt;:void&lt;/code&gt; to specify that this function won't be returning anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the second example we will take a look at the &lt;code&gt;onClick&lt;/code&gt; event called by the form submit button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button className="button" type="submit" onClick={(e) =&amp;gt; handleClick(e)}&amp;gt;Submit&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleClick = (e: React.MouseEvent&amp;lt;HTMLButtonElement&amp;gt;): void =&amp;gt; {
        e.preventDefault();
        if (!input.name || !input.rate) {
            return
        }
        setForm([...form, {
            name: input.name,
            rate: parseInt(input.rate),
            review: input.review
        }])
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to &lt;code&gt;handleChange&lt;/code&gt; function the &lt;code&gt;handleClick&lt;/code&gt; function takes a proper type definition of &lt;code&gt;e&lt;/code&gt; which in this case is &lt;code&gt;React.MouseEvent&amp;lt;HTMLButtonElement&amp;gt;&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;That's it for this crash course! Hope this gives you a fair enough idea of how to use typescript in react. Keep learning and have a great day!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.pinimg.com%2Foriginals%2Fd2%2Fdc%2F98%2Fd2dc98de6dd20c4a34abf3ae1ee1ef49.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.pinimg.com%2Foriginals%2Fd2%2Fdc%2F98%2Fd2dc98de6dd20c4a34abf3ae1ee1ef49.gif" alt="happy gif"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Setting up Webpack 5 with React and Babel from scratch</title>
      <dc:creator>RiyaNegi</dc:creator>
      <pubDate>Sun, 21 Mar 2021 09:33:38 +0000</pubDate>
      <link>https://forem.com/riyanegi/setting-up-webpack-5-with-react-and-babel-from-scratch-2021-271l</link>
      <guid>https://forem.com/riyanegi/setting-up-webpack-5-with-react-and-babel-from-scratch-2021-271l</guid>
      <description>&lt;p&gt;Creating your own Webpack configurations sounds intimidating to even intermediate React developers. But the best way to tackle a problem is to face it head on! This blog will help you setup your own basic Webpack bundler with React and Babel for your next React project! It is also suitable for people who are trying to understand basics of Webpack, how Webpack is setup or exactly what happens under the hood. So let's dive into it!&lt;/p&gt;

&lt;h3&gt;
  
  
  TLDR
&lt;/h3&gt;

&lt;p&gt;For coders who want to straightaway take a look at the code.&lt;br&gt;
&lt;a href="https://github.com/RiyaNegi/react-webpack" rel="noopener noreferrer"&gt;Github repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 (Setting up folder and downloading dependencies)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Start with creating a folder and name it whatever you like. I named mine &lt;code&gt;react-webpack&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Go inside the file and initialize the package manager. &lt;code&gt;-y&lt;/code&gt; stands for “yes” to all general development questions asked on the command line.
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;npm init -y&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
This is what it will look like.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4413ji1g3rtmpxb2cxmc.jpg)

- Next we will install react dependencies. 
```bash


npm i react react-dom


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Your dependencies in the package.json file will now have following:
```jsx
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"dependencies": {&lt;br&gt;
   "react": "^17.0.1",&lt;br&gt;
   "react-dom": "^17.0.1"&lt;br&gt;
 }&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Next we will install dev dependencies and loaders
```bash


npm i -D @babel/core @babel/preset-env @babel/preset-react babel-loader file-loader css-loader style-loader webpack webpack-cli html-webpack-plugin


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;This is how the devDependencies will look like in &lt;code&gt;package.json&lt;/code&gt;:
```jsx
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"devDependencies": {&lt;br&gt;
   "&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/core": "^7.13.10",&lt;br&gt;
   "&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-env": "^7.13.10",&lt;br&gt;
   "&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-react": "^7.12.13",&lt;br&gt;
   "babel-loader": "^8.2.2",&lt;br&gt;
   "css-loader": "^5.1.3",&lt;br&gt;
   "file-loader": "^6.2.0",&lt;br&gt;
   "html-webpack-plugin": "^5.3.1",&lt;br&gt;
   "style-loader": "^2.0.0",&lt;br&gt;
   "webpack": "^5.27.0",&lt;br&gt;
   "webpack-cli": "^4.5.0",&lt;br&gt;
   "webpack-dev-server": "^3.11.2"&lt;br&gt;
 }&lt;/p&gt;



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

##Step 2 (Setting up Webpack with Babel)

- Next we will create a file called `.babelrc` which will transpile our react code from jsx to regular js. We need to include the following presets :
```jsx


{
   "presets": [
       "@babel/preset-env",
       "@babel/preset-react"
   ]
}


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

&lt;/div&gt;

&lt;p&gt;Our code and file structure by far looks like this.&lt;br&gt;
&lt;a href="https://media.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%2Fkedhc20586opnedr33gk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fkedhc20586opnedr33gk.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next we will create the webpack file. We will name it &lt;code&gt;webpack.config.js&lt;/code&gt;. This webpack folder essentially runs in the node environment and not the browser. Therefore we can write vanilla js code here.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how the file will look like:&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;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&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;HtmlWebpackPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Where files should be sent once they are bundled&lt;/span&gt;
 &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.bundle.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// webpack 5 comes with devServer which loads in development mode&lt;/span&gt;
 &lt;span class="na"&gt;devServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;watchContentBase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// Rules of how webpack will take our files, complie &amp;amp; bundle them for the browser &lt;/span&gt;
 &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;rules&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="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.(&lt;/span&gt;&lt;span class="sr"&gt;js|jsx&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/nodeModules/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="na"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel-loader&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="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;css$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;style-loader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;css-loader&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="p"&gt;},&lt;/span&gt;
 &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HtmlWebpackPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/index.html&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;h3&gt;
  
  
  Understanding webpack.config.js
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In &lt;code&gt;output&lt;/code&gt; we mention where the files should be sent once they are bundled.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;path&lt;/code&gt; mentions the name of the directory to be created where all the bundled files will be stored. We have named our folder &lt;code&gt;dist&lt;/code&gt;, which is a common practice.&lt;/li&gt;
&lt;li&gt;And &lt;code&gt;filename&lt;/code&gt; is the name we set for the new bundled file that will be created after webpack runs it's magic(basically bundles all the js code into one file).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;devServer&lt;/code&gt; is used to quickly develop an application, which is contrary to the production mode, which takes slighlty more time to build the application since it minifies the file, which doesn't happen in development mode.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With &lt;code&gt;port&lt;/code&gt; we can set a port number of our choice. I have set it to 3000. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;watchContentBase&lt;/code&gt; triggers a full page reload when any changes are made in any file.It is disabled by default.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;module&lt;/code&gt; is where you pass the rules for bundling files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt; is where we mention the extension of file which needs to be targetted by the specific loader.
All &lt;code&gt;.js&lt;/code&gt; or &lt;code&gt;.jsx&lt;/code&gt; files need to be bundled by the babel loader.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exclude&lt;/code&gt; is where we mention files that are needed to be ignored by the bundler.&lt;/li&gt;
&lt;li&gt;Same goes for the &lt;code&gt;css&lt;/code&gt; files.It is important to take in note that the array of &lt;code&gt;use :['style-loader', 'css-loader']&lt;/code&gt; needs to be written in that exact order.&lt;/li&gt;
&lt;li&gt;When webpack bundles the css files,this is the order it follows :

&lt;ul&gt;
&lt;li&gt;It first runs the &lt;code&gt;css-loader&lt;/code&gt; which turns css files to common js.&lt;/li&gt;
&lt;li&gt;Then it runs &lt;code&gt;style-loader&lt;/code&gt; which extracts css into files as string.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lastly we add a &lt;code&gt;plugin&lt;/code&gt; called HtmlWebpackPlugin which ensures that the webpack knows which html file template to follow for running the app.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step 3 (Setting up react folder)
&lt;/h2&gt;

&lt;p&gt;Okay so the complicated part is done!Let us build our react app files now!😄&lt;br&gt;
Create a &lt;code&gt;src&lt;/code&gt; folder and inside it create 4 files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;src&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;index.html&lt;br&gt;
index.js&lt;br&gt;
App.js&lt;br&gt;
App.css&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  index.html
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;&lt;span class="na"&gt;DOCTYPE&lt;/span&gt; &lt;span class="na"&gt;html&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;html&lt;/span&gt; &lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"en"&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;head&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;meta&lt;/span&gt; &lt;span class="na"&gt;charset&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&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;meta&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&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;title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;React Webpack App&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;title&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;head&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;body&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="p"&gt;&amp;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;script&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"index.bundle.js"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;script&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;body&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;html&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  index.js
&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDom&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-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&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;./App&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="s2"&gt;./App.css&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;render&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="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;app&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;h3&gt;
  
  
  App.js
&lt;/h3&gt;

&lt;p&gt;I am creating a very basic app for example but you can add in more stuff you like.&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;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;(&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome to React App&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;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Date : &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toDateString&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="nt"&gt;h3&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  App.css
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;teal&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  package.json
&lt;/h3&gt;

&lt;p&gt;Okay so now we have to create a script for running our app in &lt;code&gt;package.json&lt;/code&gt; file. Add the following scripts :&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;serve&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack serve --mode development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack --mode production&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;h3&gt;
  
  
  Step 4 (Running the app)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Now simply run it through the CLI using the run command and you can see your code running on the mentioned port. In my case it is running on port 3000. This how the bundle looks in development mode.
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;npm run serve&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0apvkk2jyd7eqoo3blb.jpg)

- You can then do build to see how the dist folder is build in production mode.
```bash


npm run build


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

&lt;/div&gt;

&lt;p&gt;-Once the dist folder is built, right click on the &lt;code&gt;index.html&lt;/code&gt; file in the dist folder and click on 'Open in live server' to check out how it will look in production mode. This is how the bundle looks in production mode. It is minfied.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fz6eqpb19h97cbr2m4cfs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fz6eqpb19h97cbr2m4cfs.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Optimizations!
&lt;/h2&gt;

&lt;p&gt;For optimizng the react web app, we will separate the JS/JSX files and CSS/SASS files.&lt;br&gt;
The reason why this is good practice is because in Webpack, loaders like &lt;code&gt;style-loader&lt;/code&gt; and &lt;code&gt;css-loader&lt;/code&gt; pre-process the stylesheets and embed them into the output JavaScript bundle, instead of the HTML file.&lt;br&gt;
And sometimes because of this there might be a flash of unstyled content (FOUC) where for a second you can see ugly plain HTML without any styling. To avoid this bad visual experience we need to split files and link CSS in the HTML file so that they load at the same time and there is no FOUC.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plugins like &lt;code&gt;MiniCssExtractPlugin&lt;/code&gt; can help in separating and minifying the CSS files and embedding them as a link into the HTML file, and thus avoiding FOUC. Install it with the following command:
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;npm install --save-dev mini-css-extract-plugin&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- Here's how we can add it. First `require` it in your webpack.config.js and replace the MiniCssExtractPlugin loader with `style-loader` and also add it in the plugin.

- It should look something like this (I have highlighted the 3 places where changes are required):

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m46j02h1gjmwdiyot948.jpg)

- Finally run build command again and in your `dist` folder you can see your new `main.css` file.
```bash


npm run build


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;This is the final look. I added a generic form element to show how we can build various components with their own CSS files and build a React web app similar to CRA.
&lt;img src="https://media.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%2Frcugg9l502k1so40n3rs.jpg" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Credits
&lt;/h3&gt;

&lt;p&gt;If you are a visual learner and want a video explanation, check out this amazing video by &lt;a href="https://twitter.com/codebubb" rel="noopener noreferrer"&gt;James Bubb&lt;/a&gt;. I followed his &lt;a href="https://www.youtube.com/watch?v=WDpxqopXd9U" rel="noopener noreferrer"&gt;Youtube Video&lt;/a&gt; to learn and build this react starter template.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webpack</category>
      <category>babel</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
