<?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: B.stella</title>
    <description>The latest articles on DEV Community by B.stella (@bstella).</description>
    <link>https://dev.to/bstella</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%2F522051%2Fcd287f35-cc24-4a46-9c5f-60b054fd57ec.png</url>
      <title>DEV Community: B.stella</title>
      <link>https://dev.to/bstella</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bstella"/>
    <language>en</language>
    <item>
      <title>Testing</title>
      <dc:creator>B.stella</dc:creator>
      <pubDate>Fri, 11 Nov 2022 17:06:12 +0000</pubDate>
      <link>https://dev.to/bstella/testing-35hb</link>
      <guid>https://dev.to/bstella/testing-35hb</guid>
      <description></description>
    </item>
    <item>
      <title>Redux toolkit with Typescript</title>
      <dc:creator>B.stella</dc:creator>
      <pubDate>Thu, 28 Apr 2022 13:10:06 +0000</pubDate>
      <link>https://dev.to/bstella/redux-toolkit-with-typescript-42n7</link>
      <guid>https://dev.to/bstella/redux-toolkit-with-typescript-42n7</guid>
      <description>&lt;p&gt;Why should you consider using redux toolkit with typescript? Typescript is used to apply types to the code you write in Javascript. One of its benefits is it helps in spotting bugs early, even before running your code. Code management and refactoring are made easier with typescript. IDEs are able to offer more assistance like providing accurate suggestions and autocompletion when using typescript.&lt;/p&gt;

&lt;p&gt;In this article, we are going to learn how to use redux toolkit with typescript. We will be learning what each additional typescript code means. The store structure will be simple, so by the end of this article, you should be able to understand how to integrate typescript with redux toolkit.&lt;/p&gt;

&lt;p&gt;This article assumes you have a basic knowledge of typescript and how to set up a redux toolkit. A &lt;a href="https://codesandbox.io/s/headless-breeze-88kcv5?from-embed" rel="noopener noreferrer"&gt;code sandbox&lt;/a&gt; of the project is provided at the end of the article.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To get started with typescript you can check out &lt;a href="https://www.typescriptlang.org/docs/" rel="noopener noreferrer"&gt;typescript docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check out &lt;a href="https://dev.to/bstella/a-beginners-introduction-to-redux-toolkit-1mao"&gt;my article&lt;/a&gt; on how to set up a redux toolkit.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Let's look at the store (index.tsx) first. &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651096651693%2FUD-pkN0dW.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651096651693%2FUD-pkN0dW.png" alt="code.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have added two types here. let's see what they do.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export type RootState = ReturnType&amp;lt;typeof store.getState&amp;gt;&lt;/code&gt;: &lt;code&gt;RootState&lt;/code&gt; should be a type with all the values in our state. &lt;code&gt;store.getState&lt;/code&gt; gets all the values in our state. To refer to the type of our state, we use the typescript operator &lt;code&gt;typeof&lt;/code&gt;. &lt;code&gt;ReturnType&lt;/code&gt; takes a function type and produces its return type.&lt;/p&gt;

&lt;p&gt;Therefore &lt;code&gt;RootState&lt;/code&gt; results in a type with the variables in our store&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651147020960%2Fwth5fmwvL.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651147020960%2Fwth5fmwvL.png" alt="code4.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export type AppDispatch = typeof store.dispatch&lt;/code&gt;: Using &lt;code&gt;typeof&lt;/code&gt; operator we can refer to the type of &lt;code&gt;store.dispatch&lt;/code&gt;. This sets the type of our store dispatch method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Inferring these types from the store itself means that they correctly update as you add more state slices or modify middleware settings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You will notice we are exporting both types, which means we will need them later in our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slices
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651098952614%2FqmCU59dJr.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651098952614%2FqmCU59dJr.png" alt="code1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export const authSelector = (state: RootState) =&amp;gt; state.auth&lt;/code&gt;: &lt;code&gt;authSelector&lt;/code&gt; is a function that returns a selected part of our state in this case, &lt;code&gt;auth&lt;/code&gt;. The &lt;code&gt;state&lt;/code&gt; type has been set to &lt;code&gt;RootState&lt;/code&gt;, which is a type of all the values in our state. It comes with a nice auto-completion which is useful in knowing the variable available in the store.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651147627666%2FnmzuE904y.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651147627666%2FnmzuE904y.png" alt="code5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hooks
&lt;/h2&gt;

