<?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: Stas Fedyna</title>
    <description>The latest articles on Forem by Stas Fedyna (@stanislavfedyna).</description>
    <link>https://forem.com/stanislavfedyna</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%2F475081%2F19ef6c04-f93b-4c62-aab1-2bc2b86c411b.jpeg</url>
      <title>Forem: Stas Fedyna</title>
      <link>https://forem.com/stanislavfedyna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stanislavfedyna"/>
    <language>en</language>
    <item>
      <title>Redux Toolkit and Typescript (the best way to manage application state)</title>
      <dc:creator>Stas Fedyna</dc:creator>
      <pubDate>Mon, 22 Mar 2021 07:36:02 +0000</pubDate>
      <link>https://forem.com/stanislavfedyna/redux-toolkit-and-typescript-the-best-way-to-manage-application-state-4i97</link>
      <guid>https://forem.com/stanislavfedyna/redux-toolkit-and-typescript-the-best-way-to-manage-application-state-4i97</guid>
      <description>&lt;p&gt;Today we will talk about a new and in my opinion a phenomenal functionality for working with Redux, namely - &lt;strong&gt;Redux Toolkit&lt;/strong&gt; (I really hope you understand the concept of how Redux works before you start reading this article as otherwise, some terminology may not be clear for you).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://redux-toolkit.js.org/" rel="noopener noreferrer"&gt;Redux Toolkit&lt;/a&gt; is an updated vision of Redux developers on how to manipulate data globally and do it easily and effortlessly.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's take a step back and recall what problems the classical approach has 🤔&lt;/p&gt;

&lt;p&gt;If don't go into detail and statistical comparison, Redux isn't liked by developers (especially beginners) because of its complexity.&lt;/p&gt;

&lt;p&gt;First, you need to spend a lot of time understanding the ideology of Redux, then spend time creating basic things (actions, action creators, reducers and etc.). 🤯&lt;/p&gt;

&lt;p&gt;But we are developers - we want to write code, not waste time on settings. Right? 👩‍💻&lt;/p&gt;

&lt;p&gt;Below I will describe &lt;strong&gt;my vision&lt;/strong&gt; of how you can work with Redux Toolkit and use it with TypeScript (since I adore TypeScript and sincerely recommend using it in your projects).&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation ⌛
&lt;/h2&gt;

&lt;p&gt;I want to start by using the standard &lt;strong&gt;create-react-app&lt;/strong&gt; structure with TypeScript.&lt;/p&gt;

&lt;p&gt;This can be done with the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# npm
npx create-react-app my-app --template typescript

# yarn
yarn create react-app my-app --template typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, let's add the &lt;strong&gt;toolkit&lt;/strong&gt; module directly to our project:&lt;br&gt;
&lt;/p&gt;

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

# yarn
yarn add @reduxjs/toolkit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And directly the &lt;strong&gt;react-redux&lt;/strong&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# npm
npm install react-redux

# yarn
yarn add react-redux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deep dive into Redux Toolkit 🤿
&lt;/h2&gt;

