<?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: LearnFrontend.IO</title>
    <description>The latest articles on Forem by LearnFrontend.IO (@learnfrontendio).</description>
    <link>https://forem.com/learnfrontendio</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%2F876697%2Fe8eacf7f-2fdb-414c-9a6b-b6455a56cb1c.png</url>
      <title>Forem: LearnFrontend.IO</title>
      <link>https://forem.com/learnfrontendio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/learnfrontendio"/>
    <language>en</language>
    <item>
      <title>Redux Best Practices</title>
      <dc:creator>LearnFrontend.IO</dc:creator>
      <pubDate>Sun, 19 Jun 2022 15:51:17 +0000</pubDate>
      <link>https://forem.com/learnfrontendio/redux-best-practices-231c</link>
      <guid>https://forem.com/learnfrontendio/redux-best-practices-231c</guid>
      <description>&lt;h1&gt;
  
  
  Redux Best Practices
&lt;/h1&gt;

&lt;p&gt;This article has been published in &lt;a href="https://learnfrontend.io/blog/redux-best-practices"&gt;my personal blog&lt;/a&gt; too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt; is a library used for global state management, meaning if you have a piece of state that you want to access in different places of your app, you can use Redux to manage that state and make it easily accessible anywhere. The global state can be anything from logged-in user information to UI state such as theme, etc.&lt;/p&gt;



&lt;p&gt;Redux is being used a lot together with &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; apps, however, it is possible to use Redux with any other library. Make sure you check out this tutorial on &lt;a href="https://learnfrontend.io/blog/how-to-use-react-with-redux"&gt;how to use Redux with React&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
This guide can be beneficial if you have started learning redux lately, but even&lt;br&gt;
if you are an experienced developer working with Redux already. Additionally following&lt;br&gt;
this guide is going to improve your general skills with Redux by a lot and make you&lt;br&gt;
ready to work on bigger codebases that companies have.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Below I have brought up some of the best practices when using Redux that when followed,&lt;br&gt;
will help you make apps that scale well and have better performance.&lt;/p&gt;
&lt;h2&gt;
  
  
  Redux Toolkit
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://redux-toolkit.js.org/"&gt;Redux Toolkit&lt;/a&gt; is a wrapper library written for the original Redux for the sake of making it easier, and faster to use Redux by reducing the boilerplate and introducing features such as thunks out of the box without having to install third-party libraries.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
It is fully written in &lt;a href="https://serokell.io/blog/why-typescript"&gt;Typescript&lt;/a&gt; and&lt;br&gt;
is highly recommended that Redux Toolkit is used instead of the original Redux when&lt;br&gt;
using it for managing global state.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
It offers a flexible API, making it possible for making the state management scalable&lt;br&gt;
without having to worry about the limits of the library. One of the key concepts&lt;br&gt;
of Redux is immutability, and Redux Toolkit makes it easier than ever to keep the&lt;br&gt;
immutability by already setting up the &lt;a href="https://github.com/immerjs/immer"&gt;ImmerJS&lt;/a&gt;&lt;br&gt;
library internally, so the developer does not have to worry about accidentally modifying&lt;br&gt;
the state directly.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
One of the most noticeable things for developers when switching to Redux Toolkit&lt;br&gt;
is the way reducers are created. Previously we used action creators and created a&lt;br&gt;
reducer where we handled all the actions.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Now we can use the &lt;a href="https://redux-toolkit.js.org/api/createslice"&gt;createSlice&lt;/a&gt; API&lt;br&gt;
to create reducers and it will eliminate the need to write action creators by creating&lt;br&gt;
them for you.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
An example of createSlice can be seen below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PayloadAction&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&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;span class="na"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;userLoggedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PayloadAction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&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="c1"&gt;// Later you can dispatch actions by accessing the fields inside `actions`.&lt;/span&gt;
&lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userLoggedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also introduces a whole new feature that is not in the original Redux, and that is the &lt;a href="https://redux-toolkit.js.org/rtk-query/overview"&gt;RTK Query&lt;/a&gt; which handles API calls, we are going to talk about it in a later section.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
By using Redux Toolkit, the logic of your app will be simplified and it will be easier&lt;br&gt;
for devs to maintain the codebase. If you haven’t already switched to Redux Toolkit,&lt;br&gt;
then you should probably consider it.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
The library can be installed by using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;reduxjs&lt;/span&gt;&lt;span class="sr"&gt;/toolki&lt;/span&gt;&lt;span class="err"&gt;t
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Proper Folder Structure
&lt;/h2&gt;

