<?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: ISIAKA ABDULAHI AKINKUNMI</title>
    <description>The latest articles on DEV Community by ISIAKA ABDULAHI AKINKUNMI (@isiakaabd).</description>
    <link>https://dev.to/isiakaabd</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%2F440180%2Fa71ea812-0b35-48dd-990e-ae25cc72754d.png</url>
      <title>DEV Community: ISIAKA ABDULAHI AKINKUNMI</title>
      <link>https://dev.to/isiakaabd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/isiakaabd"/>
    <language>en</language>
    <item>
      <title>Getting Started With Redux-ToolKit (RTK)</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Sat, 31 Dec 2022 17:08:57 +0000</pubDate>
      <link>https://dev.to/isiakaabd/getting-started-with-redux-tool-kit-rtk-kff</link>
      <guid>https://dev.to/isiakaabd/getting-started-with-redux-tool-kit-rtk-kff</guid>
      <description>&lt;p&gt;Managing state is an important aspect of React, For a smaller project, using useState or useReducer is a good way to manage state. But as the application grows complex, we need an efficient way to handle it, React-Redux provides us with global state management and in this article we will explore the concept of state management using &lt;a href="https://redux-toolkit.js.org" rel="noopener noreferrer"&gt;Redux-ToolKit (RTK)&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Content
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;What is React-Redux?&lt;/li&gt;
&lt;li&gt;Installations&lt;/li&gt;
&lt;li&gt;
Setting Up

&lt;ul&gt;
&lt;li&gt;ConfigureStore&lt;/li&gt;
&lt;li&gt;Wrap our App with Provider from &lt;code&gt;React-Redux&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
CreateSlice

&lt;ul&gt;
&lt;li&gt;
todoSlice.js

&lt;ul&gt;
&lt;li&gt;Add Todo action&lt;/li&gt;
&lt;li&gt;Delete Todo action&lt;/li&gt;
&lt;li&gt;Update Todo action&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

Todo App

&lt;ul&gt;
&lt;li&gt;
Get Initial State &lt;/li&gt;
&lt;li&gt;Add a todo &lt;/li&gt;
&lt;li&gt;Delete a todo &lt;/li&gt;
&lt;li&gt;Update a todo &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;When an action is performed,a state change is expected to happen and trigger the rendering of UI. Managing state  can be done using a &lt;code&gt;useState&lt;/code&gt; or &lt;code&gt;useReducer&lt;/code&gt;, It could be complex especially if those components are located in different parts of the application. Sometimes this can be solved by &lt;code&gt;"lifting state up"&lt;/code&gt; to parent components, but that doesn't always help.&lt;/p&gt;

&lt;p&gt;One way to solve this is to extract the shared &lt;code&gt;state&lt;/code&gt; from the components, and put it into a centralized location outside the component tree where any component can access the &lt;code&gt;state&lt;/code&gt;or trigger actions, no matter where they are in the &lt;code&gt;tree&lt;/code&gt;. This is the first of three articles. Other articles are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=""&gt; Making API calls Efficiently with RTK Query&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href=""&gt;A guide to using Redux-persist&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In those articles, I will explain how to use RTK queries to make API calls in efficient ways rather than installing other &lt;code&gt;packages&lt;/code&gt;, I will also explain redux-persist as an alternative to saving to persist our state or save with details in our App. see you soon.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is React-redux
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;React-Redux&lt;/code&gt; is a popular JavaScript library that helps developers manage the state of their applications. React-Redux provides a way for a React application to communicate with a central store that holds the state of the application. If you are familiar with react-redux, there are several challenges using it as explained by the documentation  ranging from&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Configuring a Redux store is too complicated
2. Installing many Packages
3. Too much boilerplate code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With those challenges, &lt;code&gt;RTK&lt;/code&gt; is meant to solve them and provide a way to write efficient ways to set up  and &lt;code&gt;store&lt;/code&gt; and define &lt;code&gt;reducers&lt;/code&gt;, &lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;For an existing project, run the following code in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&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;/toolkit react-redu&lt;/span&gt;&lt;span class="err"&gt;x
&lt;/span&gt;
&lt;span class="nx"&gt;OR&lt;/span&gt;

&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;add&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;p&gt;For a new project and this tutorial, we  will create an App with redux template, run the code below, our App name is &lt;code&gt;redux-tutorial&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;npx&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="nx"&gt;redux&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tutorial&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="nx"&gt;redux&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;After the installation, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;cd&lt;/span&gt; &lt;span class="nx"&gt;redux&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tutorial&lt;/span&gt;

&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Your &lt;code&gt;package.json&lt;/code&gt; file should like this, &lt;code&gt;redux-toolkit&lt;/code&gt; and &lt;code&gt;react-redux&lt;/code&gt; will be installed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbrkofot6t53s0pgx7e7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbrkofot6t53s0pgx7e7o.png" alt="redux-tool-kit" width="800" height="688"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up
&lt;/h3&gt;

&lt;p&gt;In this Tutorial, we will be building a simple &lt;code&gt;Todo&lt;/code&gt; list to illustrate our work. We need to basically go through three basic step as listed below&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure Store&lt;/li&gt;
&lt;li&gt;
Wrap our App with Provider from react-redux *&lt;/li&gt;
&lt;li&gt;Create Slices&lt;/li&gt;
&lt;li&gt;Dispatch our action and get state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Only necessary if you are using an existing App&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;features&lt;/code&gt; folder, a subfolder of &lt;code&gt;src&lt;/code&gt;, create another folder called &lt;code&gt;todo&lt;/code&gt; where all our configurations will be. For those that installed with &lt;code&gt;redux template&lt;/code&gt;, all configurations have been done. For existing project, create a folder &lt;code&gt;store&lt;/code&gt; and add &lt;code&gt;index.js&lt;/code&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  ConfigureStore
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;configureStore&lt;/code&gt; function is used to create a Redux store that holds the complete state tree of our App. Configure store accepts &lt;code&gt;reducers&lt;/code&gt; as the required parameters, others parameters that can be included are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thunk middleware&lt;/li&gt;
&lt;li&gt;Redux DevTools&lt;/li&gt;
&lt;li&gt;PreloadedState​&lt;/li&gt;
&lt;li&gt;Enhancers​&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets create our &lt;code&gt;store&lt;/code&gt;by importing the following&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/app/store.js&lt;/code&gt; OR &lt;code&gt;./store&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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="s1"&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;import&lt;/span&gt; &lt;span class="nx"&gt;todoReducer&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;features/todo/todoSlice.js&lt;/span&gt;&lt;span class="dl"&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;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todoReducer&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;Here, we import our &lt;code&gt;todoReducer&lt;/code&gt; (which will be created soon). the import was done using absolute path. Read more about absolute path in my article &lt;a href="https://dev.to/isiakaabd/configuring-absolute-path-in-react-projects-4h9o"&gt;here&lt;/a&gt;. We can have multiple &lt;code&gt;reducers&lt;/code&gt; combined together.  later on, we will use &lt;code&gt;todos&lt;/code&gt; as the key to access our state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap our App with Provider from react-redux
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createRoot&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-dom/client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&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-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&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;./app/store&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&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;./App&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="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&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="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StrictMode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.StrictMode&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  CreateSlice
&lt;/h3&gt;

&lt;p&gt;This helps us to write reducers without the need to spread our state. &lt;code&gt;Redux Toolkit&lt;/code&gt; allows us to write "mutating" logic in reducers. It doesn't actually mutate the state because it uses the &lt;a href="https://redux-toolkit.js.org/usage/immer-reducers" rel="noopener noreferrer"&gt;Immer library&lt;/a&gt;, It also automatically generates action creator functions for each reducer. It literarily saves us with writing our &lt;code&gt;actions&lt;/code&gt; type logic repetitively.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;features&lt;/code&gt; folder, create another folder &lt;code&gt;todo&lt;/code&gt; and add &lt;code&gt;todoSlice.js&lt;/code&gt; where we write our &lt;code&gt;todoSlice&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  todoSlice.js
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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="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="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;todoList&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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;todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;initialState&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;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;todoSlice&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="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;actions&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;reducer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is a &lt;code&gt;boilerplate&lt;/code&gt; for writing our slices, in our &lt;code&gt;createSlice&lt;/code&gt; function, we passed in our name, the initial State and our reducers.&lt;/p&gt;

&lt;h5&gt;
  
  
  Actions
&lt;/h5&gt;

&lt;p&gt;let's write our first action,  &lt;code&gt;addTodo&lt;/code&gt; add a new todo to the list and we create it  in   &lt;code&gt;reducers&lt;/code&gt; object as below&lt;/p&gt;

&lt;h6&gt;
  
  
  Add Todo
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;addTodo&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;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="nx"&gt;payload&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;Here we are using the &lt;code&gt;push&lt;/code&gt; method to add new todo to our list. &lt;code&gt;action.payload&lt;/code&gt; is the &lt;code&gt;todo&lt;/code&gt; that will be added to &lt;code&gt;todoList&lt;/code&gt;.&lt;/p&gt;

&lt;h6&gt;
  
  
  Delete Todo
&lt;/h6&gt;

&lt;p&gt;To be able to delete from the list, we use the &lt;code&gt;index&lt;/code&gt; to select a particular &lt;code&gt;todo&lt;/code&gt; and use the array method of &lt;code&gt;splice&lt;/code&gt;. here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;deleteTodo&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;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&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;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&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="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1&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;h6&gt;
  
  
  Update Todo
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
 &lt;span class="nx"&gt;updateTodo&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&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;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findIndex&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="nx"&gt;payload&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;]&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can export all our &lt;code&gt;actions&lt;/code&gt; and our final code like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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="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="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;todoList&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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;todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;initialState&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;addTodo&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;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="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="na"&gt;deleteTodo&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;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&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;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&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="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;updateTodo&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&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;todoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findIndex&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="nx"&gt;payload&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;]&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="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;todoSlice&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;addTodo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;actions&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;reducer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;With this we concluded writing on &lt;code&gt;redux&lt;/code&gt; logic, last thing we need to do is to import it in our App.&lt;/p&gt;

&lt;h3&gt;
  
  
  useDispatch and useSelector
&lt;/h3&gt;

&lt;p&gt;The two hooks work to get &lt;code&gt;set&lt;/code&gt; and &lt;code&gt;get&lt;/code&gt; data from our store. if we need to modify the state, &lt;code&gt;useDispatch&lt;/code&gt; will be used to dispatch an &lt;code&gt;action&lt;/code&gt; while if we need to access our state, &lt;code&gt;useSelector&lt;/code&gt; will be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Todo App
&lt;/h3&gt;

