<?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: Ashu Deshwal</title>
    <description>The latest articles on DEV Community by Ashu Deshwal (@thestl).</description>
    <link>https://dev.to/thestl</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%2F560776%2Fe12e2e31-ceeb-44d9-9256-ee6e5b663c25.jpeg</url>
      <title>DEV Community: Ashu Deshwal</title>
      <link>https://dev.to/thestl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thestl"/>
    <language>en</language>
    <item>
      <title>React Fast Refresh in Django-react</title>
      <dc:creator>Ashu Deshwal</dc:creator>
      <pubDate>Thu, 04 Feb 2021 12:19:48 +0000</pubDate>
      <link>https://dev.to/thestl/react-fast-refresh-in-django-react-kn7</link>
      <guid>https://dev.to/thestl/react-fast-refresh-in-django-react-kn7</guid>
      <description>&lt;h2&gt;
  
  
  My Journey
&lt;/h2&gt;

&lt;p&gt;While trying to add hot module replacement in a Django-react app I found out that there are 2 ways of adding it.&lt;/p&gt;

&lt;p&gt;a) Using &lt;code&gt;django-webpack-loader&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Cons: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Required too many changes.&lt;/li&gt;
&lt;li&gt;It's not easy to add.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;b) Using &lt;code&gt;react-hot-loader&lt;/code&gt; + devServer:{ writeToDisk: true }&lt;/p&gt;

&lt;p&gt;Pros: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minumum code change required. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you are using --hotOnly then after sometimes react-hot-loader breaks then you have to reload the whole page to check your changes.&lt;/li&gt;
&lt;li&gt;If you are using --hot then instead of component re-render most of the time page reloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  React Fast Refresh
&lt;/h2&gt;

&lt;p&gt;Development experience with React Hot Loader was not good. That's why I tried to replace it with React Fast Refresh and it was a success. Create React App also use it now. &lt;br&gt;
Some key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it will re-render a React component when we edit a module that exports only React components&lt;/li&gt;
&lt;li&gt;it will reload all React components that import a non-React module and the module itself when we edit it&lt;/li&gt;
&lt;li&gt;it will do a full reload when we edit a module outside of the React tree&lt;/li&gt;
&lt;li&gt;it will continue working once we resolve a syntax or a runtime error without having to reload manually&lt;/li&gt;
&lt;li&gt;local state will be preserved for function components and Hooks out of the box&lt;/li&gt;
&lt;li&gt;local state won't be preserved for class components&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, install the react-refresh and @pmmmwh/react-refresh-webpack-plugin libraries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;react-refresh/babel&lt;/code&gt; in babel plugins.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-env", "@babel/preset-react"],
          plugins: ["react-refresh/babel"],
        },
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;HotModuleReplacementPlugin&lt;/code&gt; and &lt;code&gt;ReactRefreshWebpackPlugin&lt;/code&gt; in webpack plugins.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  new webpack.HotModuleReplacementPlugin(),
    new ReactRefreshWebpackPlugin({
      overlay: {
        sockPort: 8090,
      },
    }),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;4.Set webpack devServer config to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; devServer: {
    port: 8090,
    hotOnly: true,
    writeToDisk: true,
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
      "Access-Control-Allow-Headers":
        "X-Requested-With, content-type, Authorization",
    },
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Optional):&lt;br&gt;
Sometimes if webpack-dev-server skips files to write on disk, then set&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;publicPath: "/static/" 
```


to


``` 
publicPath: "/http://localhost:8090/" 
```


Source code is available [here](https://github.com/TheSTL/django-react-fast-refresh-boilerplate)

## Demo
![react-fast-refresh](https://user-images.githubusercontent.com/50075905/106893122-16d48c80-6713-11eb-94a7-372e9c9f0141.gif)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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