&lt;p&gt;All that remains is to use it in our component. We will need the useDispatch and useSelector hooks. Let's also add types to them.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651099120714%2FuzdSd2uFw.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651099120714%2FuzdSd2uFw.png" alt="code2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export const useAppDispatch = () =&amp;gt; useDispatch&amp;lt;AppDispatch&amp;gt;()&lt;/code&gt;: We have set the type &lt;code&gt;useDispatch()&lt;/code&gt; to our previously exported &lt;code&gt;AppDispatch&lt;/code&gt; here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export const useAppSelector: TypedUseSelectorHook&amp;lt;RootState&amp;gt; = useSelector&lt;/code&gt;: &lt;code&gt;TypeUseSelector&lt;/code&gt; is a type from redux, together with our previously exported &lt;code&gt;RootState&lt;/code&gt; we used it to set &lt;code&gt;useAppSelector&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What have we done?&lt;/strong&gt; Instead of using useDipatch and useSelector, we can now use useAppDispatch and useAppSelector in their place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why have we gone through the trouble of doing it like this?&lt;/strong&gt; Redux docs brilliantly explain this &lt;a href="https://react-redux.js.org/using-react-redux/usage-with-typescript#define-typed-hooks" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that, we can use them in our various components. which will look like this&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651144899959%2FGtoty8HrX.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1651144899959%2FGtoty8HrX.png" alt="code3.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const { isLogin, userName } = useAppSelector(authSelector)&lt;/code&gt; : &lt;code&gt;authSelector&lt;/code&gt; points to &lt;code&gt;state.auth&lt;/code&gt; which contains two variables. Using object restructuring, we can get those variables we need.&lt;/p&gt;

&lt;p&gt;And we are Done🥳🥳. Here is a sandbox for what we have done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/embed/headless-breeze-88kcv5?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark" rel="noopener noreferrer"&gt;https://codesandbox.io/embed/headless-breeze-88kcv5?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Beginner's introduction to redux toolkit</title>
      <dc:creator>B.stella</dc:creator>
      <pubDate>Sat, 09 Apr 2022 17:33:52 +0000</pubDate>
      <link>https://dev.to/bstella/a-beginners-introduction-to-redux-toolkit-1mao</link>
      <guid>https://dev.to/bstella/a-beginners-introduction-to-redux-toolkit-1mao</guid>
      <description>&lt;p&gt;The bigger your project the more state you need to manage. This article explains how to use/set up redux toolkit for state management in react.&lt;/p&gt;

&lt;p&gt;This article assumes you have a basic understanding of React and javascript in general. A &lt;a href="https://codesandbox.io/s/serverless-pine-pi5p02?file=/src/App.js"&gt;code sandbox&lt;/a&gt; of the project is provided at the end of the article. you can check out the project structure among other things there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why redux toolkit?
&lt;/h3&gt;

&lt;p&gt;Redux helps us to manage app-wide states. In other words, State that we would like to share across various components like user authentication status, etc. An alternative to redux is context API. so why redux toolkit? It reduces lots of boilerplates that redux has. So you can think of redux toolkit as a package that helps to write redux more elegantly.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does it works?
&lt;/h3&gt;

&lt;p&gt;To understand how redux state management works. This image perfectly demonstrates it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fx83CYe6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649421028221/PTz3BtQTT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fx83CYe6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649421028221/PTz3BtQTT.png" alt="image1.png" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/45416237/axios-calls-in-actions-redux"&gt;https://stackoverflow.com/questions/45416237/axios-calls-in-actions-redux&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We store our state in the store, The only thing permitted to change/mutate the state in the store is a reducer function. if that's the only thing permitted to change/mutate our store what then triggers the reducer function? A dispatch function/Action.&lt;/p&gt;

&lt;p&gt;A high-level overview will be, for instance, we click a button which triggers a dispatch function which in turn is sent to the reducer function which is responsible for updating the state. After the state is updated our components will automatically be re-rendered with the new state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Redux Toolkit
&lt;/h3&gt;

&lt;p&gt;To understand more, we are going to do a simple project where we use redux toolkit to set user authentication status in our store.&lt;/p&gt;

&lt;p&gt;The first step to get started is to install redux toolkit and redux in our react app with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install @reduxjs/toolkit react-redux&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating a slice
&lt;/h4&gt;