&lt;p&gt;For this, we created our &lt;code&gt;Todo component&lt;/code&gt; in the root of our &lt;code&gt;src&lt;/code&gt; folder, our folder structure should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fyy4xzmt0hhq5doezwjz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fyy4xzmt0hhq5doezwjz5.png" alt="folder_structure" width="271" height="805"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We create a simple UI and you can have access to the code in the linked &lt;a href="https://github.com/isiakaabd/react-redux-tutorial.git" rel="noopener noreferrer"&gt;repository&lt;/a&gt;.. Here is the code in our &lt;code&gt;Todo.js&lt;/code&gt;  file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="nx"&gt;useSelector&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-redux&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="nx"&gt;Todo&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTodo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&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="o"&gt;=&amp;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;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoList&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Simple&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input__container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setTodo&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="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Add&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ol&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;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
          &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h6&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;todo&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h6&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;edit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Edit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delete&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span class="nx"&gt;Delete&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ol&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Todo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;The first thing we do here is to get the current state of our &lt;code&gt;todoList&lt;/code&gt; using &lt;code&gt;useSelector&lt;/code&gt; hook, we have&lt;/p&gt;

&lt;h5&gt;
  
  
  Get Initial State
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&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="o"&gt;=&amp;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;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todoList&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;todos&lt;/code&gt; here refer to the name of the &lt;code&gt;reducer&lt;/code&gt; we used when configuring our store. So we have access to our &lt;code&gt;todoList&lt;/code&gt; array which contains our &lt;code&gt;initialState&lt;/code&gt; and if we log it to &lt;code&gt;console&lt;/code&gt;, we have&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdxuwogyrtzps7kmpcal9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdxuwogyrtzps7kmpcal9.png" alt="console.image" width="800" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Add a Todo
&lt;/h5&gt;

&lt;p&gt;To add to the list, we need to &lt;code&gt;dispatch&lt;/code&gt; our &lt;code&gt;addTodo&lt;/code&gt; action as shown below. we will use &lt;code&gt;useDispatch hook&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first we call the useDispatch and assigned it to dispatch variable.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDispatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We dispatch our action when we clicked the button, with our &lt;code&gt;todo&lt;/code&gt; as a paramater as shown below. we checked if there's &lt;code&gt;todo&lt;/code&gt; before we dispatch our action and clear our input field afterwards
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;input&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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;addTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                 &lt;span class="nf"&gt;setInput&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="p"&gt;}}&lt;/span&gt;
     &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nx"&gt;Add&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;on the click of Add Todo &lt;code&gt;button&lt;/code&gt;, we have something similar to this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2cp0l6ijomae0vmz9cdh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2cp0l6ijomae0vmz9cdh.png" alt="Added todolist" width="724" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Delete a todo
&lt;/h5&gt;

&lt;p&gt;Similar to what we did for &lt;code&gt;addTodo&lt;/code&gt;, we will pass the &lt;code&gt;index&lt;/code&gt; a todo and dispatch our &lt;code&gt;deleteTodo&lt;/code&gt; action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;addTodo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteTodo&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;features/todo/todoSlice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In our &lt;code&gt;edit button&lt;/code&gt;, we add the code to set our todo to the input value, here is how our button code looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delete&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;deleteTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&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;Delete&lt;/span&gt;
 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Update a todo
&lt;/h5&gt;

&lt;p&gt;Here , we need to select a particular todo item and make update to it, full details to the implementation is available in the &lt;a href="https://github.com/isiakaabd/react-redux-tutorial.git" rel="noopener noreferrer"&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading. Please leave a comment if you have any question or clarification and I hope you've learned something new from this post. Want to stay up to date with regular content regarding JavaScript, React and React-Native? Follow me ❤️ on &lt;a href="https://linkedin.com/in/isiakaabd" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>redux</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Configuring Absolute path in React Projects</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Sat, 24 Dec 2022 10:38:01 +0000</pubDate>
      <link>https://dev.to/isiakaabd/configuring-absolute-path-in-react-projects-4h9o</link>
      <guid>https://dev.to/isiakaabd/configuring-absolute-path-in-react-projects-4h9o</guid>
      <description>&lt;p&gt;Recently, I worked on a legacy codebase and looking into it I found out the imports were done using &lt;code&gt;relative&lt;/code&gt; path, It was difficult to comprehend and also show errors when trying to copy the code from one file to another folder, thereby spending more time to resolve the import errors. In this article, I will explain how to use &lt;code&gt;absolute&lt;/code&gt; path in your &lt;code&gt;CRA&lt;/code&gt;, &lt;code&gt;Vite&lt;/code&gt; and &lt;code&gt;React-Native&lt;/code&gt; projects&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fm3swfr41y3lrx4mh1dhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm3swfr41y3lrx4mh1dhy.png" alt="Legacy Upload" width="800" height="227"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;strong&gt;Table Of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; What is jsconfig.json ? &lt;/li&gt;
&lt;li&gt; Configuring Absolute path in:

&lt;ul&gt;
&lt;li&gt; Create React App &lt;/li&gt;
&lt;li&gt; Vite &lt;/li&gt;
&lt;li&gt; React-Native &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What is jsconfig.json?
&lt;/h3&gt;

&lt;p&gt;The presence of &lt;code&gt;jsconfig.json&lt;/code&gt; file in a directory indicates that the directory is the root of a &lt;code&gt;JavaScript&lt;/code&gt; Project. In &lt;code&gt;jsconfig.json&lt;/code&gt;, files belonging to the project and the files to be excluded from the project can be listed.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to Configure Absolute Path in Create-React-App
&lt;/h3&gt;

&lt;p&gt;In the root of our folder, where we have &lt;code&gt;package.json&lt;/code&gt; file, create a file called &lt;code&gt;jsconfig.json&lt;/code&gt;. Add the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;compilerOptions&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;baseUrl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;include&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;exclude&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node_modules,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;]
}

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

&lt;/div&gt;



&lt;p&gt;👉🏼 By default the JavaScript language service will analyze and provide IntelliSense for all files in your JavaScript project. Its a good practise to exclude &lt;code&gt;node_modulues&lt;/code&gt; , &lt;code&gt;build&lt;/code&gt; , &lt;code&gt;dist&lt;/code&gt; etc to speed up IntelliSense.  If you do not have a &lt;code&gt;jsconfig.json&lt;/code&gt; in your workspace, VS Code will by default exclude the &lt;code&gt;node_modules&lt;/code&gt; folder for you.&lt;/p&gt;

&lt;p&gt;👉🏼 After adding the file, &lt;strong&gt;You will need to restart your server for this to be effective&lt;/strong&gt; and we can reference the folders inside our &lt;code&gt;src&lt;/code&gt; without error as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frtf6gysuti0xt6xnln92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frtf6gysuti0xt6xnln92.png" alt="import_files" width="800" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Relative Path Configuration in Vite
&lt;/h3&gt;

&lt;p&gt;👉🏼 Here, we need to install a dependency &lt;code&gt;vite-jsconfig-paths&lt;/code&gt; which gives &lt;code&gt;vite&lt;/code&gt; the ability to resolve imports using &lt;code&gt;jsconfig.json&lt;/code&gt; path mapping. open your terminal and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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="nx"&gt;vite&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;jsconfig&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;paths&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;D&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;👉🏼 After installation, we inject it into our vite app by adding it to our &lt;code&gt;vite.config.js&lt;/code&gt; file as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;defineConfig&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;vite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;react&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;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;jsconfigPaths&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;vite-jsconfig-paths&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="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;jsconfigPaths&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;👉🏼 After that, we create &lt;code&gt;jsconfig.json&lt;/code&gt; file as shown in the react demonstration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Relative Path configuration In React-Native
&lt;/h3&gt;

&lt;p&gt;👉🏼 For this, we use  &lt;code&gt;babel-plugin-root-import&lt;/code&gt; library. lets install the package as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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="nx"&gt;babel&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;plugin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;D&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;👉🏼 After installation, add the dependency to your  &lt;code&gt;babel.config.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;👉🏼 Custom rootPathPrefix: By default, the import will be relative to the working directory of the process running babel. we can customized by using &lt;code&gt;~&lt;/code&gt;, &lt;code&gt;@&lt;/code&gt; or any other symbol. I will be using the &lt;code&gt;@&lt;/code&gt; and our &lt;code&gt;babel.config.js&lt;/code&gt; looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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="na"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;babel-preset-expo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;plugins&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;babel-plugin-root-import&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="na"&gt;rootPathPrefix&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;rootPathSuffix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;src&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;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;👉🏼 If you don't like the &lt;code&gt;@&lt;/code&gt; syntax you can use your own symbol (for example an &lt;code&gt;#&lt;/code&gt; symbol or &lt;code&gt;\&lt;/code&gt; or anything you want).&lt;/p&gt;

&lt;p&gt;👉🏼 It is now possible to import your files using &lt;code&gt;"@/"&lt;/code&gt; as a prefix. Here's an example below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw1czbjtjh5k6lq24dpdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw1czbjtjh5k6lq24dpdj.png" alt="uploading images without config" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fchtgmv3i5qwmjyno5c15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fchtgmv3i5qwmjyno5c15.png" alt="After Adding config" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Important Notice: After making the necessary changes in &lt;code&gt;babel.config.js&lt;/code&gt;, Don't forget to restart your server.
&lt;/h3&gt;

&lt;p&gt;Thank you for reading. I hope you've learned something new from this post. Want to stay up to date with regular content regarding &lt;strong&gt;JavaScript&lt;/strong&gt;, &lt;strong&gt;React&lt;/strong&gt;? Follow me ❤️ on  &lt;a href="https://linkedin.com/in/isiakaabd" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Beginner Guide on Unit Testing in React using React Testing Library and Vitest</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Sun, 18 Dec 2022 20:52:23 +0000</pubDate>
      <link>https://dev.to/isiakaabd/beginner-guide-on-unit-testing-in-react-using-react-testing-library-and-vitest-4ifp</link>
      <guid>https://dev.to/isiakaabd/beginner-guide-on-unit-testing-in-react-using-react-testing-library-and-vitest-4ifp</guid>
      <description>&lt;p&gt;As Application grows bigger and more complex, manual testing becomes time-consuming, difficult to achieve its purpose and prone to error as it might be difficult to notice the wee change that can break the functionalities. In this article, I'd like to explain the concept of unit testing, I expect you have a basic understanding of &lt;code&gt;React&lt;/code&gt; concepts like &lt;code&gt;components&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;lets get started💧&lt;/p&gt;

&lt;h3&gt;
  
  
  Table Of Content
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
What is Unit Testing ? &lt;/li&gt;
&lt;li&gt;
Purpose Of Unit Testing &lt;/li&gt;
&lt;li&gt;
What is React Testing Library ? &lt;/li&gt;
&lt;li&gt;
Setting React App With Vite &lt;/li&gt;
&lt;li&gt;
Vitest 

&lt;ul&gt;
&lt;li&gt;
Why use Vitest? &lt;/li&gt;
&lt;li&gt;
Stages of Testing &lt;/li&gt;
&lt;li&gt;
Write Your First Unit Test &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Unit Testing?
&lt;/h3&gt;