&lt;p&gt;A good folder structure is crucial for scalability no matter what app you are building, and is not any less true for apps using Redux. However, choosing the right folder structure can be really hard, I’m going to bring the most preferred ways of structuring your Redux app, one that has been tested and is very scalable for building massive web applications.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
It is a &lt;strong&gt;feature-based structure&lt;/strong&gt; and it means structuring the app based on features,&lt;br&gt;
for example, if one of the features you have is the checkout process for e-commerce,&lt;br&gt;
then you might want to group all related global states for that process in a single&lt;br&gt;
reducer created using createSlice that we mentioned above.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
You will have separated files for the reducers, selectors, thunks, types, etc., however,&lt;br&gt;
everything related to a feature will be put inside the same folder. Take the example&lt;br&gt;
below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="sr"&gt;/sr&lt;/span&gt;&lt;span class="err"&gt;c
&lt;/span&gt;    &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tsx&lt;/span&gt;
    &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;pages&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;features&lt;/span&gt;
        &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;
            &lt;span class="c1"&gt;// The slice mainly contains the reducers functionality&lt;/span&gt;
            &lt;span class="c1"&gt;// and exports the actions. Would be created using createSlice API.&lt;/span&gt;
            &lt;span class="nx"&gt;checkoutSlice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt;

            &lt;span class="c1"&gt;// This file would be used for writing the types&lt;/span&gt;
            &lt;span class="c1"&gt;// if you are using typescript.&lt;/span&gt;
            &lt;span class="c1"&gt;// Example here would be the store interface.&lt;/span&gt;
            &lt;span class="nx"&gt;checkoutTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt;

            &lt;span class="c1"&gt;// Here would go all the selectors that would&lt;/span&gt;
            &lt;span class="c1"&gt;// be used to select data from the redux store for this feature.&lt;/span&gt;
            &lt;span class="nx"&gt;checkoutSelectors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt;

            &lt;span class="c1"&gt;// Here would go all the thunks that would be used by this feature.&lt;/span&gt;
            &lt;span class="nx"&gt;checkoutThunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt;

            &lt;span class="c1"&gt;// In this file there would be any functions which you would&lt;/span&gt;
            &lt;span class="c1"&gt;// use in your features store.&lt;/span&gt;
            &lt;span class="c1"&gt;// Can be anything that will make the code more readable,&lt;/span&gt;
            &lt;span class="c1"&gt;// say a function which does modifications to the payload&lt;/span&gt;
            &lt;span class="c1"&gt;// before saving to the store.&lt;/span&gt;
            &lt;span class="nx"&gt;checkoutHelpers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt;

            &lt;span class="c1"&gt;// It is not limited to only these files, you can&lt;/span&gt;
            &lt;span class="c1"&gt;// create anything you want, ex. you can have epics file, constants, etc.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, no folder structure is perfect, and finally, it is up to you how you structure everything. As Redux is used to maintain a global state, it can get really messy if we start putting everything in Redux. So it is a good practice to keep Redux as small as possible with only data that really needs to be used in multiple places of the app. When developing an app, you should really take the time to think about where the data should live and not just put everything in Redux.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In libraries like React it has started being easier to separate data, for example,&lt;br&gt;
you can use the local component state for managing component UI data, or even &lt;a href="https://www.upbeatcode.com/react/when-to-use-context-api-vs-redux"&gt;context&lt;br&gt;
providers&lt;/a&gt; if&lt;br&gt;
you need some specific data across a page. Context providers should also be considered&lt;br&gt;
when saving UI data. It can really help implementing features with the &lt;a href="https://en.wikipedia.org/wiki/Single-responsibility_principle"&gt;principle&lt;br&gt;
of single responsibility&lt;/a&gt;&lt;br&gt;
in mind. Say for example you can have 1 context provider for the Theme, and another&lt;br&gt;
for UI changes in other components (ex. navigation, layout, etc.)&lt;/p&gt;
&lt;h2&gt;
  
  
  Event actions
&lt;/h2&gt;

