<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Suvankar Majumder's blog]]></title><description><![CDATA[Suvankar Majumder's blog]]></description><link>https://www.suvankar.xyz</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 00:21:59 GMT</lastBuildDate><atom:link href="https://www.suvankar.xyz/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Desktop application with Electron & React (CRA)]]></title><description><![CDATA[When it comes to building cross-platform Desktop applications with Javascript, Electron is one of the most reliable choices for developers.
In this post, we will learn to create a production-ready configuration of React with Electron.
To begin with w...]]></description><link>https://www.suvankar.xyz/desktop-application-with-electron-react-cra</link><guid isPermaLink="true">https://www.suvankar.xyz/desktop-application-with-electron-react-cra</guid><category><![CDATA[Electron]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Suvankar Majumder]]></dc:creator><pubDate>Sat, 17 Dec 2022 13:17:43 GMT</pubDate><content:encoded><![CDATA[<p>When it comes to building cross-platform Desktop applications with Javascript, <a target="_blank" href="https://electronjs.org">Electron</a> is one of the most reliable choices for developers.</p>
<p>In this post, we will learn to create a production-ready configuration of React with Electron.</p>
<p>To begin with we need to create our project with create-react-app (CRA).</p>
<pre><code class="lang-bash">npx create-react-app electron-react
</code></pre>
<p>The next thing we will do is to create a new folder named electron inside of electron-react. This is where we will put all of our electron code. We will also install <code>electron</code> &amp; <code>electron-builder</code> (to package our application) as dev dependencies.</p>
<pre><code class="lang-bash">npm i -D electron electron-builder
</code></pre>
<p>We need to make a few changes to the <code>package.json</code> file. First, we need to add the <code>main</code> property to our package.json file, this will be the entry point to our electron application.</p>
<pre><code class="lang-json"><span class="hljs-string">"main"</span>: <span class="hljs-string">"electron/main.js"</span>,
<span class="hljs-string">"homepage"</span>: <span class="hljs-string">"./"</span>,
</code></pre>
<p>Next will add a few scripts to start and package our electron application.</p>
<pre><code class="lang-json"><span class="hljs-string">"start"</span>: <span class="hljs-string">"export BROWSER=none &amp;&amp; react-scripts start"</span>,
<span class="hljs-string">"start-win"</span>: <span class="hljs-string">"set BROWSER=none &amp;&amp; react-scripts start"</span>,
<span class="hljs-string">"start-electron"</span>: <span class="hljs-string">"export ELECTRON_START_URL=http://localhost:3000 &amp;&amp; electron ."</span>,
<span class="hljs-string">"start-electron-win"</span>: <span class="hljs-string">"set ELECTRON_START_URL=http://localhost:3000 &amp;&amp; electron ."</span>,
<span class="hljs-string">"build"</span>: <span class="hljs-string">"react-scripts build"</span>,
<span class="hljs-string">"build-electron"</span>: <span class="hljs-string">"mkdir build/src &amp;&amp; cp -r electron/. build/electron"</span>,
<span class="hljs-string">"build-electron-win"</span>: <span class="hljs-string">"mkdir build\\src &amp;&amp; Xcopy /E /I /Y electron build\\electron"</span>,
<span class="hljs-string">"package"</span>: <span class="hljs-string">"npm run build &amp;&amp; npm run build-electron &amp;&amp; electron-builder build -c.extraMetadata.main=build/electron/main.js --publish never"</span>,
<span class="hljs-string">"package-win"</span>: <span class="hljs-string">"npm run build &amp;&amp; npm run build-electron-win &amp;&amp; electron-builder build -c.extraMetadata.main=build/electron/main.js --publish never"</span>,
</code></pre>
<p>Note: The scripts that end with <code>-win</code> are for the Windows platform, if you are on Windows you need to run these scripts.</p>
<p>Finally, we will add the <code>build</code> property which will be used by <code>electron-builder</code> to package the application. <code>"build": { "appId": "com.example.electron-react", "productName": "electron-react", "files": [ "build/**/*", "node_modules/**/*" ], }</code></p>
<p>Now that all of our configurations are done we can finally proceed to write some code. Create a <code>main.js</code> file in the <code>electron/</code> folder and add the following code.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { app, BrowserWindow } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'electron'</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'path'</span>);
<span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);

<span class="hljs-keyword">let</span> mainWindow;