&lt;p&gt;Unit tests are tests focused on individual &lt;code&gt;component&lt;/code&gt;, function modules called &lt;code&gt;unit&lt;/code&gt;. It isolates the particular unit and &lt;code&gt;test&lt;/code&gt; it separately to ensure it works as expected. For &lt;code&gt;React&lt;/code&gt; &lt;code&gt;components&lt;/code&gt;, it could mean checking if the &lt;code&gt;component&lt;/code&gt; renders correctly for a specified prop.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The more your tests resemble the way your software is used, the more confidence they can give you.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Purpose Of Unit Testing
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved code quality and reliability&lt;/strong&gt;: Unit testing ensures that individual components and functions in a &lt;code&gt;React&lt;/code&gt; application are working as intended, reducing the chances of bugs and errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier debugging and maintenance&lt;/strong&gt;: With unit tests in place, it is easier to identify and fix any code issues and make modifications and updates without breaking the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better collaboration and communication among team members&lt;/strong&gt;: Unit tests provide clear documentation of each &lt;code&gt;component's&lt;/code&gt; expected behavior and functionality, enabling team members to understand and work on the code more effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced user experience&lt;/strong&gt;: By thoroughly testing each &lt;code&gt;component&lt;/code&gt; and &lt;code&gt;function&lt;/code&gt;, &lt;code&gt;unit tests&lt;/code&gt; ensure that the user experience is consistent and seamless, resulting in a better overall product.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Faster development and deployment&lt;/strong&gt;: With the ability to quickly identify and fix issues, unit testing allows for faster development and deployment of &lt;code&gt;React&lt;/code&gt; applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is React Testing Library?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://testing-library.com" rel="noopener noreferrer"&gt;&lt;strong&gt;React Testing Library&lt;/strong&gt;&lt;/a&gt; &lt;code&gt;RTL&lt;/code&gt; is a lightweight solution for testing web pages by querying and interacting with DOM nodes.&lt;br&gt;
&lt;code&gt;RTL&lt;/code&gt; is a popular testing library for &lt;code&gt;React&lt;/code&gt; applications that helps developers write reliable tests for their components. It is designed to encourage writing tests that are easy to read and understand, which makes it a great tool for teams working on large React projects.&lt;/p&gt;

&lt;p&gt;One of the key features of &lt;code&gt;react-testing-library&lt;/code&gt; is its focus on testing the behavior of a &lt;code&gt;component&lt;/code&gt; rather than its implementation. This means that tests written with &lt;code&gt;react-testing-library&lt;/code&gt; are less likely to break when the implementation of a &lt;code&gt;component&lt;/code&gt; changes, which can save developers a lot of time and effort.&lt;br&gt;
The library provides a set of utility functions for interacting with &lt;code&gt;React components&lt;/code&gt; in a way that simulates how a user would interact with them. &lt;/p&gt;
&lt;h3&gt;
  
  
  Setting React App With Vite
&lt;/h3&gt;

&lt;p&gt;To set up our application,  we will be using &lt;code&gt;vite&lt;/code&gt; as an alternative to &lt;code&gt;create-react-app&lt;/code&gt;, &lt;code&gt;Vite&lt;/code&gt; is a lightweight and minimalistic tool, providing only the essential features and tools needed for &lt;code&gt;React&lt;/code&gt; development.&lt;/p&gt;

&lt;p&gt;To create a &lt;code&gt;vite&lt;/code&gt; project, run the following command in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest

OR

yarn create vite

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

&lt;/div&gt;



&lt;p&gt;Then follow the prompts to  input project name, framework and variant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For project name, we can use &lt;code&gt;react-testing-project&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For framework, select &lt;code&gt;React&lt;/code&gt; option&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;variant&lt;/code&gt; select &lt;code&gt;javascript&lt;/code&gt; from the available options&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After All these, run 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;
cd react-testing-project
npm install

OR

cd react-testing-project
yarn install

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

&lt;/div&gt;



&lt;p&gt;After these steps, open it in your favorite &lt;code&gt;IDE&lt;/code&gt; or run&lt;br&gt;
&lt;code&gt;code .&lt;/code&gt; to open in default &lt;code&gt;IDE&lt;/code&gt; and start the server with the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev

OR

yarn run dev

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

&lt;/div&gt;



&lt;p&gt;Open the URL in your browser and it should display something similar here&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0dtpf38l6ug1llxqg8tn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0dtpf38l6ug1llxqg8tn.png" alt="image of VS Code" width="800" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Vitest
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Vitest&lt;/code&gt; is a blazing-fast unit test framework powered by &lt;code&gt;vite&lt;/code&gt;. It aims to position itself as the rest runner of choice for &lt;code&gt;vite&lt;/code&gt; projects, and as a solid alternative even for projects not using &lt;code&gt;vite&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use Vitest?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Shared configuration with &lt;code&gt;vite&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Vitest&lt;/code&gt; supports &lt;code&gt;HMR&lt;/code&gt; (Hot Module Reloading), which speeds up your workflow. With &lt;code&gt;HMR&lt;/code&gt;, only the changes are updated on the server, and the server reflects the new changes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Vitest&lt;/code&gt; is relatively simple to use and doesn't require complex configuration. This makes it easier to get started with testing your components.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Jest&lt;/code&gt; Snapshot support.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stages of Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Setup&lt;/strong&gt;: Before you can test an element, you will need to set up your testing environment. This typically involves installing the testing library and any dependencies, creating a test file, and importing the necessary components and utilities from the library.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rendering&lt;/strong&gt;: To test an element, you will need to render it in a testing environment. You can use the render function from the testing library to do this, which will return an object containing the rendered element and various utility functions for interacting with it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Querying&lt;/strong&gt;: Once you have rendered the element, you can use the query functions provided by the testing library (such as &lt;code&gt;getByText&lt;/code&gt; or &lt;code&gt;getByLabelText&lt;/code&gt;) to locate specific elements within the rendered tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interacting&lt;/strong&gt;: After you have located the element you want to test, you can use the utility functions provided by the testing library (such as &lt;code&gt;fireEvent&lt;/code&gt; or &lt;code&gt;userEvent&lt;/code&gt;) to simulate user interactions with the element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Assertion&lt;/strong&gt;: Finally, you can use the assertion functions provided by the testing library (such as expect or assert) to verify that the element is behaving as expected in response to the simulated interactions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Setup&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Install &lt;code&gt;vitest&lt;/code&gt; and &lt;code&gt;jsdom&lt;/code&gt; as dev dependencies using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm i -D vitest jsdom

OR

yarn add -D vitest jsdom

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

&lt;/div&gt;



&lt;p&gt;Also the following also&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @testing-library/jest-dom @testing-library/react

OR

yarn add @testing-library/jest-dom @testing-library/react

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

&lt;/div&gt;



&lt;p&gt;After all installations are successful, we need to add a test script in our &lt;code&gt;package.json&lt;/code&gt; file 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;test: "vitest"

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

&lt;/div&gt;



&lt;p&gt;So, our &lt;code&gt;package.json&lt;/code&gt; file, look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqccpan7qnqbhj6s5vqaz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqccpan7qnqbhj6s5vqaz.png" alt="Package.json file" width="547" height="789"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;src&lt;/code&gt; folder, create a &lt;code&gt;setupTests.js&lt;/code&gt; and  add the following code&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import "@testing-library/jest-dom/extend-expect&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also in our &lt;code&gt;vite.config.js&lt;/code&gt; add a test object to the config like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "src/setupTests.js",
  },

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

&lt;/div&gt;



&lt;p&gt;so, our &lt;code&gt;vite.config.js&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0qe7m60g1j3cnlgn572h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0qe7m60g1j3cnlgn572h.png" alt="vite.config.js file" width="661" height="664"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are done with installation and configuration, last thing left before running our test is to create &lt;code&gt;__tests__&lt;/code&gt; folder in &lt;code&gt;src&lt;/code&gt; folder. we will also create &lt;code&gt;App.test.js&lt;/code&gt; in &lt;code&gt;__tests_&lt;/code&gt; folder where we are to write all our codes, our file structure should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Falywr5zitzjv43gsi0f5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Falywr5zitzjv43gsi0f5.png" alt="file layout" width="310" height="570"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Querying&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before writing our test, we should get acquainted with several methods that allow us to locate elements based on their text content, display properties, or other attributes on the page. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To select a single element, you can use the &lt;code&gt;getBy*&lt;/code&gt;, &lt;code&gt;findBy*&lt;/code&gt;, or &lt;code&gt;queryBy*&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To select multiple  elements, you can use the &lt;code&gt;getAllBy*&lt;/code&gt;, &lt;code&gt;findAllBy*&lt;/code&gt; or &lt;code&gt;queryAllBy*&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;queryBy*&lt;/code&gt;: These element selectors allow you to search for an element in your component, but don't expect the element to be present. They will return the element if it is found or return null if the element is not found. These element selectors are useful when you want to check for the presence of an element, but don't want the query to throw an error if the element is not found.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;getBy*&lt;/code&gt;: These element selectors allow you to synchronously search for an element in your component. They will return the element if it is found or throw an error if the element is not found. These element selectors are useful when you expect the element to be present in the DOM at the time the query is executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;findBy*: These element selectors allow you to asynchronously search for an element in your component. They will return a promise that resolves to the element when it becomes available or rejects it with an error if the element is not found. These element selectors are useful when you need to wait for an element to appear in the DOM before interacting with it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;queryAllBy*&lt;/code&gt;: These element selectors allow you to search for multiple elements in your component, but don't expect the elements to be present. They will return an array of elements if any elements are found, or return an empty array if no elements are found. These element selectors are useful when you want to check for the presence of multiple elements, but don't want the query to throw an error if no elements are found.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;getAllBy*&lt;/code&gt;: These element selectors allow you to synchronously search for multiple elements in your component. They will return an array of elements if any elements are found, or throw an error if no elements are found. These element selectors are useful when you expect multiple elements to be present in the DOM at the time the query is executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;findAllBy*&lt;/code&gt;: These element selectors allow you to asynchronously search for multiple elements in your component. They will return a promise that resolves to an array of elements when the elements become available, or rejects with an error if no elements are found. These element selectors are useful when you need to wait for multiple elements to appear in the DOM before interacting with them.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more about the configuration  &lt;a href="https://testing-library.com/docs/react-testing-library/intro/" rel="noopener noreferrer"&gt;here&lt;/a&gt;  &lt;/p&gt;

&lt;h3&gt;
  
  
  Write Your First Unit Test
&lt;/h3&gt;

&lt;p&gt;In Our &lt;code&gt;App.jsx&lt;/code&gt; file, we have some code there that we can write a basic test to debug our screen and check if some elements are present , &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rendering&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;lets debug our screen, copy and paste the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import App from "../App";
import { it, describe } from "vitest";
import { render, screen } from "@testing-library/react";


describe("App.js", () =&amp;gt; {
  it("Check if the App render very well", () =&amp;gt; {
    //render our App properly
    render(&amp;lt;App /&amp;gt;);
    screen.debug();
  });
});

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