&lt;p&gt;When an action is dispatched, all of the reducers will be notified and every single one of them will check if they can handle the action. Not only that, but the &lt;a href="https://github.com/reduxjs/redux-devtools"&gt;Redux dev tools&lt;/a&gt; will be a mess trying to see what action was fired when. That’s why we should try to think of actions as events that happened and not what the action is changing. For example, we should rather have an action called &lt;strong&gt;userLoggedIn&lt;/strong&gt; than &lt;strong&gt;setUserId&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Normally in the &lt;strong&gt;userLoggedIn&lt;/strong&gt; event action, you could put what more data in the&lt;br&gt;
payload than in the &lt;strong&gt;setUserId&lt;/strong&gt;. For example, in the &lt;strong&gt;userLoggedIn&lt;/strong&gt; you could&lt;br&gt;
set all the user data in Redux, including any side effects you want to have when&lt;br&gt;
the user logs in (ex. changing the status to authenticated). Additionally, you can&lt;br&gt;
have multiple reducers responding to a single action, and with &lt;strong&gt;createSlice&lt;/strong&gt; now&lt;br&gt;
it is even easier to respond to other external actions using the &lt;strong&gt;extraReducers&lt;/strong&gt;&lt;br&gt;
field, see the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// userSlice.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userActions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;//...&lt;/span&gt;
  &lt;span class="na"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;userLoggedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&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="c1"&gt;// environmentSlice.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;environmentSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;environment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;//...&lt;/span&gt;
  &lt;span class="na"&gt;extraReducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;builder&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="nx"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userActions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userLoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authenticationStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loggedIn&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;Thinking of actions as events we can keep the number of dispatched actions lower and debugging will always be faster as the dev tools will be cleaner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting small pieces of State
&lt;/h2&gt;

&lt;p&gt;Hooks like &lt;a href="https://react-redux.js.org/api/hooks"&gt;useSelector&lt;/a&gt; will cause the component using it to rerender every time the returned value has changed. Meaning if you select the whole user object, whenever something in the object changes, even if you didn’t use it in the component, it is still going to rerender the component. So it is always preferred to only select what you really need from the state. So instead of selecting the whole user object, we would select only the user firstName. This way there will be a rerender only when the firstName changes.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Will cause a rerender no matter if you are not using the whole user object.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;selectUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Preferred way, as this will cause a rerender only when firstName changes.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;selectUserFirstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  RTK Query for the API requests
&lt;/h2&gt;