&lt;p&gt;After successful installation, we create an auth slice using createSlice from redux toolkit. &lt;code&gt;createSlice&lt;/code&gt; requires three fields: the name, initialState, and reducer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Kf92lyBh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649401454457/772_fvCkX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kf92lyBh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649401454457/772_fvCkX.png" alt="code.png" width="880" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; identifies the name of our slice.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;initialState&lt;/code&gt; is the initial state value of the state.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reducers&lt;/code&gt; is an object that will contain functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;let's set the &lt;code&gt;name&lt;/code&gt; to auth, the initial state to an object of &lt;code&gt;isLogin&lt;/code&gt; with its value, and the reducer will contain two functions &lt;code&gt;login&lt;/code&gt; and &lt;code&gt;logout&lt;/code&gt;. with that we have:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7-jYOVHF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649425416964/eJ68urNGq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7-jYOVHF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649425416964/eJ68urNGq.png" alt="code1.png" width="880" height="863"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are almost done setting up the &lt;code&gt;createSlice&lt;/code&gt; function but we still need two things from it. one is its actions and the second is the reducer. &lt;strong&gt;Why?&lt;/strong&gt; remember the only thing permitted to mutate the store is the reducer so we need to export it to configure it with the store later on. If the reducer is gonna be changing the state, the store(which we will also set up very soon) needs to know about it. &lt;strong&gt;Why actions too?&lt;/strong&gt; right now we have two reducer functions (logout and login). actions will determine the particular reducer function we want to be implemented on the store. from our example, that is either the &lt;code&gt;login&lt;/code&gt; or &lt;code&gt;logout&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--194gvn-_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649425451144/BdWgK2qzp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--194gvn-_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649425451144/BdWgK2qzp.png" alt="code2.png" width="880" height="960"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to export &lt;code&gt;authReducer&lt;/code&gt; as default while &lt;code&gt;authActions&lt;/code&gt; as a constant. we will need them later on. with that, we are done with the auth slice section. we can move to the store section.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting Up The Store
&lt;/h4&gt;

&lt;p&gt;To create a store section we use &lt;code&gt;configureStore&lt;/code&gt; from redux toolkit. &lt;code&gt;configureStore&lt;/code&gt; combines all the various store slices we might have into one. In our case, we have just one slice(auth slice) but in a bigger project, you might have more than one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9wOBNh8o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649425646572/nuXxnGt8N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9wOBNh8o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649425646572/nuXxnGt8N.png" alt="code22.png" width="880" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;configureStore&lt;/code&gt; expects a reducer field which is an object that contains a key-value pair. The key is the name of our auth slice while the value will be our auth reducer function. We exported it as default from the auth slice file, we can import it with any name of our choice in this case &lt;code&gt;authReducer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A high level of what we are doing is telling the store "hey we have a store slice named &lt;code&gt;auth&lt;/code&gt; and also its reducer is named &lt;code&gt;authReducer&lt;/code&gt;". If we had more than one store slice let's say we have a count slice. which would have been created similar to how we created the auth slice. we would have also included it here i.e &lt;code&gt;reducer{auth:authReducer, count: countReducer}&lt;/code&gt;. Thereby telling the store "hey we have two store slices (auth and count) and two reducers (authReducer and countReducer). "&lt;/p&gt;

&lt;p&gt;You will notice we are also exporting the variable store which means we need it somewhere else in our application but where?!? we will use it along with the provider component.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Provider
&lt;/h4&gt;

&lt;p&gt;To ensure all our react components have access to this store, we go to the topmost file in react.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dMsPjN8W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649403959248/Zt4q8suM3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dMsPjN8W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649403959248/Zt4q8suM3.png" alt="code3.png" width="880" height="834"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We import &lt;code&gt;Provider&lt;/code&gt; from react-redux and then wrap it around the app component, provider expects a store. We pass our previously exported &lt;code&gt;store&lt;/code&gt; to it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Actions
&lt;/h4&gt;

&lt;p&gt;So far we have created our store and configured it so that the only thing permitted to change it are reducers. but how do we trigger a change to this store we configured - &lt;strong&gt;Actions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;let's set up a simple UI for this. we will use bootstrap to achieve this simple layout.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GnAGNJ3T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649406061150/GmCb8ay8d.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GnAGNJ3T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649406061150/GmCb8ay8d.PNG" alt="Capture.PNG" width="764" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mSPmluHw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649404991883/9mivP_uDB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mSPmluHw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649404991883/9mivP_uDB.png" alt="code4.png" width="880" height="793"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the workflow we want to achieve, when we click on the login button we want a state change of isLogin from false to true. This is where our previously exported &lt;code&gt;authActions&lt;/code&gt; will come in handy. On the login button, we are going to add a loginHandler function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LoWz3U92--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649407297521/589ZYDOCr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LoWz3U92--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649407297521/589ZYDOCr.png" alt="code6.png" width="880" height="1376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To dispatch an action we use the useDispatch hook from react-redux. dispatch is an action that triggers any reducer function. in this case, we use &lt;code&gt;authActions&lt;/code&gt; to trigger the login reducer function. This will mutate/change &lt;code&gt;isLogin&lt;/code&gt; (which has an initialState of false) to true.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using The New State
&lt;/h4&gt;