&lt;/div&gt;



&lt;p&gt;Here, we import our &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt; which is to be tested, Also screen and render are imported to be able to interact with the component. &lt;code&gt;describe&lt;/code&gt; method is used to organize and structure tests, and it typically takes a string parameter that describes the group of tests being defined and a &lt;code&gt;callback function&lt;/code&gt; that contains the actual tests. In this example, the string argument, "Check if the App render very well", provides a label for the group of tests, and the &lt;code&gt;callback function&lt;/code&gt; contains the actual tests that are being run. The &lt;code&gt;render&lt;/code&gt; method is used to render a given &lt;code&gt;React&lt;/code&gt; component and return an object that provides several utility functions for interacting with the rendered component. &lt;code&gt;screen.debug&lt;/code&gt; method is a utility function that allows you to print the current state of the rendered component to the &lt;code&gt;console&lt;/code&gt;. This can be useful when writing or debugging tests.&lt;/p&gt;

&lt;p&gt;lets run the test by entering the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run test

OR

yarn run test

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

&lt;/div&gt;



&lt;p&gt;After running the test, it renders our &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt; in the terminal as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fj8mw0fbjxkkwtbqcmlnq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj8mw0fbjxkkwtbqcmlnq.png" alt="result of test" width="800" height="801"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assertion&lt;/strong&gt;
lets check if &lt;code&gt;button&lt;/code&gt; and &lt;code&gt;heading&lt;/code&gt; elements are in our component will the following code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import App from "../App";
import { it, describe } from "vitest";
import { render, screen } from "@testing-library/react";

describe("App.js", () =&amp;gt; {
  it("Check if the button is in the document", () =&amp;gt; {
    render(&amp;lt;App /&amp;gt;);
    const button = screen.getByRole("button");
    expect(button).toBeInTheDocument();
  });

  it("check if h1 element is in the document", () =&amp;gt; {
    render(&amp;lt;App /&amp;gt;);
    const h1 = screen.getByRole("heading", { level: 1 });
    expect(h1).toBeInTheDocument();
  });
});

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

&lt;/div&gt;



&lt;p&gt;Here, we are having two tests that render our &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt; , in &lt;code&gt;App.jsx&lt;/code&gt; , I added an &lt;code&gt;attribute&lt;/code&gt; of &lt;code&gt;role&lt;/code&gt; with the value of &lt;code&gt;button&lt;/code&gt; to easily find the element, &lt;code&gt;getByRole&lt;/code&gt; method accepts a second parameter which we used to indicate the heading is &lt;code&gt;h1&lt;/code&gt;. &lt;code&gt;expect&lt;/code&gt; is used to define the expected behaviour of a test and to verify that the actual behaviour matches the expected behaviour. in our two tests, we expect both elements to be in the document. below is what happens when we  run our test &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqp47bawrr5zk494dlj7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqp47bawrr5zk494dlj7d.png" alt="expected result on test" width="568" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Interaction&lt;/strong&gt; 
In this test, we will test when a user interact with element such as &lt;code&gt;click&lt;/code&gt; event and check the &lt;code&gt;state&lt;/code&gt; change as shown below
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import App from "../App";
import { it, describe,  } from "vitest";
import { render, screen } from "@testing-library/react";

describe("App.js", () =&amp;gt; {
  it("Check if the button is in the document", () =&amp;gt; {
    render(&amp;lt;App /&amp;gt;);
    const button = screen.getByRole("button");
    expect(button).toBeInTheDocument();
  });

  it("check if h1 element is in the document", () =&amp;gt; {
    render(&amp;lt;App /&amp;gt;);
    const h1 = screen.getByRole("heading", { level: 1 });
    expect(h1).toBeInTheDocument();
  });
  it("Check if the current value renders when click", () =&amp;gt; {
    render(&amp;lt;App /&amp;gt;);
    const h1 = screen.getByRole("heading", { level: 1 });
    expect(h1.textContent).toBe("0");
  });
});

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

&lt;/div&gt;



&lt;p&gt;since we render &lt;code&gt;App.jsx&lt;/code&gt; in the three test, we can refactor our code and keep it &lt;code&gt;DRY&lt;/code&gt; by using another method &lt;code&gt;beforeEach&lt;/code&gt;, which runs before each test, so our updated test code becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import App from "../App";
import { it, describe, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";

beforeEach(() =&amp;gt; {
  render(&amp;lt;App /&amp;gt;);
});

describe("App.js", () =&amp;gt; {
  it("Check if the button is in the document", () =&amp;gt; {
    const button = screen.getByRole("button");
    expect(button).toBeInTheDocument();
  });

  it("check if h1 element is in the document", () =&amp;gt; {
    const h1 = screen.getByRole("heading", { level: 1 });
    expect(h1).toBeInTheDocument();
  });
  it("Check if the current value renders when click", () =&amp;gt; {
    const h1 = screen.getByRole("heading", { level: 1 });
    expect(h1.textContent).toBe("0");
  });
});

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

&lt;/div&gt;



&lt;p&gt;With this, Our code becomes neater and more readable, our test is expected to run successfully.&lt;/p&gt;

&lt;p&gt;I made a change to &lt;code&gt;App.jsx&lt;/code&gt; file, by putting our state in the &lt;code&gt;h1&lt;/code&gt; element. as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7bk5psykjhv58hnr17ai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7bk5psykjhv58hnr17ai.png" alt="updated App.js file" width="800" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To simulate the click event, I added the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it("Check if the  value increases when button is clicked", async () =&amp;gt; {
    const button = screen.getByRole("button");
    await userEvent.click(button);
    const h1 = screen.queryByRole("heading", { level: 1 });
    expect(h1.textContent).toBe("4");
  });

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

&lt;/div&gt;



&lt;p&gt;we use  &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; when testing asynchronous events such as &lt;code&gt;click&lt;/code&gt;, because it ensures that the test will wait for the &lt;code&gt;click&lt;/code&gt; event to complete before moving on to the next step. running this test will definitely fail because the expected value when clicked is &lt;code&gt;1&lt;/code&gt; instead of &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1t7mt36bu7mfmchbxw23.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1t7mt36bu7mfmchbxw23.png" alt="failed test result" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if we change the value from &lt;code&gt;4&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;, the test runs as expected as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdciv0mfbogjnq28sne04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdciv0mfbogjnq28sne04.png" alt="successfully test" width="556" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading. I hope you've learned something new from this post. The code can be found in this &lt;a href="https://github.com/isiakaabd/react-unit-tests-example" rel="noopener noreferrer"&gt;repository&lt;/a&gt; Want to stay up to date with regular content regarding JavaScript, React? Follow me on  &lt;a href="https://linkedin.com/in/isiakaabd" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>InnerHTML vs InnerText</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Fri, 16 Dec 2022 11:51:49 +0000</pubDate>
      <link>https://dev.to/isiakaabd/innerhtml-vs-innertext-52ma</link>
      <guid>https://dev.to/isiakaabd/innerhtml-vs-innertext-52ma</guid>
      <description>&lt;p&gt;When trying to set the property of elements in Javascript, there are several similar ways of doing it, in this article I will explain what &lt;code&gt;innerHTML&lt;/code&gt; and &lt;code&gt;innerText&lt;/code&gt; are all about and their differences. Let's get started!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's InnerHTML ?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;innerHTML&lt;/code&gt; is the property of an element that set or get the &lt;code&gt;HTML&lt;/code&gt; markup contained within an element. It contains &lt;code&gt;HTML&lt;/code&gt; tags like &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; ,&lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, spacing and formatting. Let's create a &lt;code&gt;p&lt;/code&gt; element with some content as shown below&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;p id="p"&amp;gt; This is an example of &amp;lt;strong&amp;gt;innerHTMl  &lt;br&gt;
 &amp;lt;/strong&amp;gt;text &amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Javascript&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;let element = document.getElementById("p").innerHTML;

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

&lt;/div&gt;



&lt;p&gt;if we &lt;code&gt;log&lt;/code&gt; the value to &lt;code&gt;console&lt;/code&gt; we have &lt;/p&gt;

&lt;p&gt;&lt;code&gt;"This is an example of &amp;lt;strong&amp;gt;innerHTMl  &amp;lt;/strong&amp;gt; text"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Everything in between the &lt;code&gt;p&lt;/code&gt; element is shown without formatting and spacing.&lt;/p&gt;

&lt;p&gt;Setting the value of an element's &lt;code&gt;innerHTML&lt;/code&gt; removes all of its descendants and replaces them with &lt;code&gt;elements&lt;/code&gt; constructed by parsing the &lt;code&gt;HTML&lt;/code&gt; given in the string. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;element = "This has been changed"&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Whenever you add, append, delete or modify contents on a webpage using &lt;code&gt;innerHTML&lt;/code&gt;, all contents are replaced, also all the DOM nodes inside that element are reparsed and recreated, thereby making the process slow.&lt;/p&gt;

&lt;p&gt;The use of &lt;code&gt;innerHTML&lt;/code&gt; also creates a potential security risk for your website. Malicious users can use &lt;code&gt;cross-site scripting (XSS)&lt;/code&gt; to add malicious client-side scripts that steal private user information stored in session cookies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;innerText&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;innerText&lt;/code&gt; property returns the rendered text content of an element that is visible in the browser. It doesn't return the content of  &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; elements, It also neglects the spacing unlike &lt;code&gt;innerHTML&lt;/code&gt;. &lt;code&gt;innerText&lt;/code&gt; cannot access elements If elements are hidden by CSS properties like visibility and display.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;p id="p"&amp;gt; This is an example of &amp;lt;strong&amp;gt;innerText  &lt;br&gt;
 &amp;lt;/strong&amp;gt;text &amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Javascript&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;let element = document.getElementById("p").innerText;

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

&lt;/div&gt;



&lt;p&gt;in the console, we have &lt;code&gt;This is an example of innerText text&lt;/code&gt; in the log.&lt;/p&gt;

&lt;p&gt;When you set the &lt;code&gt;innerText&lt;/code&gt; property, all child &lt;code&gt;nodes&lt;/code&gt; are removed and replaced by only one new text node. &lt;code&gt;innerText&lt;/code&gt; is what you get when a user copies and pastes the content of an element. &lt;code&gt;innerText&lt;/code&gt; is best used when you need to retrieve the content of an element in plain text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for Reading.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How To Create Custom Input Components With Formik, yup and Material-UI</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Wed, 07 Dec 2022 12:07:03 +0000</pubDate>
      <link>https://dev.to/isiakaabd/how-to-create-custom-input-components-with-formik-yup-and-material-ui-4oh9</link>
      <guid>https://dev.to/isiakaabd/how-to-create-custom-input-components-with-formik-yup-and-material-ui-4oh9</guid>
      <description>&lt;p&gt;Components are building blocks for our UIs and with React, reusable, clean and efficient codes are easy. In this tutorial, we will learn how to create custom input field components with React, &lt;a href="https://mui.com" rel="noopener noreferrer"&gt;material-ui&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/yup" rel="noopener noreferrer"&gt;yup&lt;/a&gt;. We will use &lt;code&gt;material-ui&lt;/code&gt; components in the &lt;code&gt;Formik Field&lt;/code&gt; and implement validations using &lt;code&gt;yup&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Forms are great features that are common in many web applications, creating a custom component with a popular &lt;code&gt;Javascript&lt;/code&gt; library like &lt;code&gt;React&lt;/code&gt; and &lt;code&gt;formik&lt;/code&gt; is a straightforward process with decent functionalities and error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets create a new project using  &lt;code&gt;vite&lt;/code&gt; command-line tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="nx"&gt;vite&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;latest&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This will prompt us to select the project Name( for this tutorial we used Formik-Input-component), Package name (formik-input-component), framework( React) and variant (JavaScript).&lt;br&gt;
