<?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: Daniil Polovinkin</title>
    <description>The latest articles on DEV Community by Daniil Polovinkin (@xxdondi).</description>
    <link>https://dev.to/xxdondi</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%2F579545%2F3892e9f8-b605-480c-a4c4-13bbfa01b585.jpeg</url>
      <title>DEV Community: Daniil Polovinkin</title>
      <link>https://dev.to/xxdondi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xxdondi"/>
    <language>en</language>
    <item>
      <title>LeetCode 46. Permutations: Solution Explained</title>
      <dc:creator>Daniil Polovinkin</dc:creator>
      <pubDate>Sat, 06 May 2023 20:10:29 +0000</pubDate>
      <link>https://dev.to/xxdondi/learn-how-to-solve-leetcode-46-permutations-3kgj</link>
      <guid>https://dev.to/xxdondi/learn-how-to-solve-leetcode-46-permutations-3kgj</guid>
      <description>&lt;p&gt;Let's dive into one of the LeetCode problems which is a great example to learn recursive/backtracking approach to solving algorithmic problems. Also we will beat 94% of JS solutions on the site 😉 (at the time of writing).&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%2Fui8vi4yn2tkcp7yzd2tb.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%2Fui8vi4yn2tkcp7yzd2tb.png" alt="Solution runtime performance - beats 94% of the submitted JS solutions on LeetCode" width="681" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Description
&lt;/h2&gt;

&lt;p&gt;Courtesy of LeetCode:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given an array &lt;code&gt;nums&lt;/code&gt; of distinct integers, return all the possible permutations. You can return the answer in any order.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our constraints are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 6&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10 &amp;lt;= nums[i] &amp;lt;= 10&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;All the integers of nums are unique.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recursive approach
&lt;/h2&gt;

&lt;p&gt;According to LeetCode itself, the problem is suggested to be solved with recursion/backtracking. For recursion-based solution, we have to figure out what is the &lt;em&gt;base case&lt;/em&gt; for recursive calls and what is the &lt;em&gt;recursive case&lt;/em&gt;. In order for the algorithm to work, we need our recursive case to get to the base case eventually by decreasing the problem size.&lt;/p&gt;

&lt;p&gt;Can we apply this approach here?&lt;/p&gt;

&lt;p&gt;In this problem all we have is a &lt;code&gt;nums&lt;/code&gt; array, so probably we need to shrink the array size, until we hit the &lt;em&gt;base case&lt;/em&gt;, and then work our way up, while keeping in mind the fact that we shrunk the array size, we do not want to lose any data. This is where the backtracking part fits in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining the base case
&lt;/h2&gt;

&lt;p&gt;Now, the base case is definitely an array of size 1. Why? Permutations of the &lt;code&gt;[3]&lt;/code&gt; array is just &lt;code&gt;[[3]]&lt;/code&gt;, we know that for certain, there is no need to do any extra operations here. How do we reduce our initial problem to the base case? We could simply go through each element of the array, removing the first element of the array. If we do that, the recursive case should work it's way to the base case. Then we need to handle the results returned by the base case in the recursive case and return the new result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining the recursive case
&lt;/h2&gt;

&lt;p&gt;For example, let us take the array of size 2: &lt;code&gt;nums = [1, 2]&lt;/code&gt;. Is there a way to reduce this input to our base case?&lt;/p&gt;

&lt;p&gt;If we remove the first element, we get just &lt;code&gt;[2]&lt;/code&gt;, this is our base case. In order to complete the permutations array, we should add the missing &lt;code&gt;1&lt;/code&gt; to the permutations returned by the &lt;em&gt;base case&lt;/em&gt;, which will get us &lt;code&gt;[[2, 1]]&lt;/code&gt;. Then we add the missing &lt;code&gt;1&lt;/code&gt; &lt;strong&gt;back&lt;/strong&gt; to the nums array, we get &lt;code&gt;nums = [2, 1]&lt;/code&gt;. We remove &lt;code&gt;2&lt;/code&gt;, since this is our first element now and get just &lt;code&gt;[1]&lt;/code&gt;. This is, again, the base case. Then we add the &lt;code&gt;2&lt;/code&gt; to the end and get &lt;code&gt;[1, 2]&lt;/code&gt;. We merge these results and get &lt;code&gt;[[2, 1], [1, 2]]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If it works for the array of size 2, it should work for &lt;em&gt;an array of any size&lt;/em&gt;.&lt;/p&gt;

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