&lt;p&gt;The new and most important concept that we will immediately encounter will be - &lt;strong&gt;slice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin within the &lt;strong&gt;src&lt;/strong&gt; folder I will create one more folder with the name - &lt;strong&gt;slices.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside, I'll create a &lt;strong&gt;profile.ts&lt;/strong&gt; file that will contain functionality that pertains to the user's profile (it's a simple division by type of work, nothing special).&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%2Flhzsayhad0wcmr2p39xp.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%2Flhzsayhad0wcmr2p39xp.png" alt="0_LzfvlubsmSG3dh7W"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(React is flexible in terms of architecture, so you can choose a different files location. My choice is based on the ability to conveniently use the absolute paths provided by default when using react with TypeScript. You just need to find tsconfig.json file, find "compilerOptions" object and add another field - "baseUrl": "src")&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We go to the profile.ts &lt;strong&gt;profile.ts file&lt;/strong&gt;. In order to create a slice directly, you need to import the function that is responsible for it, namely - &lt;strong&gt;createSlice&lt;/strong&gt;. Together with it, we import the module - &lt;strong&gt;PayloadAction&lt;/strong&gt; (about which I will tell later).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice, PayloadAction } from '@reduxjs/toolkit';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The createSlice function accepts an object with main fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;name&lt;/strong&gt; - describe the type of actions inside (for example, actions on user data or actions on authentication data);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;the initial value for our redux state&lt;/strong&gt; (any data type, mainly array, or object);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;reducers&lt;/strong&gt; (an object that will contain methods that will implement changes in our redux state).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const profileSlice = createSlice({
  name: 'profile',
  initialState: initialState,
  reducers: {},
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything is clear with the &lt;strong&gt;name&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The next field is &lt;strong&gt;initialState&lt;/strong&gt;. Let's create it above the implementation of the &lt;strong&gt;createSlice&lt;/strong&gt; function (&lt;strong&gt;initialState&lt;/strong&gt; can be made into a separate file depending on your wishes and the size of this very &lt;strong&gt;initialState&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;It will look like a simple object with one field - &lt;strong&gt;email&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialState = {
  email: '',
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last field - &lt;strong&gt;reducers&lt;/strong&gt;. Inside we describe the methods that will implement the change of our redux state.&lt;/p&gt;

&lt;p&gt;Let's implement the &lt;strong&gt;changeEmail&lt;/strong&gt; method (which represents just action creator).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const profileSlice = createSlice({
  name: 'profile',
  initialState: initialState,
  reducers: {
    changeEmail: (state, { payload }: PayloadAction&amp;lt;TPayload&amp;gt;) =&amp;gt; {
      state.email = payload.email;
    },
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's deal with everything that is described above and what is used here.&lt;/p&gt;

&lt;p&gt;The first thing I want to note is that the &lt;strong&gt;changeEmail&lt;/strong&gt; method takes two parameters &lt;strong&gt;(identity how a reducer does in a classic Redux structure).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The first parameter - state&lt;/strong&gt;, which will contain the initial value and will change after applying some of our methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The second parameter is the object that represents the action&lt;/strong&gt; (in the classic use of this object contains the type field and the payload field), but in our, we use only the payload, which is obtained by destructuring (ES6).&lt;/p&gt;

&lt;p&gt;We add type for our payload using &lt;strong&gt;PayloadAction&lt;/strong&gt; (which we imported). &lt;br&gt;
&lt;strong&gt;PayloadAction&lt;/strong&gt; takes the type created above implementation of function &lt;strong&gt;createSlice&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type TPayload = {
  email: string;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's look directly inside in our &lt;strong&gt;changeEmail&lt;/strong&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;changeEmail: (state, { payload }: PayloadAction&amp;lt;TPayload&amp;gt;) =&amp;gt; {
  state.email = payload.email;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing we can see is a somewhat unconventional approach to change the Redux state. We simply assign new values ​​to &lt;strong&gt;state.email&lt;/strong&gt; with the values ​​we get with the payload.&lt;/p&gt;

&lt;p&gt;And what about the rule of &lt;strong&gt;immutability&lt;/strong&gt;, which we must always remember when we change the Redux state? (&lt;em&gt;If you don't fully understand what I'm talking about, then I'll make a very quick explanation of what I mean&lt;/em&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The main concept of immutability when changing the Redux state is to always make a copy of the current Redux state (mostly using the spread operator with ES6 for objects and arrays) and then change the copy and return it as a new state of our Redux state. This is so that React can recognize the changes (since the references have changed) and redraw our component that uses data from the Redux state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this regard, the Redux Toolkit "under the hood" uses an additional module called &lt;strong&gt;&lt;a href="https://github.com/immerjs/immer" rel="noopener noreferrer"&gt;immer&lt;/a&gt;&lt;/strong&gt;. This module takes on the job of copying and granting just these rules of immutability. Therefore, we can not worry about it and change the fields directly.&lt;/p&gt;

&lt;p&gt;Let's summarize what happened. &lt;strong&gt;We just took the old value from the Redux store and replaced it with a new one&lt;/strong&gt;, which we passed to one of our components (will see later).&lt;/p&gt;

&lt;p&gt;That's it, our first slice is ready, with which I congratulate you! 🎉.&lt;/p&gt;

&lt;p&gt;After implementing &lt;strong&gt;createSlice&lt;/strong&gt;, let's do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const profileReducer = profileSlice.reducer;
export const { changeEmail } = profileSlice.actions;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The first export&lt;/strong&gt; is our reducer, which we will call &lt;strong&gt;profileReducer&lt;/strong&gt; (we will need it soon).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The second export&lt;/strong&gt; is just our created &lt;strong&gt;changeEmail&lt;/strong&gt; method (our action creator).&lt;/p&gt;

&lt;p&gt;All this is available to us by referring to our created slice - &lt;strong&gt;profileSlice&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux store configuration 🔧
&lt;/h2&gt;

&lt;p&gt;Let's use our &lt;strong&gt;profileReducer&lt;/strong&gt; (the first export from the &lt;strong&gt;profile.ts&lt;/strong&gt; file) and work on a full-fledged redux connection.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;src&lt;/strong&gt; folder, create another folder - &lt;strong&gt;reducer&lt;/strong&gt;. Inside create a file &lt;strong&gt;reducer.ts&lt;/strong&gt;:&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%2Fysuyg0zx6ci9aehnh050.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%2Fysuyg0zx6ci9aehnh050.png" alt="0_uPjWKkj9W3YRsue9"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reducer.ts&lt;/strong&gt; will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { profileReducer } from "slices/profile";

const reducer = {
  profileReducer,
  // another reducers (if we have)
};
export default reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import our created reducer so that in the future we can add another and combine them together.&lt;/p&gt;

&lt;p&gt;I will note one more interesting thing - the reducer object in which we can add other reducers is analogous to the use of the &lt;strong&gt;&lt;a href="https://redux.js.org/api/combinereducers" rel="noopener noreferrer"&gt;combineReducers&lt;/a&gt;&lt;/strong&gt; function but without the additional import of this &lt;strong&gt;combineReducers&lt;/strong&gt; function and the generally compact appearance of the whole combination structure.&lt;/p&gt;

&lt;p&gt;Let's create another file - &lt;strong&gt;store.ts&lt;/strong&gt; in the &lt;strong&gt;src&lt;/strong&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit";
import reducer from "./reducer/reducer";

export const store = configureStore({ reducer });
export type TStore = ReturnType&amp;lt;typeof store.getState&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://redux-toolkit.js.org/api/configureStore" rel="noopener noreferrer"&gt;If we used the standard Redux approach&lt;/a&gt;, it would be an ideal place to add middleware (for example thunk) and connect redux-dev-tool. But we use Redux Toolkit, and here everything is much more interesting 🤩.&lt;/p&gt;

&lt;p&gt;This is where the magic is hidden, which we don't see 🔮. In fact, the &lt;strong&gt;Redux-Toolkit already connected thunk and redux-dev-tool "under the hood"&lt;/strong&gt; when we just used the &lt;strong&gt;configureStore&lt;/strong&gt; function. Only 3 lines of code and what is great the result.&lt;/p&gt;

&lt;p&gt;Additionally, on the last line, you can see the creating of the &lt;strong&gt;TStore&lt;/strong&gt; type. It allows us to use this type in the case when we want to grab data from the Redux store (for example using selector hook - &lt;strong&gt;&lt;a href="https://react-redux.js.org/api/hooks#useselector" rel="noopener noreferrer"&gt;useSelector&lt;/a&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;We go further and go to the last step of the redux connection - the connection in the &lt;strong&gt;index.tsx&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { store } from "store";
import App from "./App";

ReactDOM.render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;Provider store={store}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;,
document.getElementById("root")
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simply uses the &lt;strong&gt;Provider&lt;/strong&gt; component, which should by default wrap our main internal component (&lt;strong&gt;App in our case&lt;/strong&gt;) and use the store property with the value which has name store too (which we created and in the previous step and imported here).&lt;/p&gt;

&lt;p&gt;We finished with the setup. Now let's use what we've been working on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using 🏄‍♂️
&lt;/h2&gt;

&lt;p&gt;Go to the component where we want to use our previously created action creator (in my case it is the &lt;strong&gt;App.tsx&lt;/strong&gt; file).&lt;/p&gt;

&lt;p&gt;We import the &lt;strong&gt;useDispatch&lt;/strong&gt; function (which we will need) and directly our action creator - &lt;strong&gt;changeEmail&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { changeEmail } from "slices/profile";

const App = () =&amp;gt; {
  const dispatch = useDispatch();
  const { email } = useSelector((state: TStore) =&amp;gt; state.profileReducer);
  const handleEmailChange = () =&amp;gt; {
    dispatch(changeEmail({ email: "newEmail@gmail.com" }));
  };

return (
  &amp;lt;div&amp;gt;
    &amp;lt;button onClick={handleEmailChange}&amp;gt;
      Change email
    &amp;lt;/button&amp;gt;    
    &amp;lt;h2&amp;gt;
      Email: {email}
    &amp;lt;/h2&amp;gt;
  &amp;lt;/div&amp;gt;
);
}

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

&lt;/div&gt;



&lt;p&gt;We added a button with an event - &lt;strong&gt;onClick&lt;/strong&gt; which provides as add &lt;strong&gt;handleEmailChange&lt;/strong&gt; function. Inside we call function dispatch with our action creator - &lt;strong&gt;changeEmail&lt;/strong&gt; as a parameter. Inside &lt;strong&gt;changeEmail&lt;/strong&gt; we see an object - it's actually our &lt;strong&gt;payload object&lt;/strong&gt;.📦&lt;/p&gt;

&lt;p&gt;Below the button, we have a header. Inside we use the variable - &lt;strong&gt;email&lt;/strong&gt;. You've probably noticed that we got this variable from the Redux store using the previously mentioned hook - &lt;strong&gt;useSelector&lt;/strong&gt;. useSelector accepts the function with the - state (to which we add the &lt;strong&gt;TStore&lt;/strong&gt; type) parameter and returns the value we want to get.&lt;/p&gt;

&lt;p&gt;After clicking the button we see a new result.&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%2Fc5y5ysx307xj8ljyoluu.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5y5ysx307xj8ljyoluu.gif" alt="1_OTrRse2rR5azAz-qz5iFKQ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we check &lt;strong&gt;&lt;a href="https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=ru" rel="noopener noreferrer"&gt;Redux Dev-Tool&lt;/a&gt;&lt;/strong&gt; (hope you have this useful extension) we can notice what everything is works and our &lt;strong&gt;Redux store&lt;/strong&gt; is changed and now we have a new value for the field - &lt;strong&gt;email&lt;/strong&gt;.&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%2F0gawerlem022dn0nkk5n.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%2F0gawerlem022dn0nkk5n.png" alt="1_CMjwzAY05u5d_UyZPu2oMQ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it, we have full functionality when working with Redux Toolkit. &lt;br&gt;
We can create other methods, create other slices, &lt;a href="https://redux-toolkit.js.org/api/createAsyncThunk#createasyncthunk" rel="noopener noreferrer"&gt;perform asynchronous actions&lt;/a&gt; and use it all in our application. &lt;/p&gt;

&lt;p&gt;Final code example you can check &lt;a href="https://codesandbox.io/s/react-toolkit-typescript-ctnox?file=/src/App.tsx:236-247" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you very much for taking the time to read my article. I hope she helped you figure out the Redux Toolkit.&lt;/p&gt;

&lt;p&gt;As always open to feedback ❤️&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Three steps to understanding Babel</title>
      <dc:creator>Stas Fedyna</dc:creator>
      <pubDate>Fri, 25 Sep 2020 11:43:17 +0000</pubDate>
      <link>https://forem.com/stanislavfedyna/three-steps-to-understanding-babel-1mbn</link>
      <guid>https://forem.com/stanislavfedyna/three-steps-to-understanding-babel-1mbn</guid>
      <description>&lt;p&gt;Babel is a transcompiler, without which development would be much more difficult and not as convenient as now.&lt;/p&gt;

&lt;p&gt;So what is the convenience of using Babel and what opportunities does it provide?&lt;/p&gt;

&lt;p&gt;First of all, you need to understand the main task of Babel — &lt;strong&gt;the ability to write code in new standard (with new functionality) without worrying that this new functionality may not be supported by browsers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The bottom line is that JavaScript is not standing still and is evolving rapidly. Due to this, we (developers) receive every year additions to the functionality of the language in the form of a new standard (so-called ECMAScript or simply ES).&lt;/p&gt;

&lt;p&gt;But the fact that JavaScript is updated every year does not mean that the new functionality becomes available in browsers immediately. Everything is exactly the opposite. This is a fairly long process that takes a long time.&lt;/p&gt;

&lt;p&gt;But we are developers and do not want to wait so long to start using something very cool and something that really simplifies our lives. That’s where Babel comes on stage.&lt;/p&gt;

&lt;p&gt;What is he doing? &lt;strong&gt;Babel takes the code we wrote with the new functionality and converts it into an analog code, but older standard.&lt;/strong&gt; This is done because it is this code browser understands without problems.&lt;/p&gt;

&lt;p&gt;The following is an example of a transcompilation taken from the official &lt;a href="https://babeljs.io/" rel="noopener noreferrer"&gt;Babel&lt;/a&gt; website.&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%2Fi%2Fohxlum4q4dj5yxklqk98.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fohxlum4q4dj5yxklqk98.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is important to note that there are no disadvantages to such a transformation (transcompilation). Our project doesn’t get slower or anything like that. So you can not worry.&lt;/p&gt;

&lt;p&gt;To get the final code understandable to the browser, Babel performs 3 basic steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parsing.&lt;/li&gt;
&lt;li&gt;Transformation.&lt;/li&gt;
&lt;li&gt;Code generation.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Step 1. Parsing
&lt;/h1&gt;

&lt;p&gt;In the first step, the &lt;strong&gt;input data is code written by us with new functionality.&lt;/strong&gt; Babel takes and converts this code into AST (&lt;em&gt;Abstract Syntax Tree&lt;/em&gt;). AST looks like a regular JavaScript object (key: value).&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%2Fi%2Fb5jfkhgsrx7k8byzeeqr.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%2Fi%2Fb5jfkhgsrx7k8byzeeqr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is not necessary to delve into the specified code, we will simply single out the most important idea — &lt;strong&gt;the AST-object contains a description of what our code is (functions, arguments, operators, destructuring, or simply declaring changes).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Module &lt;strong&gt;babel-parcel&lt;/strong&gt; is responsible for this operation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 2. Transformation
&lt;/h1&gt;

&lt;p&gt;In this step, &lt;strong&gt;the input data is the AST object obtained from the previous step.&lt;/strong&gt; As we remember, this object contains a description of what we use in the code (including the new functionality).&lt;/p&gt;

&lt;p&gt;The main idea of ​​this step — &lt;strong&gt;convert the input AST object with the new functionality into the same AST object with the old functionality.&lt;/strong&gt; That is, when passing through the AST object there are keys and their values, which represent the new functionality and therefore must be replaced. As mentioned earlier, this is done so that the browser can perceive our code correctly.&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%2Fi%2Fjbarc6zlo70rcsz3mkop.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%2Fi%2Fjbarc6zlo70rcsz3mkop.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two modules are responsible for this step (transformation) at once — &lt;strong&gt;Babel-traverse&lt;/strong&gt; and &lt;strong&gt;Babel plugin/presets&lt;/strong&gt;, which have their tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;babel-traverse&lt;/strong&gt; — a module that can parse the AST object and replace the required values ​​according to the key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;babel plugin/presets&lt;/strong&gt; — a module that contains a set of presets responsible for a single functional (usually 1 preset = 1 functional, where functional can be some independent unit — destructuring, function, etc.).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Step 3. Code generation
&lt;/h1&gt;

&lt;p&gt;Last step. &lt;strong&gt;Input data — AST object, but with the old functionality (which we got from the previous step).&lt;/strong&gt; All that remains is to turn it into a simple JS code that will be executed by the browser in the future.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;babel-generator&lt;/strong&gt; module is responsible for this action.&lt;/p&gt;

&lt;p&gt;At the output we get the code below (in the block on the right).&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%2Fi%2Fl2mjzv6bfhgmywl9g8r3.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%2Fi%2Fl2mjzv6bfhgmywl9g8r3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The block on the left is what we have from the beginning, our code, which in particular contains new functionality (creation of variables through const, arrow functions and a shortened version of the return from the same functions).&lt;/p&gt;

&lt;p&gt;Next is the “magic” described above, namely 3 steps: parsing, transformation and code generation. Code generation is the end result and what is contained in the image in the right block.&lt;/p&gt;

&lt;p&gt;Schematically, the entire life cycle can be depicted as follows:&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%2Fi%2F0imorh4f7qu3fu1kyzqh.jpeg" 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%2Fi%2F0imorh4f7qu3fu1kyzqh.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now that’s all. I hope I helped you understand how Babel works.&lt;/p&gt;

&lt;p&gt;P.S. Don’t judge me harshly, as this is my first article and I’m always open to feedback ❤️&lt;/p&gt;

</description>
      <category>babel</category>
      <category>javascript</category>
      <category>compiler</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