After all, we run the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;cd&lt;/span&gt; &lt;span class="nx"&gt;Formik&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;
  &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt;
  &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;dev&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Attached is the screenshot of the process:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9r7p898ldzlc2p0vybtl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9r7p898ldzlc2p0vybtl.png" alt="image showing the installation of react " width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we will install &lt;code&gt;formik&lt;/code&gt;, &lt;code&gt;yup&lt;/code&gt;, and &lt;code&gt;material ui&lt;/code&gt; packages using npm or yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;mui&lt;/span&gt;&lt;span class="sr"&gt;/material @emotion/&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;emotion&lt;/span&gt;&lt;span class="sr"&gt;/styled formik yu&lt;/span&gt;&lt;span class="err"&gt;p
&lt;/span&gt;
&lt;span class="nx"&gt;OR&lt;/span&gt;

&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;mui&lt;/span&gt;&lt;span class="sr"&gt;/material @emotion/&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;emotion&lt;/span&gt;&lt;span class="sr"&gt;/styled formik yu&lt;/span&gt;&lt;span class="err"&gt;p
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the packages are installed successfully, we can create a folder called &lt;code&gt;formikControl&lt;/code&gt; where all our files will be saved. In the &lt;code&gt;formikControl&lt;/code&gt; folder, we will create the following files&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;FormikControl.jsx&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Input.jsx&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SelectInput.jsx&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;TextError.jsx&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start with with our &lt;code&gt;Input.jsx&lt;/code&gt; file, we will do the following imports  into our file&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;Field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useField&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;formik&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&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;prop-types&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FormLabel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TextField&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;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;After the imports, we create a functional component and the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&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;field&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Grid&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="nx"&gt;direction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;column&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormLabel&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;legend&lt;/span&gt;&lt;span class="dl"&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;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormLabel&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;TextField&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Grid&lt;/span&gt;&lt;span class="err"&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="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&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="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&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;Input&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;lt;Field/&amp;gt;&lt;/code&gt; automatically hooks up our &lt;code&gt;TextField&lt;/code&gt; to &lt;code&gt;Formik&lt;/code&gt; courtesy of the &lt;code&gt;as&lt;/code&gt; attribute.  We can render our &lt;code&gt;React&lt;/code&gt; component and &lt;code&gt;formik&lt;/code&gt; will automatically inject &lt;code&gt;onChange&lt;/code&gt;, &lt;code&gt;onBlur&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;value&lt;/code&gt; props of the input field attributed to the name prop of the component. We destructed the &lt;code&gt;field&lt;/code&gt; object from &lt;a href="https://formik.org/docs/api/useField" rel="noopener noreferrer"&gt;usefield&lt;/a&gt; to perform this.&lt;/p&gt;

&lt;p&gt;To be able to handle errors from the validations, we use our already created &lt;code&gt;&amp;lt;TextError/&amp;gt;&lt;/code&gt; and render it beneath our . To get the error messages, we destructure &lt;code&gt;ErrorMessage&lt;/code&gt; from &lt;code&gt;formik&lt;/code&gt; and pass in &lt;code&gt;&amp;lt;TextError/&amp;gt;&lt;/code&gt; just like we did for TextField in . Below is the new code that we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;ErrorMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useField&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;formik&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&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;prop-types&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FormLabel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TextField&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;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;TextError&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;./TextError&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="nx"&gt;Input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&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;field&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Grid&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="nx"&gt;direction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;column&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormLabel&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;legend&lt;/span&gt;&lt;span class="dl"&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;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FormLabel&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;TextField&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorMessage&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;TextError&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Grid&lt;/span&gt;&lt;span class="err"&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="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&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="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isRequired&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;Input&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The next thing we do is to define our .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TextError.jsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&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;prop-types&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="nx"&gt;TextError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&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;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;TextError&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&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;TextError&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;lt;TextError/&amp;gt;&lt;/code&gt; render any error that returns from the ErrorMessage and display it, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: we gave it a color red to show it's an error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Rendering Our Input Field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To display our input fields to the user,  we try to make them seamless and easier to use. To do this, we import our Input component into our . Here is the complete code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Input&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;./Input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&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;prop-types&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="nx"&gt;FormikControl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&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="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Input&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="c1"&gt;// other cases...&lt;/span&gt;
     &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&gt;FormikControl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;control&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isRequired&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;FormikControl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;control&lt;/code&gt; prop helps to switch between different types of input field created and the rest prop is used to pass other details passed to the input field component.&lt;/p&gt;

&lt;h4&gt;
  
  
  yaaah..... our component is ready for use......
&lt;/h4&gt;

&lt;p&gt;To use the Component, we import it into a  and render it just like it shown  below in &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.jsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;Grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;Button&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;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="nx"&gt;Formik&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;Form&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;formik&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;FormikControl&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;./FormikControl&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="nx"&gt;App&lt;/span&gt; &lt;span class="o"&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Formik&lt;/span&gt;
      &lt;span class="nx"&gt;initialValues&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
        &lt;span class="na"&gt;FullName&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="p"&gt;}}&lt;/span&gt;
      &lt;span class="nx"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;values&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;values&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Form&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100%&lt;/span&gt;&lt;span class="dl"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Grid&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="nx"&gt;direction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;column&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormikControl&lt;/span&gt;
                &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FullName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter your name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
              &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Grid&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Formik&lt;/span&gt;&lt;span class="err"&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;p&gt;Here, We imported our &lt;code&gt;&amp;lt;FormikControl/&amp;gt;&lt;/code&gt; and rendered it inside  &lt;code&gt;Formik&lt;/code&gt;, we passed our &lt;code&gt;initialValues&lt;/code&gt;, and &lt;code&gt;onSubmit&lt;/code&gt; Props. We must ensure that the &lt;code&gt;name&lt;/code&gt; attribute in our &lt;code&gt;&amp;lt;FormikControl/&amp;gt;&lt;/code&gt; matches the one in our &lt;code&gt;intialValues&lt;/code&gt;. With that Formik automatically assigned values and other field props. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yup implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After creating our input components, we would like to add some validations to them, &lt;code&gt;yup&lt;/code&gt; is a JavaScript schema builder for value parsing and validation. we already installed the package and use it we import it into our   and define the validation rules as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Yup&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;yup&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we define our validation schema object as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;validationSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter your Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name is required&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we also use the name attribute that was used in  i.e FullName as the key for the validation to work effectively.&lt;br&gt;
Finally, we passed our &lt;code&gt;validationSchema&lt;/code&gt; as a prop to the &lt;code&gt;Formik&lt;/code&gt; component.  Here is the final code in &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.jsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;Form&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Formik&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;formik&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Button&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;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Yup&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;yup&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;FormikControl&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;./FormikControl&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="nx"&gt;App&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;validationSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Yup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter your Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name is required&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Formik&lt;/span&gt;
      &lt;span class="nx"&gt;initialValues&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
        &lt;span class="na"&gt;FullName&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="p"&gt;}}&lt;/span&gt;
      &lt;span class="nx"&gt;validationSchema&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;validationSchema&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;values&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Form&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;marginTop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1rem&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100%&lt;/span&gt;&lt;span class="dl"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Grid&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="nx"&gt;direction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;column&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FormikControl&lt;/span&gt;
            &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FullName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter your FullName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Grid&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Formik&lt;/span&gt;&lt;span class="err"&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;p&gt;That ends our lesson on implementing a &lt;code&gt;&amp;lt;FormikControl /&amp;gt;&lt;/code&gt; with &lt;code&gt;yup&lt;/code&gt;, &lt;code&gt;formik&lt;/code&gt; and &lt;code&gt;material-ui&lt;/code&gt;. The code is available in this &lt;a href="https://github.com/isiakaabd/Formik-input-component" rel="noopener noreferrer"&gt;repo&lt;/a&gt; and I have also implemented input fields like textarea, radio, date, time, select and checkbox.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introduction to JavaScript Bitwise Operators</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Sun, 15 Aug 2021 20:37:12 +0000</pubDate>
      <link>https://dev.to/isiakaabd/introduction-to-javascript-bitwise-operators-4i5e</link>
      <guid>https://dev.to/isiakaabd/introduction-to-javascript-bitwise-operators-4i5e</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Recently on twitter, a tweep asked how to determine if a number is even or odd. I hurriedly answered using modulus. Yeah, my answer was right, but any other approach? I was hinted about bitwise operator. JavaScript operates using different type of operator which includes &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Assignment operators&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Arithmetic operators&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ternary operators&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical operators&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comparison operators&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typeof operator&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bitwise operators&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;We commonly use most of these operators in our routine but sparingly use bitwise operator especially if you just getting started with &lt;em&gt;JavaScript&lt;/em&gt;.&lt;br&gt;
This article will elucidate on different bitwise operators, how we can use it and  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What's bitwise?
&lt;/h3&gt;