&lt;p&gt;Our theoretical musings lead us to writing something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;permute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="c1"&gt;// - Base case&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;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="c1"&gt;// If there's just 1 element, we simply return a copy&lt;/span&gt;
        &lt;span class="c1"&gt;// of that array&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[[...&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// - Recursive case&lt;/span&gt;
    &lt;span class="c1"&gt;// We move through each number in `nums`&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="c1"&gt;// Remove the first element of `nums`&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// Now we have reduced the `nums` size to &lt;/span&gt;
        &lt;span class="c1"&gt;// `nums.length - 1`, this will get us to the&lt;/span&gt;
        &lt;span class="c1"&gt;// base case eventually&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;perms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;permute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Traverse through each of the permutations&lt;/span&gt;
        &lt;span class="c1"&gt;// returned by a recursive call&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;perm&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;perms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// We add the number that we shifted earlier&lt;/span&gt;
            &lt;span class="c1"&gt;// to reduce the problem size to the back of&lt;/span&gt;
            &lt;span class="c1"&gt;// the permutation array&lt;/span&gt;
            &lt;span class="nx"&gt;perm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;// We add this permutation to the result array&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;perm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// Finally, we add the number we shifted back&lt;/span&gt;
        &lt;span class="c1"&gt;// to the array&lt;/span&gt;
        &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Return the result&lt;/span&gt;
    &lt;span class="k"&gt;return&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;This is just enough to solve the problem! Great job on getting to this point in the post.&lt;/p&gt;

&lt;p&gt;But we could go the extra mile here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further runtime improvement
&lt;/h2&gt;

&lt;p&gt;What if we make our base case an array of size 2? Figuring out what the permutations of a 2-sized array is pretty straightforward. For example, permutations of &lt;code&gt;[1, 2]&lt;/code&gt; are &lt;code&gt;[[1, 2], [2, 1]]&lt;/code&gt;. So if we change our base case to an array of size 2, we could get rid of a few extra recursive calls, and save execution time. Recursive case will do its work as is, no changes needed there.&lt;/p&gt;

&lt;p&gt;New base case could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// - Base case&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;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;nums&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;nums&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;This should get you to the promised 94%+ runtime result. Enjoy your place on the leaderboard 🥳 .&lt;/p&gt;

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

&lt;p&gt;LeetCode 46 - Permutations - problem is a great example to learn recursive/backtracking approach. We briefly went through the theoretical basis and applied the same logic to this problem.&lt;/p&gt;

&lt;p&gt;Of course, all this is just scratching the surface of the exciting world of algorithm design. Want more depth? Consider reading these books:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/Grokking-Algorithms-illustrated-programmers-curious/dp/1617292230?&amp;amp;_encoding=UTF8&amp;amp;tag=devtoblog-20&amp;amp;linkCode=ur2&amp;amp;linkId=4d937610eff664c3a2d539fa623f2d35&amp;amp;camp=1789&amp;amp;creative=9325" rel="noopener noreferrer"&gt;Grokking Algorithms: Aditya Bhargava&lt;/a&gt; - great and simple to understand introduction to algorithms;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/Algorithm-Design-Manual-Steven-Skiena/dp/1849967202?&amp;amp;_encoding=UTF8&amp;amp;tag=devtoblog-20&amp;amp;linkCode=ur2&amp;amp;linkId=8826c13f2f52be0a1b307471c21565fb&amp;amp;camp=1789&amp;amp;creative=9325" rel="noopener noreferrer"&gt;The Algorithm Design Manual: Skiena, Steven S S.&lt;/a&gt; - amazing in-depth guide to algorithm design, it has answers to all your questions on this topic.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>leetcode</category>
      <category>tutorial</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Speeding up your development with Webpack 5 HMR and React Fast Refresh</title>
      <dc:creator>Daniil Polovinkin</dc:creator>
      <pubDate>Thu, 18 Feb 2021 12:15:11 +0000</pubDate>
      <link>https://dev.to/xxdondi/speeding-up-your-development-with-webpack-5-hmr-and-react-fast-refresh-of8</link>
      <guid>https://dev.to/xxdondi/speeding-up-your-development-with-webpack-5-hmr-and-react-fast-refresh-of8</guid>
      <description>&lt;h4&gt;
  
  
  Part One: Phonecalls
&lt;/h4&gt;