&lt;p&gt;RTK Query is a feature from Redux included in the Redux Toolkit library to make it easier to make API requests and handle caching. RTK Query aims to totally replace thunks for making API requests.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
RTK Query introduces a new API called &lt;strong&gt;createApi&lt;/strong&gt; that can be used to create the&lt;br&gt;
necessary functions for making the requests and handling caching. It includes features&lt;br&gt;
such as cache invalidation, prefetching, code splitting, etc.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
It is written to work with React out of the box as it creates hooks that can be used&lt;br&gt;
directly in the components for making the requests.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Let's take a look at a simple example of how to create simple query hooks we can&lt;br&gt;
use in React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createApi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fetchBaseQuery&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@reduxjs/toolkit/query/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// createApi should only be used once per app&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createApi&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;baseQuery&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fetchBaseQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="na"&gt;endpoints&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;build&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="c1"&gt;// Building a query function that will later be transformed in a hook&lt;/span&gt;
    &lt;span class="na"&gt;getPost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`post/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;span class="c1"&gt;// Hook exported to be used in React&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useGetPostQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endpoints&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPost&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useQuery&lt;/span&gt;

&lt;span class="c1"&gt;// MyComponent.tsx&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isLoading&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;RTK Query can be very useful, however, compared to other libraries such as React Query or SWR, it is more complicated and requires more boilerplate. So you should really do your research on what you need for your app before deciding on whether to use RTK Query or other alternatives. If you are building apps from the start which don’t need to work much with Redux then you can surly go with React Query or SWR, but if you already have a massive application that uses Redux as its main state management solution and you can’t afford to introduce a new library, then RTK Query can be more suitable.&lt;/p&gt;



&lt;p&gt;Are you interested in reading more about Frontend in general? Then make sure you &lt;a href="https://twitter.com/learnfrontendio"&gt;follow us on Twitter&lt;/a&gt; to get the latest updates.&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How to use React with Redux</title>
      <dc:creator>LearnFrontend.IO</dc:creator>
      <pubDate>Thu, 16 Jun 2022 15:17:40 +0000</pubDate>
      <link>https://forem.com/learnfrontendio/how-to-use-react-with-redux-20d7</link>
      <guid>https://forem.com/learnfrontendio/how-to-use-react-with-redux-20d7</guid>
      <description>&lt;p&gt;This article has been publish in &lt;a href="https://learnfrontend.io/blog/how-to-use-react-with-redux"&gt;my personal blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redux is a library used for managing global state, it can be used with or without React, but in this tutorial, we are going to take a look at how we can use Redux together with React.&lt;/p&gt;



&lt;p&gt;You will need the following tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A code editor - I will be using &lt;a href="https://code.visualstudio.com/"&gt;VSCode&lt;/a&gt; but you can use any you like.&lt;/li&gt;
&lt;li&gt;npm &amp;amp; npx

&lt;ul&gt;
&lt;li&gt;Install on Linux: &lt;code&gt;sudo apt install npm nodejs ;npm i -g npx&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tubemint.com/install-nodejs-npm-npx-yarn/"&gt;Install on Windows&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Initial setup
&lt;/h2&gt;

&lt;p&gt;First, we are going to start by creating a &lt;a href="https://reactjs.org/"&gt;ReactJS&lt;/a&gt; app and installing &lt;a href="https://redux-toolkit.js.org/"&gt;Redux&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
I will be using &lt;a href="https://learntypescript.dev/01/l1-what-is-ts"&gt;Typescript&lt;/a&gt; but you&lt;br&gt;
can also do it in Javascript:&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Run the following commands in the terminal. (If you want to do it in Javascript,&lt;br&gt;
make sure you remove the “&lt;a href="https://create-react-app.dev/docs/adding-typescript/"&gt;—template typescript&lt;/a&gt;”&lt;br&gt;
flag)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app my-react-redux-app &lt;span class="nt"&gt;--template&lt;/span&gt; typescript
&lt;span class="nb"&gt;cd &lt;/span&gt;my-react-redux-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need 2 libraries for redux, the first one has the &lt;a href="https://redux-toolkit.js.org/"&gt;redux API&lt;/a&gt; and the second one has the &lt;a href="https://react-redux.js.org/"&gt;Redux configured to be used with React&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now go ahead and replace the content of App.tsx with the following:&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Don’t worry about it as it's just &lt;a href="https://egghead.io/learn/react/beginners/wtf-is-jsx"&gt;JSX&lt;/a&gt;&lt;br&gt;
with an input whose value we are going to save in the store when the button is clicked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;localStateName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLocalStateName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
        &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;localStoreName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setLocalStoreName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Submit to store&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;br&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;br&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      Name in the store: &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;asd&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Store Setup
&lt;/h2&gt;

&lt;p&gt;Next, we are going to configure the store and create a reducer that will handle our state changes&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Redux &lt;a href="https://learnfrontend.io/blog/what-is-functional-programming"&gt;state is immutable&lt;/a&gt;&lt;br&gt;
and the only way to update it is using &lt;a href="https://redux.js.org/tutorials/fundamentals/part-3-state-actions-reducers"&gt;reducers&lt;/a&gt;.&lt;br&gt;
Think of reducers as state machines, they handle how the state is changed.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
First, create a new file called store.ts (or store.js if you are not using typescript),&lt;br&gt;
import a function called configureStore, and create an empty store with it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;configureStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go ahead and import createSlice from the same library and create a reducer with it. createSlice is a wrapper around the &lt;a href="https://redux.js.org/"&gt;old API from redux&lt;/a&gt; which reduces quite some boilerplate when creating reducers.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
createSlice requires an object with 3 arguments. The name of the reducer, the initial&lt;br&gt;
state of the sub-store, and the reducer itself. You can place the code just above&lt;br&gt;
myStore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;reducers&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;Now we can add our reducer to the &lt;a href="https://redux-toolkit.js.org/api/configureStore"&gt;store configuration&lt;/a&gt;. Modify myStore so it gets the reducer we created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Actions
&lt;/h2&gt;

&lt;p&gt;We dispatch an action to the reducer when we want to have a state change. And the reducer will know what function to trigger for updating the state based on the action that we dispatch. But first, we need to create actions that we can use. For now, let us create 1 action and call it “nameUpdated”. Basically, it will update the "name"`property we have in the state with whatever payload we give.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow"&gt;Payloads&lt;/a&gt;&lt;br&gt;
can be passed when you dispatch an action. We are going to take a look at that a&lt;br&gt;
bit later.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Inside the createSlice in the reducers property, add a function called nameUpdated&lt;br&gt;
like below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`tsx&lt;br&gt;
import {&lt;br&gt;
  configureStore,&lt;br&gt;
  createSlice,&lt;br&gt;
  PayloadAction, // Typescript type used to type the action.&lt;br&gt;
} from "@reduxjs/toolkit";&lt;/p&gt;

&lt;p&gt;export const { actions, reducer } = createSlice({&lt;br&gt;
...&lt;br&gt;
  reducers: {&lt;br&gt;
    nameUpdated: (state, action: PayloadAction) =&amp;gt; {&lt;br&gt;
      state.name = action.payload;&lt;br&gt;
    },&lt;br&gt;
  },&lt;br&gt;
});&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux Provider for React
&lt;/h2&gt;

&lt;p&gt;Later we are going to use hooks to work with the redux store from react components. And those hooks need access to the redux store. react-redux allows that by providing a &lt;a href="https://reactjs.org/docs/context.html"&gt;React context provider&lt;/a&gt; to us, we can wrap our App with.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
You can do so by going to index.ts (or index.js) and importing the initStore you&lt;br&gt;
created earlier and the Provider from “react-redux”&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;tsx&lt;br&gt;
import { initStore } from "./store";&lt;br&gt;
import { Provider as ReduxProvider } from "react-redux";&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can wrap the App with the Provider and pass the initStore through the store prop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;tsx&lt;br&gt;
...&lt;br&gt;
root.render(&lt;br&gt;
  &amp;lt;React.StrictMode&amp;gt;&lt;br&gt;
    &amp;lt;ReduxProvider store={initStore}&amp;gt;&lt;br&gt;
      &amp;lt;App /&amp;gt;&lt;br&gt;
    &amp;lt;/ReduxProvider&amp;gt;&lt;br&gt;
  &amp;lt;/React.StrictMode&amp;gt;&lt;br&gt;
);&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating the store
&lt;/h2&gt;

&lt;p&gt;We are going to update the store by dispatching the actions that we created earlier. Let's go in the App.tsx file and do that when the user clicks the submit button. First, import the useDispatch hook from react-redux and import the actions you created from store.ts&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;tsx&lt;br&gt;
import { useDispatch } from "react-redux";&lt;br&gt;
import { actions } from "./store";&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let us get the dispatch function from useDispatch hook by executing it and getting its return value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`tsx&lt;br&gt;
function App() {&lt;br&gt;
  const dispatch = useDispatch()&lt;/p&gt;

&lt;p&gt;const [localStateName, setLocalStateName] = useState("");&lt;br&gt;
  ...&lt;br&gt;
}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we are going to create the click handler, so when we click the submit button it will dispatch the action and change the state with the value that is in the localStateName. Create the function just below the useState hook and dispatch the nameUpdated action.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`tsx&lt;br&gt;
function App() {&lt;br&gt;
  ...&lt;br&gt;
  const [localStateName, setLocalStateName] = useState("");&lt;/p&gt;

&lt;p&gt;const handleClick = () =&amp;gt; {&lt;br&gt;
    dispatch(actions.nameUpdated(localStateName));&lt;br&gt;
  };&lt;/p&gt;

&lt;p&gt;...&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can just pass this function to the submit button as click handler and when we click in the submit button it is going to change the state.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;tsx&lt;br&gt;
function App() {&lt;br&gt;
  ...&lt;br&gt;
  return (&lt;br&gt;
    ...&lt;br&gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Submit to store&amp;lt;/button&amp;gt;&lt;br&gt;
    ...&lt;br&gt;
  );&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I would suggest you install the &lt;a href="https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en"&gt;Redux Devtools&lt;/a&gt; and then you can track any changes to the redux state. For example, let's type some random input and press submit to see what changes in the state.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
You can open Redux Devtools by opening the chrome dev tools (normally F12) and finding&lt;br&gt;
the redux tab. Should be at the end if you just installed it.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Now type something in the input and press “Submit to store”&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="/redux-devtools.png" class="article-body-image-wrapper"&gt;&lt;img src="/redux-devtools.png" alt="Redux Devtools"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
We can see that the name in the state has changed to the value we typed in the input.&lt;br&gt;
You can play around with the dev tools a bit to get used to them as they can be really&lt;br&gt;
helpful. Check out this &lt;a href="https://codeburst.io/redux-devtools-for-dummies-74566c597d7"&gt;tutorial on how to use the Redux devtools&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting the state
&lt;/h2&gt;

&lt;p&gt;react-redux gives us a hook called useSelector, which we can use to select a state from the store. It accepts a callback where it will pass the state as the first argument and will cause the component to rerender every time the return value of the callback changes.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Go ahead and import it from react-redux like below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;tsx&lt;br&gt;
import { useDispatch, useSelector } from "react-redux";&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets use it to get our name value from the state.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`tsx&lt;br&gt;
const [localStateName, setLocalStateName] = useState("");&lt;/p&gt;