<span class="hljs-keyword">const</span> createWindow = <span class="hljs-function">() =&gt;</span> {
  mainWindow = <span class="hljs-keyword">new</span> BrowserWindow({ <span class="hljs-attr">width</span>: <span class="hljs-number">600</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">600</span>, <span class="hljs-attr">show</span>: <span class="hljs-literal">false</span> });
  mainWindow.loadURL(
    !app.isPackaged
      ? process.env.ELECTRON_START_URL
      : url.format({
          <span class="hljs-attr">pathname</span>: path.join(__dirname, <span class="hljs-string">'../index.html'</span>),
          <span class="hljs-attr">protocol</span>: <span class="hljs-string">'file:'</span>,
          <span class="hljs-attr">slashes</span>: <span class="hljs-literal">true</span>,
        })
  );

  mainWindow.once(<span class="hljs-string">'ready-to-show'</span>, <span class="hljs-function">() =&gt;</span> {
    mainWindow.show();
  });

  mainWindow.on(<span class="hljs-string">'closed'</span>, <span class="hljs-function">() =&gt;</span> {
    mainWindow = <span class="hljs-literal">null</span>;
  });
};

app.on(<span class="hljs-string">'ready'</span>, createWindow);

app.on(<span class="hljs-string">'window-all-closed'</span>, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (process.platform !== <span class="hljs-string">'darwin'</span>) {
    app.quit();
  }
});

app.on(<span class="hljs-string">'activate'</span>, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (mainWindow === <span class="hljs-literal">null</span>) {
    createWindow();
  }
});
</code></pre>
<p>Note: <code>app.isPackaged</code> is a boolean flag that is true if the application is packaged and false otherwise, this behavior can be used to determine whether the app is running in a production or development environment.</p>
<p>Go ahead and run <code>npm start</code> and <code>npm run start-electron</code>. You should see something like this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/ur5xm06e5km89ktelgb5.jpg" alt="Electron Window" /></p>
<p>Congratulations!! Your first desktop application is up and running!!</p>
<p>Now all you need to package your application is to run <code>npm run package</code>.</p>
<h2 id="heading-final-notes">Final notes</h2>
<p>Watch this space for more!!</p>
]]></content:encoded></item><item><title><![CDATA[Smarter state management with Redux Toolkit (RTK)]]></title><description><![CDATA[Redux is one of the most sought-after tools for state management in React and React Native applications.
But one of the major issues developers especially beginners face is the amount of boilerplate and unnecessary code that is required to set up Red...]]></description><link>https://www.suvankar.xyz/smarter-state-management-with-redux-toolkit-rtk</link><guid isPermaLink="true">https://www.suvankar.xyz/smarter-state-management-with-redux-toolkit-rtk</guid><category><![CDATA[redux-toolkit]]></category><category><![CDATA[Redux]]></category><category><![CDATA[React]]></category><category><![CDATA[React Native]]></category><dc:creator><![CDATA[Suvankar Majumder]]></dc:creator><pubDate>Sat, 17 Dec 2022 13:11:12 GMT</pubDate><content:encoded><![CDATA[<p><a target="_blank" href="https://redux.js.org/">Redux</a> is one of the most sought-after tools for state management in React and React Native applications.</p>
<p>But one of the major issues developers especially beginners face is the amount of boilerplate and unnecessary code that is required to set up Redux in a project.</p>
<p>While looking for answers, the React team found a solution for the <strong>three common concerns of Redux</strong></p>
<ul>
<li><p>"Configuring a Redux store is too complicated"</p>
</li>
<li><p>"I have to add a lot of packages to get Redux to do anything useful"</p>
</li>
<li><p>"Redux requires too much boilerplate code"</p>
</li>
</ul>
<p>Enters <a target="_blank" href="https://redux-toolkit.js.org/">Redux Toolkit</a>...</p>
<p>Which according to the documentation is,</p>
<blockquote>
<p>The official, opinionated, batteries-included toolset for efficient Redux development</p>
</blockquote>
<p>RTK not only removes a lot of complications in Redux but also offers enhancements giving a better development experience in the process.</p>
<p>It uses Redux core for state management, <a target="_blank" href="https://github.com/reduxjs/reselect">Reselect</a> as a selector library, <a target="_blank" href="https://github.com/immerjs/immer">Immer</a> which gives the functionality to directly mutate the state, and <a target="_blank" href="https://github.com/reduxjs/redux-thunk">Redux Thunk</a> for async tasks.</p>
<p>In this post, we will set up RTK for the good ol' Counter Application.</p>
<p>To begin with we need to install the required dependencies. <code>npm install react-redux @reduxjs/toolkit</code></p>
<h3 id="heading-store-configuration">Store Configuration</h3>
<p>We will create our store in <code>store/index.js</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// src/store/index.js</span>

<span class="hljs-keyword">import</span> {configureStore, getDefaultMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">'@reduxjs/toolkit'</span>;

<span class="hljs-keyword">import</span> reducer <span class="hljs-keyword">from</span> <span class="hljs-string">'./counterSlice'</span>;

<span class="hljs-keyword">const</span> middleware = getDefaultMiddleware();
<span class="hljs-keyword">const</span> store = configureStore({
  reducer,
  middleware,
});

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<h3 id="heading-creating-the-slice">Creating the Slice</h3>
<p>Gone are the days of creating separate files for Reducers, Actions, Action Creators, and those lengthy switch cases to handle these Actions in our reducers.</p>
<p>RTK provides us with a <code>createSlice</code> function that takes in a single object with the name of the slice, initial state, and all of our reducers.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// src/store/counterSlice.js</span>

<span class="hljs-keyword">import</span> { createSlice } <span class="hljs-keyword">from</span> <span class="hljs-string">'@reduxjs/toolkit'</span>;

<span class="hljs-keyword">const</span> initialState = {
  <span class="hljs-attr">counter</span>: <span class="hljs-number">0</span>,
};

<span class="hljs-keyword">const</span> counterSlice = createSlice({
  <span class="hljs-attr">name</span>: <span class="hljs-string">'counter'</span>,
  initialState,
  <span class="hljs-attr">reducers</span>: {
    <span class="hljs-attr">increment</span>: <span class="hljs-function">(<span class="hljs-params">state</span>) =&gt;</span> {
      ++state.counter;
    },
    <span class="hljs-attr">decrement</span>: <span class="hljs-function">(<span class="hljs-params">state</span>) =&gt;</span> {
      --state.counter;
    },
  },
});

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> { increment, decrement } = counterSlice.actions;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> counterSlice.reducer;
</code></pre>
<p>As you can see we are mutating the state directly instead of returning a new object, RTK uses Immer under the hood to handle immutability in the store. We are exporting the <code>counterSlice.reducer</code> object which is provided by the <code>createSlice</code> method. This is the reducer that we imported and passed to <code>configureStore</code> earlier in <code>store/index.js</code>. RTK also automatically generates our actions for us, which are available in the <code>counterSlice.actions</code> object.</p>
<p>That's all folks. With just two files and a few lines of code, we are able to bootstrap RTK in our application.</p>
<p>Watch this space for more!!</p>
]]></content:encoded></item></channel></rss>