&lt;p&gt;Recently I have received a phone call on my answering machine from my manager: I was assigned to work on a legacy project. It was not a very complex task, but still rather time-consuming. I started working on it with a "quick in-and-out" attitude, not intending to do any serious changes to the project. However, as time went on, I realized that I am spending a lot of time repeating the actions required to get to the UI component I was working on after the page refreshes after a code change. Every project I usually work on has at least HMR setup, but here I was faced with a reload after the slightest CSS change. So I decided to dig in and get HMR and React fast-refresh up and running in order to speed up the process.&lt;/p&gt;

&lt;h4&gt;
  
  
  Part Two: Questions
&lt;/h4&gt;

&lt;p&gt;What even is HMR and React Fast Refresh? &lt;/p&gt;

&lt;p&gt;Let's start with the first one - HMR or Hot Module Replacement. This is a &lt;a href="https://webpack.js.org/concepts/hot-module-replacement/" rel="noopener noreferrer"&gt;feature of &lt;code&gt;webpack&lt;/code&gt;&lt;/a&gt; which has been around for quite some time now, it is enabled by default in a &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;popular bootstrapping package &lt;code&gt;create-react-app&lt;/code&gt;&lt;/a&gt;. It enables your app to swap modules while it is running (the "Hot" of "HMR"), without a full page reload and losing the app's state. However, it is hard to retain the state of a module when it is something complex, like a stateful React component. That is why a group of wonderful people developed React Fast Refresh.&lt;/p&gt;

&lt;p&gt;React Fast Refresh is a younger cousin of another similar feature - Hot Reloading, but it is officially supported by React and is stated to be more reliable on its &lt;a href="https://www.npmjs.com/package/react-refresh" rel="noopener noreferrer"&gt;README page&lt;/a&gt;. Now, is it possible that someone blatantly lied in the README file? I know I did that a few times, but from my experience, this is not the case with &lt;code&gt;react-refresh&lt;/code&gt;. It handles even very complex component changes very well. Once again, projects set up with &lt;code&gt;create-react-app@^4.0.0&lt;/code&gt; have it enabled by default.&lt;/p&gt;

&lt;h4&gt;
  
  
  Part Three: Connections
&lt;/h4&gt;

&lt;p&gt;The project that I was assigned to work had an outdated &lt;code&gt;webpack&lt;/code&gt; and &lt;code&gt;react&lt;/code&gt; version so I went ahead and updated &lt;code&gt;react&lt;/code&gt; to &lt;code&gt;^17.0.0&lt;/code&gt; and &lt;code&gt;webpack&lt;/code&gt; with &lt;code&gt;webpack-dev-server&lt;/code&gt; to &lt;code&gt;^5.0.0&lt;/code&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  Disclaimer
&lt;/h4&gt;

&lt;p&gt;While working on legacy projects you have to be aware of the risks that you are taking when making large changes to the codebase (like updating the bundler and core framework to the next major version). If you do not have automated tests set up or do not have enough QA resources to thoroughly test the software after such an update, I strongly urge you to consider other options if it is possible. &lt;/p&gt;




&lt;p&gt;After fixing a number of dependencies issues and seeing a green light on my CI dashboard, I proceeded to set up HMR and React Fast Refresh.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up HMR
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Packages used
&lt;/h5&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;webpack&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;^5.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;react&lt;/code&gt;, &lt;code&gt;react-dom&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;^17.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This one could be as simple as slightly editing your &lt;code&gt;devServer&lt;/code&gt; section of &lt;code&gt;webpack.config.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="nx"&gt;devServer&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;hot&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line tells your &lt;code&gt;webpack-dev-server&lt;/code&gt; to enable HMR. The last step is enabling &lt;code&gt;webpack.HotModuleReplacementPlugin&lt;/code&gt;. You could do that in the config file manually, but I suggest you to take a safer route and add &lt;code&gt;--hot&lt;/code&gt; to your &lt;code&gt;package.json&lt;/code&gt; &lt;code&gt;start&lt;/code&gt; (or whatever name you prefer to run your project in development mode) script to make sure that the plugin is used only in the development environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&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="p"&gt;...&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&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 --hot&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should do it in most cases and you are set up with flawless reloading of CSS and other assets without any extra work. However, editing a React component will probably result in a full page refresh, and all the app state is still lost.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up React Fast Refresh
&lt;/h4&gt;




&lt;h4&gt;
  
  
  Disclaimer
&lt;/h4&gt;