&lt;p&gt;Bitwise is a level of operations that involves working with individual bits, which are the smallest units of data in a computer. a bit can be &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;. All bitwise operations are performed with &lt;code&gt;32bits&lt;/code&gt; binary numbers and later converted to 64bits(&lt;em&gt;javaScript&lt;/em&gt; works with &lt;code&gt;64bits&lt;/code&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In this article, we will make use of  &lt;code&gt;ES6&lt;/code&gt; &lt;em&gt;arrow function&lt;/em&gt; syntax and conversion to decimal(base 10) will follow the example below:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjkt2j7hkxj5cqzetz2r5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjkt2j7hkxj5cqzetz2r5.PNG" alt="base 10" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Conversion of integers to bit will be done using the binary operation as illustrated below, Writing of the bits begins from the lasts bit to the first as pointed by the arrow:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F14kwd6oynej6wzrxlfzu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F14kwd6oynej6wzrxlfzu.png" alt="base2" width="210" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhbz7o9hqssz43jx0366b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhbz7o9hqssz43jx0366b.png" alt="bits" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;From the above snippets, we can conclude that numbers that has the last bit as &lt;code&gt;0&lt;/code&gt;  are &lt;code&gt;even&lt;/code&gt; while integer with last bit as &lt;code&gt;1&lt;/code&gt; are &lt;code&gt;odd&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Converting 12 to bit , we have&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;000000000000000000000000000001100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But for simplicity sake, we remove the preceding &lt;code&gt;0s&lt;/code&gt; and our bits are&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  1100

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Types of Bitwise Operators&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;amp;&lt;/code&gt; — AND&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;|&lt;/code&gt; — OR&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~&lt;/code&gt; — NOT&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;^&lt;/code&gt; —  XOR&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; — Left Shift&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; — Sign-Propagating Right Shift&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; — Zero-Fill Right Shift&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Lets get started&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;amp;&lt;/code&gt; - &lt;code&gt;AND&lt;/code&gt; OPERATOR&lt;/strong&gt; : This return  &lt;code&gt;1&lt;/code&gt; if the corresponding bits of  &lt;code&gt;operands&lt;/code&gt; are &lt;code&gt;1&lt;/code&gt; and return &lt;code&gt;0&lt;/code&gt;  if they differs. &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Below is a table for comparison
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxyci7cby0bgzcswx6q62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxyci7cby0bgzcswx6q62.png" alt="AND" width="800" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1: The example below is a &lt;code&gt;function&lt;/code&gt; that computes the &lt;code&gt;AND&lt;/code&gt; operator.
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffe79gf8lxgzbkdtn4or3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffe79gf8lxgzbkdtn4or3.png" alt="AND" width="800" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Common interview question is to determine if a number is even or odd, JavaScript Bitwise AND can be used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Example 2 : Determine if a number is even or odd
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1epnsetx68icoeo2efza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1epnsetx68icoeo2efza.png" alt="even" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; &lt;br&gt;
As I said earlier, &lt;code&gt;Even&lt;/code&gt; Numbers when converted to &lt;code&gt;bits&lt;/code&gt; ends with &lt;code&gt;0&lt;/code&gt;, &lt;em&gt;JavaScript&lt;/em&gt; compares the last bit and discard the remaining. Therefore,  &lt;code&gt;0 &amp;amp; 1&lt;/code&gt;  will return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Example 3: The below code checks for &lt;code&gt;odd&lt;/code&gt; Number
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F530dcn0vyyjl6h0h0xkk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F530dcn0vyyjl6h0h0xkk.png" alt="odd" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; &lt;br&gt;
This check if the last &lt;code&gt;bit&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, then compare   &lt;code&gt;1 &amp;amp; 1&lt;/code&gt;  and return &lt;code&gt;true&lt;/code&gt; otherwise return &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;|&lt;/code&gt; - &lt;code&gt;OR&lt;/code&gt; OPERATOR&lt;/strong&gt; : This return &lt;code&gt;1&lt;/code&gt; if any of the corresponding operand's &lt;code&gt;bit&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;and return &lt;code&gt;0&lt;/code&gt; is the  operand's bits are &lt;code&gt;0&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Below is a table illustrating different bits combination&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzsgje7ozee7tg6j6vgem.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzsgje7ozee7tg6j6vgem.png" alt="OR" width="800" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 4 : The below code takes two &lt;code&gt;operands&lt;/code&gt; as arguments and perform the &lt;code&gt;OR&lt;/code&gt; bitwise operation.
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgv54ar2zkskd13wwlfa1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgv54ar2zkskd13wwlfa1.png" alt="Bitwise OR" width="800" height="796"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;~&lt;/code&gt; &lt;code&gt;NOT&lt;/code&gt; Operator&lt;/strong&gt; : This accepts only one &lt;code&gt;operand&lt;/code&gt;(unary operator). &lt;code&gt;~&lt;/code&gt; performs &lt;code&gt;NOT&lt;/code&gt; operator on every &lt;code&gt;bit&lt;/code&gt;. &lt;code&gt;bits&lt;/code&gt; that are &lt;code&gt;1&lt;/code&gt; become &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;bits&lt;/code&gt; that are &lt;code&gt;0&lt;/code&gt; turn to &lt;code&gt;1&lt;/code&gt;, forming the ones' complement of the given binary value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example 5 : The &lt;code&gt;function&lt;/code&gt; below performs &lt;code&gt;NOT&lt;/code&gt; operation
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Feeg7iio28kwvb6q5a2w3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Feeg7iio28kwvb6q5a2w3.png" alt="bitwiseNOT" width="800" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we said earlier, ~ operator turns the bit from zero to one and vice-versa.&lt;br&gt;
 &lt;code&gt;~ 8&lt;/code&gt; becomes &lt;code&gt;1111111111111111111111111111011&lt;/code&gt; (-9 in decimal) &lt;/p&gt;

&lt;h5&gt;
  
  
  Few things to note:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first bit by the left is called the &lt;code&gt;sign bit&lt;/code&gt;. The &lt;code&gt;sign bit&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; for positive integer and  &lt;code&gt;1&lt;/code&gt;   for negative  integer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The remaining &lt;code&gt;31bits&lt;/code&gt; are used to represent integer. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the maximum &lt;code&gt;32bits&lt;/code&gt; integer that can be represented can be calculated as  &lt;code&gt;2^31 - 2^0 = 2147483647&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While the minimum is &lt;code&gt;-(2^31) = - 2147483648&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Bitwise operators convert their operands to 32-bit signed integers in two’s complement format.  when the NOT operator is used on an integer, the resulting value is the two’s complement of the integer as shown below&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6kz61hcbyhd1jzs2iph6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6kz61hcbyhd1jzs2iph6.png" alt="noToPERATOR" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;^&lt;/code&gt; - &lt;code&gt;XOR&lt;/code&gt; OPERATOR&lt;/strong&gt; : Also called (exclusive-Or), it returns &lt;code&gt;0&lt;/code&gt; if the operand's bit are the same &lt;code&gt;(0 or 1)&lt;/code&gt; and if different return &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Faw457v90o2zvex7wssxl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Faw457v90o2zvex7wssxl.png" alt="XOR" width="800" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 6 : The &lt;code&gt;function&lt;/code&gt; below performs  &lt;code&gt;XOR&lt;/code&gt; operation
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3iyvyqxjs6zbo4x7cpp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3iyvyqxjs6zbo4x7cpp4.png" alt="XOR" width="800" height="796"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; - &lt;code&gt;Left shift&lt;/code&gt; OPERATOR&lt;/strong&gt; : This takes two &lt;code&gt;operands&lt;/code&gt;, the &lt;code&gt;first&lt;/code&gt; is an &lt;code&gt;integer&lt;/code&gt; to be converted to &lt;code&gt;bits&lt;/code&gt; while the &lt;code&gt;second&lt;/code&gt; &lt;code&gt;operand&lt;/code&gt; is the number of &lt;code&gt;bits&lt;/code&gt; of the &lt;code&gt;first&lt;/code&gt; &lt;code&gt;operand&lt;/code&gt; to be shifted away from the &lt;code&gt;left&lt;/code&gt; and added to the &lt;code&gt;right&lt;/code&gt;.
##### Example 7 : The &lt;code&gt;function&lt;/code&gt; below performs  &lt;code&gt;Left shift&lt;/code&gt; operation &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F80bf83o9qxigda7e6jr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F80bf83o9qxigda7e6jr2.png" alt="checkLeftShift" width="800" height="942"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; - &lt;code&gt;Sign-propagating right shift&lt;/code&gt; OPERATOR&lt;/strong&gt;: This takes two &lt;code&gt;operands&lt;/code&gt;, the &lt;code&gt;first&lt;/code&gt; is an integer to be converted to bits while the &lt;code&gt;second&lt;/code&gt; &lt;code&gt;operand&lt;/code&gt; is the number of &lt;code&gt;bits&lt;/code&gt; of the first &lt;code&gt;operand&lt;/code&gt; to be shifted away from the &lt;code&gt;right&lt;/code&gt; and also discarded from the &lt;code&gt;left&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example 8 : The &lt;code&gt;function&lt;/code&gt; below performs  &lt;code&gt;Sign-propagating right shift&lt;/code&gt; operation.
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Focfagl285xcekd8fj8up.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Focfagl285xcekd8fj8up.png" alt="checkRightShift" width="800" height="976"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; - &lt;code&gt;Zero-fill right shift&lt;/code&gt;&lt;/strong&gt; : This behaves  like the sign-propagating right shift (&amp;gt;&amp;gt;) operator. The difference is that &lt;code&gt;bits&lt;/code&gt; are shifted in from the left.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example 9 : The &lt;code&gt;function&lt;/code&gt; below performs  &lt;code&gt;Zero-fill right shift&lt;/code&gt; operation.
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fr7mv9kevdd92df5c4hqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fr7mv9kevdd92df5c4hqi.png" alt="Zero-fill" width="800" height="976"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for Reading!!!!&lt;/strong&gt;&lt;br&gt;
If you enjoy this and wish to get Notified when I published new articles, click &lt;a href="https://mailchi.mp/4f641d33a5ee/subscribe-ti-my" rel="noopener noreferrer"&gt;here&lt;/a&gt; to subscribe.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>operations</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>My Target as HNGi8/Zuri Intern 2021</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Sun, 15 Aug 2021 18:01:56 +0000</pubDate>
      <link>https://dev.to/isiakaabd/my-target-as-hngi8-zuri-intern-2021-7b3</link>
      <guid>https://dev.to/isiakaabd/my-target-as-hngi8-zuri-intern-2021-7b3</guid>
      <description>&lt;p&gt;My Name is &lt;a href="https://linktr.ee/isiakaabd" rel="noopener noreferrer"&gt;Isiaka Abdulahi&lt;/a&gt;, a Front end developer proficient in &lt;em&gt;HTML&lt;/em&gt;, &lt;em&gt;CSS&lt;/em&gt;, &lt;em&gt;JavaScript&lt;/em&gt;, &lt;em&gt;ReactJS&lt;/em&gt; and &lt;em&gt;Bootstrap&lt;/em&gt;. I'm currently Interning at &lt;a href="https://internship.zuri.team" rel="noopener noreferrer"&gt;&lt;strong&gt;HNGi8/ZURI&lt;/strong&gt;&lt;/a&gt;. I joined the fellowship series that ended and the experiences were awesome, learning to meet deadlines, working on projects individually and also as a team, exhilarating feedbacks after tasks, team collaboration and grit were all that featured in the last fellowship. If all could be rosy for the fellowship, what would you think the internship would be?&lt;/p&gt;

&lt;p&gt;My joy knew no bound when I learnt that application for the next internship was out, I applied and was anxious about the admission. Having read and heard of those who had been successful in the previous cohort, I believe it's my time to take on a larger stage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fp8uo8nm852y8ibnvsrjx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fp8uo8nm852y8ibnvsrjx.jpg" alt="congratulations" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Below are the lists I hope to achieve during the 8 weeks of intensive training.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A tech role&lt;/strong&gt;: Before or at the end of this internship, I want to secure my first role as a developer, this time, I want to pass technical interviews in flying colors with supports from team mates and mentors&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-life Projects&lt;/strong&gt;: one of the challenges &lt;em&gt;#newbies&lt;/em&gt; faced is working on impactful projects that can make their portfolios stand out, with this internship, I hope to work on  real life projects just like it was done during the fellowship.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Leadership and Team collaboration&lt;/strong&gt;: oftentimes, I've always being a team player, rallying supports to my team. This time I want to take a step forward, take the bull by the horn and go for the leadership role.  This will further improve my interpersonal skills and huge step towards landing my first role.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Networking&lt;/strong&gt;: As popularly said, &lt;em&gt;Network is net worth&lt;/em&gt;, I want to improve my network by meeting people from &lt;em&gt;all walks of life&lt;/em&gt;, collaborating with them on projects and having a relationship that goes beyond the internship. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The list is endless. If the above mentioned are achieved.. The internship will be a greater success.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Course Recommendations for Beginners&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are tons of videos on YouTube, it's always a pain in the neck to choose one, but I will always advise watching videos are made  less than a year, this will help to keep in touch with the latest development  in that particular field. Below are the recommended videos&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;UI/UX :&lt;br&gt;
&lt;a href="https://youtu.be/1MbQaYCCzzI" rel="noopener noreferrer"&gt; Introduction to Figma&lt;/a&gt;  by  &lt;strong&gt;Zuri Team&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Version control : &lt;br&gt;
&lt;a href="https://youtu.be/3fUbBnN_H2c" rel="noopener noreferrer"&gt;Introduction to GIT/GITHUB&lt;/a&gt; by &lt;strong&gt;Amigoscode&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HTML/CSS : &lt;br&gt;
&lt;a href="https://youtu.be/_Jb2fnEFOm0" rel="noopener noreferrer"&gt;Practical Introduction to HTML &amp;amp; CSS&lt;/a&gt; by &lt;strong&gt;Zuri Team&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript :&lt;br&gt;
&lt;a href="https://youtu.be/jS4aFq5-91M" rel="noopener noreferrer"&gt;JavaScript programming-full Course &lt;/a&gt; by &lt;strong&gt;Freecodecamp&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript library (REACTJS)&lt;br&gt;
&lt;a href="https://youtube.com/playlist?list=PLC3y8-rFHvwgg3vaYJgHGnModB54rxOk3" rel="noopener noreferrer"&gt;Link&lt;/a&gt; by &lt;strong&gt;Codevolution&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All  Thanks and appreciation go to the &lt;a href="https://zuri.team" rel="noopener noreferrer"&gt;Zuri Team&lt;/a&gt;. God Bless!!!!!!!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;&lt;br&gt;
I write articles on frontend development and technologies.&lt;br&gt;
Do you wish to get notified when I published a new article? click &lt;a href="https://mailchi.mp/4f641d33a5ee/subscribe-ti-my" rel="noopener noreferrer"&gt;here&lt;/a&gt; to subscribe to my posts.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Implementation of Linked List in JavaScript and solution to Leetcode interview Question</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Tue, 10 Aug 2021 13:41:01 +0000</pubDate>
      <link>https://dev.to/isiakaabd/implementation-of-linked-list-in-javascript-and-solution-to-leetcode-interview-question-35ld</link>
      <guid>https://dev.to/isiakaabd/implementation-of-linked-list-in-javascript-and-solution-to-leetcode-interview-question-35ld</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In &lt;em&gt;computer science&lt;/em&gt;, a data structure is a data organization, management and storage format that enables efficient access and modification. Data structures provide a means to manage large amounts of data efficiently for uses such as large databases and internet indexing.&lt;/p&gt;

&lt;p&gt;There are various types of data structures which includes &lt;em&gt;array&lt;/em&gt;, &lt;em&gt;linked list&lt;/em&gt;, &lt;em&gt;record&lt;/em&gt;, &lt;em&gt;union&lt;/em&gt;, &lt;em&gt;binary tree&lt;/em&gt; and &lt;em&gt;graph&lt;/em&gt;.&lt;br&gt;
In this article, we will look at &lt;em&gt;linked list&lt;/em&gt;, it's implementation, various methods we can perform on the linked list and some interview questions.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is linked list?
&lt;/h3&gt;

&lt;p&gt;A linked list also  called list is a linear collection of elements called &lt;em&gt;nodes. The nodes can be any data type(primitive or non-primitive). Each node has  a value, and points to the next node in the linked list. i.e&lt;/em&gt; a node is aware of next node.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmfwevocj8fclnlg0ltcy.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmfwevocj8fclnlg0ltcy.PNG" alt="singlyLinkedList" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Linked list&lt;/em&gt; is the second most-used data structure after &lt;em&gt;array&lt;/em&gt; and it is similar to &lt;em&gt;array&lt;/em&gt;. The entry point of a linked list is called head while the last node points to null. If a linked list is empty, the head put to null.&lt;/p&gt;
&lt;h3&gt;
  
  
  Types of Linked list
&lt;/h3&gt;

&lt;p&gt;There are &lt;code&gt;three&lt;/code&gt; basic types of linked list namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Singly-linked list&lt;/strong&gt; : This is the simplest type, each node has a data and a pointer to the next &lt;em&gt;node&lt;/em&gt;. It allow traversal of data in one direction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Doubly-linked list&lt;/strong&gt;: This is a complex type in which each &lt;em&gt;node&lt;/em&gt; has a data and two pointers which point to the previous &lt;em&gt;node&lt;/em&gt; and next node. traversal of data is bi-directional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Circular-linked list&lt;/strong&gt;: This is a similar to &lt;em&gt;singly linked list&lt;/em&gt; with the last &lt;em&gt;node&lt;/em&gt; points to the first &lt;em&gt;node&lt;/em&gt; and vice versa. While traversing a &lt;em&gt;circular liked list&lt;/em&gt;, we can start at any node and traverse the list in any direction forward and backward until we reach the same &lt;em&gt;node&lt;/em&gt; we started. Thus, a &lt;em&gt;circular linked list&lt;/em&gt; has no beginning and no end. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Why use Linked list?
&lt;/h3&gt;

&lt;p&gt;Just like &lt;em&gt;array&lt;/em&gt;, &lt;em&gt;linkedlist&lt;/em&gt; is a linear &lt;em&gt;data structure&lt;/em&gt; which performs all operations like adding, deleting and inserting data. Below are the benefits of Linked List:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insertion and Deletion&lt;/strong&gt;: Adding an element at the beginning of &lt;em&gt;array&lt;/em&gt; rearranged the items and shift their indexes which might be tedious while  working on a large database. In &lt;em&gt;linked list&lt;/em&gt;, we just update the address present in the next pointer of a &lt;em&gt;node&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Size&lt;/strong&gt; : Since &lt;em&gt;nodes&lt;/em&gt; in the linked list are aware of the next &lt;em&gt;node&lt;/em&gt; such that data can exist at scattered addresses this allows for dynamic size which can change at run time unlike &lt;em&gt;array&lt;/em&gt; which stores it's data in a non-scattered blocks of memory which can't be changed at run time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory Allocation&lt;/strong&gt;: For &lt;em&gt;array&lt;/em&gt;, memory allocation is done at compile time i.e at the time when &lt;em&gt;array&lt;/em&gt; is declared. While for linked list, memory is allocated when data is added to it thus done at run time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory wastage&lt;/strong&gt; : Since memory allocation is done at runtime for linked list, there's no memory waste. In array, if a size of 100 is declared and only 80 is utilized to store data. The remaining 20 spaces are wasted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;: Linear data structures like stack and queues are often easily implemented using a linked list.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Limitations of Linked List
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory Utilization&lt;/strong&gt;:  Memory is needed to store data and also point to the next node in the list. &lt;em&gt;Linkedlist&lt;/em&gt; require more memory compared to array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Search operation&lt;/strong&gt;: In linked list, direct access to element like index in array is not possible. It involves traversing through the whole list and which could leads to time wastage &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory wastage&lt;/strong&gt;: In &lt;em&gt;double linked list&lt;/em&gt; that reverse traversing is possible &lt;em&gt;i.e&lt;/em&gt; a node is aware of element before and ahead of it. This requires extra memory and wastage if not utilized.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Time Complexity &amp;amp; Big O Notation for Linked list and array&lt;/strong&gt;!
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fu8ipmz2jbikxqo59y82y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu8ipmz2jbikxqo59y82y.png" alt="bigO" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Implementation of Linked List&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's create a &lt;em&gt;class&lt;/em&gt; &lt;code&gt;LinkedList&lt;/code&gt; list, initialized the &lt;code&gt;head&lt;/code&gt;, and &lt;code&gt;tail&lt;/code&gt; to &lt;code&gt;null&lt;/code&gt; and length to zero in the constructor function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8fmnwt8wswnrbk01ay5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8fmnwt8wswnrbk01ay5l.png" alt="Linked-list-class" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will also use class to create nodes in the list. This can be access by using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1ut2291g9fn9on5tnynk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1ut2291g9fn9on5tnynk.png" alt="node" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;putting all together&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1l5s6fogm4rq8awy50hb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1l5s6fogm4rq8awy50hb.png" alt="Implementing=LinkedList" width="800" height="827"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Some Linkedlist method&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prepend()&lt;/strong&gt; : This method add node to the list at the beginning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fit4ajjm7r5w7wcbm1qr8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fit4ajjm7r5w7wcbm1qr8.png" alt="prepend" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;printData()&lt;/strong&gt; : This method prints all the nodes present in the list. It shows the node, pointer and the next node.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsnldfy3qjce4afhtjpgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsnldfy3qjce4afhtjpgz.png" alt="printData" width="800" height="620"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;append()&lt;/strong&gt;: This method add new node as the last node on the linked list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F989z9s8m5gwtgyx1a20r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F989z9s8m5gwtgyx1a20r.png" alt="append" width="800" height="781"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;getLength()&lt;/strong&gt;: This return the length of the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fn5uhaui6q2gwaie0ymw4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn5uhaui6q2gwaie0ymw4.png" alt="getLength" width="800" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;find()&lt;/strong&gt; : This method finds the &lt;em&gt;node&lt;/em&gt; passed as argument. or it return &lt;code&gt;null&lt;/code&gt; if its not found.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxqh5i4i6746sverbmzmx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxqh5i4i6746sverbmzmx.png" alt="find" width="800" height="1080"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;delete()&lt;/strong&gt;: This removes the node referenced from the argument.
&lt;img src="https://media2.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%2Fpwvnx6linmyq3w01sd0r.png" alt="delete" width="800" height="1037"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;code snippets available on&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://codepen.io/pen/?template=gOWZKdv" rel="noopener noreferrer"&gt;code pen&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/isiakaabd/LinkedListArticle.git" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  LeetCode  interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Question 1&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flhylv29bdc93kk0r3rw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flhylv29bdc93kk0r3rw1.png" alt="Example1" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7ljazwc0yahz5x1628ps.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7ljazwc0yahz5x1628ps.PNG" alt="leetcode-Image" width="575" height="391"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution to Question 1
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fz2slhe771eoojtvr7f90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fz2slhe771eoojtvr7f90.png" alt="deleteNode" width="800" height="591"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
Deleting a node in &lt;em&gt;linkedlist&lt;/em&gt; involves moving pointer to the next node ahead of the target.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; node.val = node.next.val ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Basically saying, where we have &lt;code&gt;5&lt;/code&gt; as the &lt;code&gt;val&lt;/code&gt;,  replace it with next &lt;code&gt;val&lt;/code&gt; which is &lt;code&gt;1&lt;/code&gt;. this remove 5 from the node.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node.next = node.next.next;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;here, our node is an array, [4,5,1,9]. our &lt;code&gt;node.next.next&lt;/code&gt; will be [1,9]. This delete the node and its value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we discussed &lt;code&gt;linkedlist&lt;/code&gt;, different types of &lt;code&gt;linkedlist&lt;/code&gt;, the benefits and limitations over &lt;code&gt;array&lt;/code&gt;, several methods that can be used and solution to leetcode problem. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;&lt;br&gt;
Do you wish to get notified when I published a new article? click &lt;a href="https://mailchi.mp/4f641d33a5ee/subscribe-ti-my" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://abdulahi.netlify.app" rel="noopener noreferrer"&gt;ISIAKA ABDULAHI&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>datascience</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Third Argument in JavaScript SetTimeOut Method and Practical Example in Your Next Project</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Wed, 04 Aug 2021 14:28:47 +0000</pubDate>
      <link>https://dev.to/isiakaabd/third-argument-in-settimeout-and-practical-example-in-your-next-project-51pk</link>
      <guid>https://dev.to/isiakaabd/third-argument-in-settimeout-and-practical-example-in-your-next-project-51pk</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is setTimeOut?&lt;/strong&gt;&lt;br&gt;
SetTimeOut is a global or window method which executes a function taken as an argument once the timer expires.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fko0vlvpvkacwf83sjbl1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fko0vlvpvkacwf83sjbl1.png" alt="timer" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;br&gt;
 &lt;em&gt;function&lt;/em&gt;  can be defined within the  method or passed by value to the method.&lt;br&gt;
&lt;em&gt;timer&lt;/em&gt; is the delay expected before the function is executed. It's usually in &lt;code&gt;milliseconds&lt;/code&gt;. This parameter is optional as a value of &lt;code&gt;0&lt;/code&gt; is set by default which makes the function executes immediately.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;More often in our use of the method, we tend to use only two parameters(function and timer). This article will shed more lights on the use of third arguments and so on.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Let's compare the common way of using &lt;code&gt;setTimeOut&lt;/code&gt; without the third argument&lt;br&gt;
syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhtwjy6ag0zonbvmwjtu4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhtwjy6ag0zonbvmwjtu4.png" alt="Greetings" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's see how we can implement the same using third argument&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0yet6sncfiqmvphiy7ut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0yet6sncfiqmvphiy7ut.png" alt="Third Argument" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the second code, we can see that third and other numbers of arguments is use as argument in the reference function given to the &lt;code&gt;setTimeOut&lt;/code&gt; method. In short these arguments are carried forward to the reference function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also array can be passed as an argument&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvrlnxp91x8coj6e42gro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvrlnxp91x8coj6e42gro.png" alt="ArrayOfNumbers" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also an object could be passed as argument. &lt;code&gt;Try that!!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application in search box&lt;/strong&gt;&lt;br&gt;
Would you like to animate or spin your search icon pending a request is fulfilled or for some delay. using &lt;code&gt;setTimeOut&lt;/code&gt; with third argument will get it done, let's get started&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. we add our form tag, input box and search icons( &lt;code&gt;fontawesome icons&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fq4ughx5to430chzfk3j7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fq4ughx5to430chzfk3j7.png" alt="HTML" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2. We add some &lt;code&gt;css&lt;/code&gt; stylings to make the page looks nicer or you can style to suit your taste.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fx2szopu7ugya0e6nmr8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx2szopu7ugya0e6nmr8w.png" alt="CSS" width="800" height="907"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;3. Our search box looks like this adding the &lt;code&gt;css&lt;/code&gt; stylings.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fc20fad8at32kz2wcnhvb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fc20fad8at32kz2wcnhvb.PNG" alt="searchBoxStyling" width="358" height="77"&gt;&lt;/a&gt;   &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lets add some functionalities&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4. lets create an &lt;code&gt;helper&lt;/code&gt; function that returns an element.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fljtn4td9fr31jgqc7c94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fljtn4td9fr31jgqc7c94.png" alt="helperFunction" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;5. Using the above &lt;code&gt;helper&lt;/code&gt; function, we can get our &lt;code&gt;search-icon&lt;/code&gt;, &lt;code&gt;searchBtn&lt;/code&gt;  elements below:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;image&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fh9cs9roum6b8td8odj5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh9cs9roum6b8td8odj5p.png" alt="searchIcon" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;6.  We add an &lt;code&gt;eventListener&lt;/code&gt; to &lt;code&gt;searchBtn&lt;/code&gt; &lt;code&gt;button&lt;/code&gt; such that when click we want it to perform some operations:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fau7u03e9sbd6epimx9of.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fau7u03e9sbd6epimx9of.png" alt="search Button" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fksw21clmngaob9c7ubsm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fksw21clmngaob9c7ubsm.png" alt="Animate" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;7. Defining the &lt;code&gt;animate&lt;/code&gt; function, We want the search icon &lt;code&gt;fa-search&lt;/code&gt; to be hidden and spinner &lt;code&gt;fa-spinner&lt;/code&gt; to animate for 3s when the &lt;code&gt;searchBtn&lt;/code&gt; is clicked. To execute that, we define our animate function  below:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;syntax&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqi5a2bwb8jogc9796kyr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqi5a2bwb8jogc9796kyr.png" alt="Animate 2" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;8. In passing &lt;code&gt;searchIcon&lt;/code&gt; as the third arguments, the setTimeOut method is aware of the icon to restore after the &lt;code&gt;fa-spinner&lt;/code&gt; finished animating after 3s.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;&lt;br&gt;
Do you wish to get notified when I published a new article? click &lt;a href="https://mailchi.mp/4f641d33a5ee/subscribe-ti-my" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://abdulahi.netlify.app" rel="noopener noreferrer"&gt;ISIAKA ABDULAHI&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Method chaining in javascript</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Mon, 19 Apr 2021 15:52:11 +0000</pubDate>
      <link>https://dev.to/isiakaabd/method-chaining-in-javascript-154a</link>
      <guid>https://dev.to/isiakaabd/method-chaining-in-javascript-154a</guid>
      <description>&lt;p&gt;&lt;strong&gt;Method&lt;/strong&gt; is a  function that is a property of an &lt;em&gt;object&lt;/em&gt;. It is also a property containing a function definition.&lt;br&gt;
let's consider the example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Var car = {
Color: “red”,
brand: “Toyota”,
description: function (){
This.brand  + "is a  Motor Corporation is a Japanese multinational automotive manufacturer"
}
}