&lt;p&gt;Great! we have been able to set up an action that can trigger a state change. How then do we use the new state? we can use useSelector hook for it. useSelector hook is used to select the particular state we need. in this case we need the &lt;code&gt;isLogin&lt;/code&gt; state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XdNADmhL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649449827110/lHvooDsBg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XdNADmhL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649449827110/lHvooDsBg.png" alt="code7.png" width="880" height="1304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why are we returning &lt;code&gt;state.auth.isLogin&lt;/code&gt;?&lt;/strong&gt; state is passed automatically by the useSelector hook, auth(the name we named our slice while configuring the store) is the particular slice isLogin is in and then we select isLogin.&lt;/p&gt;

&lt;p&gt;We used the new state update, In this case, to simply render the text conditionally. if the login is true it shows "you are logged" if not it shows "please login".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o-EF4mWL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649449536775/hTGr2NLqe.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o-EF4mWL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649449536775/hTGr2NLqe.PNG" alt="Capture2.PNG" width="759" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What we just did
&lt;/h3&gt;

&lt;p&gt;Let's look back at what we just did. when we click on the login button it dispatches a particular action which is the login reducer function, redux uses the login reducer function to then mutate the state. after the state is updated/changed the components subscribing or making use of that state are automatically updated with the new state which causes the entire component to rerender.&lt;/p&gt;

&lt;p&gt;we can easily set up the logout function too in a similar way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ysPvIgmj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649408033122/Hq_1va7S6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ysPvIgmj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649408033122/Hq_1va7S6.png" alt="code8.png" width="880" height="1465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uguHL4eL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649408047847/0J_51Nip5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uguHL4eL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649408047847/0J_51Nip5.PNG" alt="Capture3.PNG" width="761" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far we have manually set the state in our store but what if we want to set the state dynamically let's say from a user's input. Our scenario here will be to get the user name from the input field and display it when the user is logged in. How do we do that? we can decide to create a new slice entirely just like we did with auth slice or add a new state variable to the already created auth slice, you can do either you will be right. in this case, we are going to add it to the already created slice- &lt;strong&gt;auth slice&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yj129xAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649449906399/myNdjAfnp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yj129xAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649449906399/myNdjAfnp.png" alt="code9.png" width="880" height="1089"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we go back to our auth slice file and add a new state variable named &lt;code&gt;userName&lt;/code&gt; and set its initial value to an empty string, we then set up the reducer &lt;code&gt;setUserName&lt;/code&gt;. you will notice, unlike the other reducer this particular reducer has two parameters passed in; state and action. &lt;strong&gt;What is action.payload?&lt;/strong&gt; it will be explained in a bit...&lt;/p&gt;

&lt;p&gt;let's go to the app.js file and add a &lt;code&gt;submitHandler&lt;/code&gt; function to the form tag. What we want is after the form is submitted we somehow can pass the user's input value to our state - &lt;code&gt;userName&lt;/code&gt;. That is after the form is submitted we dispatch an action that changes &lt;code&gt;userName&lt;/code&gt; from an empty string to whatever the user's input was. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tFfF85gz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649450339268/aoXLgI2hn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tFfF85gz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649450339268/aoXLgI2hn.png" alt="code11.png" width="880" height="1556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait A Min!?!
&lt;/h3&gt;

&lt;p&gt;Alright, we used javascript to get the user value from the input field. targetInput selects the input field, targetInput.value gives whatever the user's input was.&lt;/p&gt;

&lt;p&gt;You will notice that when we pass our dispatch action this time around we are not just calling it (like the other dispatch actions) we are passing a parameter (targetInput.value) into the function. That is the action.payload. &lt;strong&gt;What is action.payload?&lt;/strong&gt; action payload is equivalent to what was passed into the reducer function when it was dispatched.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rounding up
&lt;/h3&gt;

&lt;p&gt;let's finish strong by fine-tunning our project a bit. You will notice loginHandler and submitHandler are doing the same thing. Let's refactor them by moving the loginHandler code into submitHandler. we can also twerk the UI a bit. when the user logs in we replace that entire form field with just a single button(logout). finally, we can add a guard clause to avoid the user's submission if the input field is empty.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ttf8d4fh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649505842409/D0GLssHUn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ttf8d4fh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649505842409/D0GLssHUn.png" alt="code13.png" width="880" height="1700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And we are Done&lt;/strong&gt; 🥳🥳. Here is a sandbox for what we have done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.toiframe%20src="&gt;" data-card-controls="0" data-card-theme="light"&amp;gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