&lt;p&gt;This part uses an &lt;strong&gt;experimental&lt;/strong&gt; webpack plugin that might not handle unknown edge cases. From my personal experience, I have had no issues with it at the time of writing. Still, proceed with caution.&lt;/p&gt;




&lt;h5&gt;
  
  
  Packages used
&lt;/h5&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;webpack&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;^5.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;react&lt;/code&gt;, &lt;code&gt;react-dom&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;^17.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;babel-loader&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;^8.2.2&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;react-refresh&lt;/code&gt; npm package that I mentioned earlier is intended to be used by bundler authors. If you want to enable React Fast Refresh in your project, you should check out &lt;a href="https://github.com/pmmmwh/react-refresh-webpack-plugin" rel="noopener noreferrer"&gt;the &lt;code&gt;react-fast-refresh-webpack-plugin&lt;/code&gt;&lt;/a&gt;. There is an extensive installation and setup guide, but I will go through these steps here as well. &lt;/p&gt;

&lt;p&gt;Its setup is a bit more complex than HMR, but should not be a big problem anyway. First of all, you need to install all the required dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; @pmmmwh/react-refresh-webpack-plugin react-refresh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 2 parts of enabling this feature:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Adding the &lt;code&gt;react-refresh/babel&lt;/code&gt; to &lt;code&gt;babel-loader&lt;/code&gt; plugins.&lt;/li&gt;
&lt;li&gt;Adding the &lt;code&gt;react-refresh-webpack-plugin&lt;/code&gt; to &lt;code&gt;webpack&lt;/code&gt; plugins.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Same as with HMR, enabling React Fast Refresh on production is a huge vulnerability, we need to make sure it is enabled only in the development environment. &lt;code&gt;webpack@^5.0.0&lt;/code&gt; strongly suggests using a &lt;code&gt;--mode&lt;/code&gt; parameter, so we could use its value as the source of truth to enable the plugins when necessary. To get the &lt;code&gt;--mode&lt;/code&gt; parameter value we need our &lt;code&gt;webpack&lt;/code&gt; config file to export a function, so you can just wrap your existing config in an arrow function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// the first parameter in a function&lt;/span&gt;
&lt;span class="c1"&gt;// webpack config is "env" [1]&lt;/span&gt;
&lt;span class="c1"&gt;// which is not used in this example&lt;/span&gt;
&lt;span class="c1"&gt;// so its name is set to "_" to indicate&lt;/span&gt;
&lt;span class="c1"&gt;// that a parameter is being passed, &lt;/span&gt;
&lt;span class="c1"&gt;// but we do not use it&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="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argv&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;mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mode&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;isDevelopment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="c1"&gt;// your existing webpack configuration&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;&lt;sup&gt;[1] - More information about the &lt;code&gt;env&lt;/code&gt; parameter available in &lt;a href="https://webpack.js.org/guides/environment-variables/" rel="noopener noreferrer"&gt;webpack docs&lt;/a&gt;.&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the handy &lt;code&gt;isDevelopment&lt;/code&gt; constant, we can edit the rule for loading JS files to conditionally include &lt;code&gt;react-refresh/babel&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="nx"&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$/&lt;/span&gt;&lt;span class="p"&gt;,&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="s2"&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="na"&gt;options&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="c1"&gt;// this code will evaluate to "false" when &lt;/span&gt;
          &lt;span class="c1"&gt;// "isDevelopment" is "false"&lt;/span&gt;
          &lt;span class="c1"&gt;// otherwise it will return the plugin&lt;/span&gt;
          &lt;span class="nx"&gt;isDevelopment&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&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="s2"&gt;react-refresh/babel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// this line removes falsy values from the array&lt;/span&gt;
        &lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&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="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then onto the webpack's &lt;code&gt;plugins&lt;/code&gt; section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="nx"&gt;plugins&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;isDevelopment&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReactRefreshWebpackPlugin&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="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&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 that we have both parts set up, you should have React Fast Refresh in your project in development mode.&lt;/p&gt;

&lt;h4&gt;
  
  
  Part Four: Answers
&lt;/h4&gt;

&lt;p&gt;Now, whenever I start the project with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;webpack serve &lt;span class="nt"&gt;--hot&lt;/span&gt; &lt;span class="nt"&gt;--mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;development
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I enjoy the development process with as few page reloads as possible, so I can instantly see the changes I made in the code immediately take effect in the app. This made the time-consuming task I was assigned with way less time-consuming and a bit more fun.&lt;/p&gt;

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