// description is a property of a car that is also a function. So a function inside an object is called METHOD.
 //Calling 
console.log(car.description())
// Toyota is a  Motor Corporation is a Japanese multinational automotive manufacturer

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

&lt;/div&gt;



&lt;p&gt;If we have 5 methods in the object. We end up with something  like this:&lt;br&gt;
&lt;code&gt;car.description()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;car.description()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;car.description()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;car.description()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;car.description()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Could you see that &lt;code&gt;car&lt;/code&gt; is unnecessarily repeated and doesn’t seems neat and could take lines of code? We can get rid of that by using a mechanism called &lt;strong&gt;METHOD CHAINING&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Method chaining&lt;/em&gt; is the mechanism of calling a method on another method of the same object. This ensures a cleaner and readable code.  Method chaining uses &lt;code&gt;this&lt;/code&gt; keyword in the object's class to access its methods. In javascript,  the &lt;code&gt;this&lt;/code&gt; keyword refers to the current object in which it is called. When a method returns &lt;code&gt;this&lt;/code&gt;, it simply returns an instance of the object in which it is returned. in another word, to chain methods together, we need to make sure that each method we define has a return value so that we can call another method on it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's write some code&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Class chaining{
    Method1(){
    console.log(method1)
    return this
    }

    Method2(){
    console.log(method2)
    return this
    }

}

 Const chaining_obj = new chaining()
 chaining_obj
 .mathod1()
 .method2()

