<?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: Suvankar Majumder</title>
    <description>The latest articles on DEV Community by Suvankar Majumder (@suvmaj).</description>
    <link>https://dev.to/suvmaj</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%2F269633%2Fca87448c-cf00-4e2b-ba03-d29b2b1148c0.jpg</url>
      <title>DEV Community: Suvankar Majumder</title>
      <link>https://dev.to/suvmaj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suvmaj"/>
    <language>en</language>
    <item>
      <title>Smarter state management with Redux Toolkit (RTK)</title>
      <dc:creator>Suvankar Majumder</dc:creator>
      <pubDate>Sun, 31 Jan 2021 05:34:17 +0000</pubDate>
      <link>https://dev.to/suvmaj/smarter-state-management-with-redux-toolkit-rtk-4jkd</link>
      <guid>https://dev.to/suvmaj/smarter-state-management-with-redux-toolkit-rtk-4jkd</guid>
      <description>&lt;p&gt;&lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt; is one of the most sought-after tools for state management in React and React Native applications.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;While looking for answers, the React team found a solution for the &lt;strong&gt;three common concerns of Redux&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Configuring a Redux store is too complicated"&lt;/li&gt;
&lt;li&gt;"I have to add a lot of packages to get Redux to do anything useful"&lt;/li&gt;
&lt;li&gt;"Redux requires too much boilerplate code"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enters &lt;a href="https://redux-toolkit.js.org/"&gt;Redux Toolkit&lt;/a&gt;...&lt;/p&gt;

&lt;p&gt;Which according to the documentation is,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The official, opinionated, batteries-included toolset for efficient Redux development&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;RTK not only removes a lot of complications in Redux but also offers enhancements giving a better development experience in the process.&lt;/p&gt;

&lt;p&gt;It uses Redux core for state management, &lt;a href="https://github.com/reduxjs/reselect"&gt;Reselect&lt;/a&gt; as a selector library, &lt;a href="https://github.com/immerjs/immer"&gt;Immer&lt;/a&gt; which gives the functionality to directly mutate the state and &lt;a href="https://github.com/reduxjs/redux-thunk"&gt;Redux Thunk&lt;/a&gt; for async tasks.&lt;/p&gt;

&lt;p&gt;In this post, we will set-up RTK for the good ol' Counter Application.&lt;/p&gt;

&lt;p&gt;To begin with we need to install the required dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-redux @reduxjs/toolkit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Store Configuration
&lt;/h3&gt;

&lt;p&gt;We will create our store in &lt;code&gt;store/index.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/store/index.js

import {configureStore, getDefaultMiddleware} from '@reduxjs/toolkit';

import reducer from './counterSlice';

const middleware = getDefaultMiddleware();
const store = configureStore({
  reducer,
  middleware,
});

export default store;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating the Slice
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;RTK provides us with a &lt;code&gt;createSlice&lt;/code&gt; function which takes in a single object with the name of the slice, initial state, and all of our reducers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/store/counterSlice.js

import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  counter: 0,
};

const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) =&amp;gt; {
      ++state.counter;
    },
    decrement: (state) =&amp;gt; {
      --state.counter;
    },
  },
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
We are exporting the &lt;code&gt;counterSlice.reducer&lt;/code&gt; object which is provided by the &lt;code&gt;createSlice&lt;/code&gt; method. This is the reducer that we imported and passed to &lt;code&gt;configureStore&lt;/code&gt; earlier in &lt;code&gt;store/index.js&lt;/code&gt;.&lt;br&gt;
RTK also automatically generates our actions for us, which are available in the &lt;code&gt;counterSlice.actions&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;That's all folks. With just two files and a few lines of code, we are able to bootstrap RTK in our application.&lt;/p&gt;

&lt;p&gt;Watch this space for more!!&lt;/p&gt;

</description>
      <category>redux</category>
      <category>javascript</category>
      <category>react</category>
      <category>reactnative</category>
    </item>
    <item>
      <title>Desktop application with Electron &amp; React (CRA)</title>
      <dc:creator>Suvankar Majumder</dc:creator>
      <pubDate>Sun, 24 May 2020 07:56:25 +0000</pubDate>
      <link>https://dev.to/suvmaj/desktop-application-with-electron-react-cra-3ooi</link>
      <guid>https://dev.to/suvmaj/desktop-application-with-electron-react-cra-3ooi</guid>
      <description>&lt;p&gt;When it comes to building cross-platform Desktop applications with Javascript, &lt;a href="https://electronjs.org"&gt;Electron&lt;/a&gt; is one of the most sought after choices of developers.&lt;/p&gt;