&lt;p&gt;const name = useSelector(({ name }: { name: string }) =&amp;gt; name);&lt;/p&gt;

&lt;p&gt;const handleClick = () =&amp;gt; {&lt;br&gt;
...&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can render the name directly in JSX&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;tsx&lt;br&gt;
...&lt;br&gt;
Name in the store: &amp;lt;code&amp;gt;{name}&amp;lt;/code&amp;gt;&lt;br&gt;
...&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now every time you type something and press submit the state in redux is going to get updated and will inform your component of the update and will change the rendered value for you.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a href="/app-example-finished.png" class="article-body-image-wrapper"&gt;&lt;img src="/app-example-finished.png" alt="Finished example in action"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Redux is a very scalable library for managing global states, is being used everywhere, from e-commerce stores to anything that requires state management. And it seems like the perfect match for React. I use it almost every day and can say that I’m very happy with it.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
If you liked the tutorial make sure you follow me on &lt;a href="https://twitter.com/learnfrontendio"&gt;Twitter&lt;/a&gt;&lt;br&gt;
to get the latest articles, or if you would like a tutorial on something you prefer,&lt;br&gt;
make sure you let me know about it and I’ll do my best to bring it to you. My email&lt;br&gt;
is &lt;a href="mailto:support@learnfrontend.io"&gt;support@learnfrontend.io&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Are you interested in reading more about Frontend in general? Then make sure you &lt;a href="https://twitter.com/learnfrontendio"&gt;follow us on Twitter&lt;/a&gt; to get the latest updates.&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>What is functional programming</title>
      <dc:creator>LearnFrontend.IO</dc:creator>
      <pubDate>Tue, 14 Jun 2022 15:47:46 +0000</pubDate>
      <link>https://forem.com/learnfrontendio/what-is-functional-programming-3eo1</link>
      <guid>https://forem.com/learnfrontendio/what-is-functional-programming-3eo1</guid>
      <description>&lt;p&gt;This is my second blog post and it has been published in &lt;a href="https://learnfrontend.io/blog/what-is-functional-programming"&gt;my personal blog too&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Functional programming means using functions as building blocks to construct a process. Unlike OOP, functional programming is not an imperative paradigm but a declarative one, meaning it describes what the code has to accomplish, but not how.&lt;/p&gt;

&lt;p&gt;Languages such as Haskell have been completely written with functional programming in mind. Javascript also makes it possible to follow functional programming principles, however, it also supports OOP.&lt;/p&gt;

&lt;p&gt;Below are some of the key concepts from functional programming&lt;/p&gt;

&lt;h2&gt;
  
  
  Pure functions
&lt;/h2&gt;

&lt;p&gt;Pure functions are simple, reusable blocks of code written as functions, that don’t have any side effects, meaning the output of a function is defined by its input.&lt;/p&gt;

&lt;p&gt;They should not modify anything outside of the function and should be stateless, meaning whenever the function runs, it should behave like it was its first time running. This also has more benefits like reducing the number of bugs that exist when there is a shared state between functions.&lt;/p&gt;

&lt;p&gt;Example of an impure function that modifies a variable outside of itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6lmp04sE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmh7q6ew6iig4xg70fpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6lmp04sE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmh7q6ew6iig4xg70fpv.png" alt="Impure function example" width="880" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Immutability
&lt;/h2&gt;

&lt;p&gt;The main idea behind immutability is that when the data is created, it will never be mutated again. In functional programming, this is a key concept. It helps get rid of the rather complex bugs caused by mutating data over time. Libraries such as React and Redux, only provide a state which is not modifiable directly, instead, they make it possible to always create a new state instead.&lt;/p&gt;

&lt;p&gt;ES6 array functions are an example of functions that don’t modify the data directly but instead always return a new one. One of them is the map function which can be seen in the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---HHXVXR_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bum3pjw9fivolnbblp1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---HHXVXR_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bum3pjw9fivolnbblp1f.png" alt="es6 map function example" width="880" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Functional programming can be very useful whether you are building frontend web applications or any other type of app. So if you haven’t already, I hope you give it a try and check for yourself if it's easier writing functional code.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