//output
method1
method2


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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;let's consider this example&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Arithmetic {
  constructor() {
    this.value = 0;
  }
  sum(...args) {
    this.value = args.reduce((sum, current) =&amp;gt; sum + current, 0);
    return this;
  }
  addition(value) {
    this.value = this.value + value;
    return this;
  }
  subtraction(value) {
    this.value = this.value - value;
    return this;
  }
  average(...args) {
    this.value = args.length? (this.sum(...args).value) / 
  args.length: undefined;
    return this;
  }
}


const Arithmetic 1= new Arithmetic()
Arithmetic1
  .sum(1, 3, 6)   // =&amp;gt; { value: 10 } 
  .subtraction(3)   // =&amp;gt; { value: 7 }
  .add(4)        // =&amp;gt; { value: 11 }
  .value         // =&amp;gt; 11 


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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;&lt;br&gt;
Do you wish to get notified when I published a new article? click &lt;a href="https://mailchi.mp/4f641d33a5ee/subscribe-ti-my" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://linkedin.com/in/isiakaabd" rel="noopener noreferrer"&gt;ISIAKA ABDULAHI&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>oop</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Portfolio Review</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Wed, 19 Aug 2020 17:39:08 +0000</pubDate>
      <link>https://dev.to/isiakaabd/portfolio-review-fpk</link>
      <guid>https://dev.to/isiakaabd/portfolio-review-fpk</guid>
      <description>&lt;p&gt;Pls my great community, &lt;br&gt;
This is my first  portfolio, kindly help to view it and give a feedback. It's due for submission as an assignment. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://isiaka-abdulahi-remlad-portfolio.netlify.app" rel="noopener noreferrer"&gt;https://isiaka-abdulahi-remlad-portfolio.netlify.app&lt;/a&gt;&lt;br&gt;
Thanks. &lt;br&gt;
It's not yet mobile responsive &lt;/p&gt;

</description>
    </item>
    <item>
      <title>My first post</title>
      <dc:creator>ISIAKA ABDULAHI AKINKUNMI</dc:creator>
      <pubDate>Sat, 25 Jul 2020 05:03:12 +0000</pubDate>
      <link>https://dev.to/isiakaabd/my-first-post-is-54bj</link>
      <guid>https://dev.to/isiakaabd/my-first-post-is-54bj</guid>
      <description>&lt;p&gt;I'm new here&lt;/p&gt;

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