&lt;p&gt;In this post, we will learn to create a production-ready configuration of React with Electron.&lt;/p&gt;

&lt;p&gt;To begin with we need to create our project with create-react-app (CRA).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app electron-react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
We will also install &lt;code&gt;electron&lt;/code&gt; &amp;amp; &lt;code&gt;electron-builder&lt;/code&gt; (to package our application) as dev dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D electron electron-builder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to make a few changes in the &lt;code&gt;package.json&lt;/code&gt; file. &lt;br&gt;
First, we need to add the &lt;code&gt;main&lt;/code&gt; property to our package.json file, this will be the entry point to our electron application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"main": "electron/main.js",
"homepage": "./",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next will add a few scripts to start and package our electron application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"start": "export BROWSER=none &amp;amp;&amp;amp; react-scripts start",
"start-win": "set BROWSER=none &amp;amp;&amp;amp; react-scripts start",
"start-electron": "export ELECTRON_START_URL=http://localhost:3000 &amp;amp;&amp;amp; electron .",
"start-electron-win": "set ELECTRON_START_URL=http://localhost:3000 &amp;amp;&amp;amp; electron .",
"build": "react-scripts build",
"build-electron": "mkdir build/src &amp;amp;&amp;amp; cp -r electron/. build/electron",
"build-electron-win": "mkdir build\\src &amp;amp;&amp;amp; Xcopy /E /I /Y electron build\\electron",
"package": "npm run build &amp;amp;&amp;amp; npm run build-electron &amp;amp;&amp;amp; electron-builder build -c.extraMetadata.main=build/electron/main.js --publish never",
"package-win": "npm run build &amp;amp;&amp;amp; npm run build-electron-win &amp;amp;&amp;amp; electron-builder build -c.extraMetadata.main=build/electron/main.js --publish never",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: The scripts that end with &lt;code&gt;-win&lt;/code&gt; are for Windows platform, if you are on Windows you need to run these scripts.&lt;/p&gt;

&lt;p&gt;Finally, we will add the &lt;code&gt;build&lt;/code&gt; property which will be used by &lt;code&gt;electron-builder&lt;/code&gt; to package the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build": {
    "appId": "com.example.electron-react",
    "productName": "electron-react",
    "files": [
      "build/**/*",
      "node_modules/**/*"
    ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that all of our configurations are done we can finally proceed to write some code.&lt;br&gt;
Create a &lt;code&gt;main.js&lt;/code&gt; file in the &lt;code&gt;electron/&lt;/code&gt; folder and add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');

let mainWindow;

const createWindow = () =&amp;gt; {
  mainWindow = new BrowserWindow({ width: 600, height: 600, show: false });
  mainWindow.loadURL(
    !app.isPackaged
      ? process.env.ELECTRON_START_URL
      : url.format({
          pathname: path.join(__dirname, '../index.html'),
          protocol: 'file:',
          slashes: true,
        })
  );

  mainWindow.once('ready-to-show', () =&amp;gt; {
    mainWindow.show();
  });

  mainWindow.on('closed', () =&amp;gt; {
    mainWindow = null;
  });
};

app.on('ready', createWindow);

app.on('window-all-closed', () =&amp;gt; {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () =&amp;gt; {
  if (mainWindow === null) {
    createWindow();
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: app.isPackaged is a boolean flag which is true if the application is packaged and false otherwise, this behavior can be used to determine whether the app is running in production or development environment.&lt;/p&gt;

&lt;p&gt;Go ahead and run &lt;code&gt;npm start&lt;/code&gt; and &lt;code&gt;npm run start-electron&lt;/code&gt;.&lt;br&gt;
You should see something like this:&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%2Fi%2Fur5xm06e5km89ktelgb5.jpg" 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%2Fi%2Fur5xm06e5km89ktelgb5.jpg" alt="Electron Window" width="586" height="614"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!! Your first desktop application is up and running!!&lt;/p&gt;

&lt;p&gt;Now all you need to package your application is to run &lt;code&gt;npm run package&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Final notes&lt;/h2&gt;

&lt;p&gt;That's all folks. That is all you need to set up a basic desktop application using React and Electron. Check out the GitHub &lt;a href="https://github.com/iampikai/electron-react"&gt;repo&lt;/a&gt; for this tutorial.&lt;/p&gt;

&lt;p&gt;Watch this space for more!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
  </channel>
</rss>
