<?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: Hesbon limo</title>
    <description>The latest articles on DEV Community by Hesbon limo (@limoh653).</description>
    <link>https://dev.to/limoh653</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%2F2756447%2F476b53b8-0aeb-4e6d-a86d-64abf7fb4581.png</url>
      <title>DEV Community: Hesbon limo</title>
      <link>https://dev.to/limoh653</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/limoh653"/>
    <language>en</language>
    <item>
      <title>Setting up (vite+react) project with shadcn ui.</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Fri, 10 Oct 2025 09:10:30 +0000</pubDate>
      <link>https://dev.to/limoh653/setting-up-vitereact-project-with-shadcn-ui-2hmn</link>
      <guid>https://dev.to/limoh653/setting-up-vitereact-project-with-shadcn-ui-2hmn</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;while trying to set app my vite react app with shadcn, I realised that the official documentation for shadcn UI had only configuration for typescript while that for react was not there. This forced me to troubleshoot and find a solution and this is what I am going to share in this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 Creating vite project and adding tailwindCSS
&lt;/h2&gt;

&lt;p&gt;In the terminal write this command to create a new react app using vite.&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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add tailwind CSS run this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install tailwindcss @tailwindcss/vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2 Setting app files.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1 index.css
&lt;/h3&gt;

&lt;p&gt;Replace everything in the   &lt;code&gt;src/index.css&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @import "tailwindcss";

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.2 jsconfig.json
&lt;/h3&gt;

&lt;p&gt;Note that in that in your vite app this file is missing so you have to create it by using the following command in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; touch jsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;paste the code below in your &lt;code&gt;jsconfig.json&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; {
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.node.json"
    }
  ],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.3 Vite.config.js
&lt;/h3&gt;

&lt;p&gt;Remove all the code in the &lt;code&gt;vite.config.js&lt;/code&gt; and paste the one 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 { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite' 
import path from "path"

// https://vite.dev/config/
export default defineConfig({
   plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
})

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  3 Setting up Shadcn.
&lt;/h2&gt;

&lt;p&gt;Run the following command to set up shadcn in your project,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npx shadcn@latest init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be asked a question to configure &lt;code&gt;components.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Which color would you like to use as base color? › Neutral&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  4 Adding components to your project
&lt;/h2&gt;

&lt;p&gt;You can now start adding components to your project.&lt;br&gt;
Let's add a button component to the project using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx shadcn@latest add button
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now import the component in the &lt;code&gt;app.js&lt;/code&gt; like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Button } from "@/components/ui/button"

function App() {
  return (
    &amp;lt;div className="flex min-h-svh flex-col items-center justify-center"&amp;gt;
      &amp;lt;Button&amp;gt;Click me&amp;lt;/Button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;Now run, npm run dev and your output should look like this in your browser&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%2Fcm3mfp604o7qulmbnzlt.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%2Fcm3mfp604o7qulmbnzlt.png" alt="screenshot of the output -" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;This tutorial covers how to set up shadcn with a vite react app. I hope this tutorial was helpful. If you have any feedback feel free to reach out to me at &lt;a href="mailto:limohesbon7@gmail.com"&gt;limohesbon7@gmail.com&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding File I/O in Python: Reading, Writing, and Managing Files</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Mon, 17 Feb 2025 14:20:37 +0000</pubDate>
      <link>https://dev.to/limoh653/understanding-file-io-in-python-reading-writing-and-managing-files-2dof</link>
      <guid>https://dev.to/limoh653/understanding-file-io-in-python-reading-writing-and-managing-files-2dof</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In most programs, we store data temporarily in the code editor, but the data is lost as soon as we exit the program. Let's consider data stored in a list, for example. Once we close the program, all the data in the List is lost. &lt;/p&gt;

&lt;p&gt;With file io, we can read from, that is, load data from a file or write to, that is, save data into a file, ensuring that data persists even after ending the program.&lt;/p&gt;

&lt;h3&gt;
  
  
  The advantages of file I/O
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Simple and easy to understand hence, it can be used by both experienced and junior developer&lt;/li&gt;
&lt;li&gt;It supports various file formats therefore. It provides flexibility in working with different file formats.&lt;/li&gt;
&lt;li&gt;Python provides many built-in functions for file I/O, hence reducing the need for using external libraries, which are not reliable sometimes.&lt;/li&gt;
&lt;li&gt;Python file I/O provides functionality for error handling.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disadvantages of file I/O
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It does not provide perform well for large files compared to low-level languages like C and C#. &lt;/li&gt;
&lt;li&gt;It consumes memory space, which can lead to memory issues in memory-constrained environments.&lt;/li&gt;
&lt;li&gt;Python file I/O has limited concurrency hence, it is less suitable for highly parallel tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Practical uses of file I/O in Python
&lt;/h3&gt;

&lt;p&gt;File I/O is fundamental for storing and retrieving data in applications and databases. It's used in automating tasks such as report generation and data transformation in big data.&lt;/p&gt;

&lt;p&gt;Additionally, it's used when handling downloads and uploads in web development.&lt;/p&gt;

&lt;h2&gt;
  
  
  File modes
&lt;/h2&gt;

&lt;p&gt;File modes are used to perform different tasks, such as reading and appending files. Here are some of the common modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'+'&lt;/code&gt;: Update mode.Used to open a file for both reading and writing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'a'&lt;/code&gt;: Append mode. Opens the file for appending.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'b'&lt;/code&gt;: Binary mode. Used when dealing with files in the binary mode.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'r'&lt;/code&gt;: Read mode. Opens the file for reading.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  - &lt;code&gt;'w'&lt;/code&gt;: Writing mode. Opens the file for writing.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Opening and writing files
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;open&lt;/code&gt; function in Python is used to open a file in Python for reading or writing. Let's write a program that opens a file, allows the user to enter data, and finally saves.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;what is the model of your car?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cars.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we are declaring a variable called cars used to store the input of the car model. We then open a file called cars.txt with the writing mode. Finally, we save the file by closing. The code above works perfectly, but the problem is that even though we write as many car models as we want, we will get only the last value. This is because the write mode overwrites the previous entry.&lt;/p&gt;

&lt;p&gt;Now, let's solve the problem by writing a program that uses the append file mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;what is the model of your car?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cars.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's add three models of cars, that is, Mazda, Honda, and Subaru, and see the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mazda
honda
subaru
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cars.txt file looks like the one above. Hence, our problem is solved.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading from files
&lt;/h3&gt;

&lt;p&gt;Now let's see how we can read from files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cars.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My car is a, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&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;readlines&lt;/code&gt; method reads all the lines from the file and stores them in a list, while the &lt;code&gt;rstrip&lt;/code&gt; method removes extra newline characters.&lt;/p&gt;

&lt;p&gt;The output looks like the one below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;My car is a, mazda
My car is a, honda
My car is a, subaru
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can further simplify the code by reading line by line using &lt;code&gt;rstrip&lt;/code&gt; without &lt;code&gt;readlines&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cars.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My car is a, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output from the terminal is the same as the previous one.&lt;/p&gt;

&lt;p&gt;Let's move on and sort the names of the cars before printing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cars.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My car is a, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output looks like the one below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;My car is a, honda
My car is a, mazda
My car is a, subaru
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As evident from the output, the cars have been sorted using the first letter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with CSV Files
&lt;/h2&gt;

&lt;p&gt;Comma-separated values (CSV) files are commonly used in big data and storing tabular data. Let's create a simple CSV file named &lt;code&gt;students.csv&lt;/code&gt; as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;George, Kilimanjaro
 Ronald,Elgon
 Frank,Kilimanjaro
 Washington,Elgon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can open the file and read it using Python as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;students.csv&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;house&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;house&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; is in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;house&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above reads the file, stores each data in a dictionary, and then sorts and prints students by name.&lt;br&gt;
For more complex &lt;code&gt;CSV&lt;/code&gt; data, the CSV Python module is used. It can process CSV data with commas inside the data.&lt;/p&gt;
&lt;h3&gt;
  
  
  Writing to CSV FIles
&lt;/h3&gt;

&lt;p&gt;Let's write data to a CSV file using the &lt;code&gt;CSV&lt;/code&gt; module as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;

&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s your name? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;house&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s your house? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;students.csv&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fieldnames&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;house&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writerow&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;house&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above prompts the user to enter a name and a house and then write to it using the append method.&lt;/p&gt;

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

&lt;p&gt;File I/O is very important as it allows users to read and write data into files. In this article, we have discussed what file I/O is and its advantages and disadvantages. We also discussed the application of file I/O in the real world. We then looked into file modes, opening and writing files, and then working with CSV files. You can check out my GitHub link for more &lt;a href="https://github.com/limoh653/file-IO" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>How to authenticate a Flask API using JWT.</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Mon, 17 Feb 2025 10:34:02 +0000</pubDate>
      <link>https://dev.to/limoh653/how-to-authenticate-a-flask-api-using-jwt-1lo</link>
      <guid>https://dev.to/limoh653/how-to-authenticate-a-flask-api-using-jwt-1lo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Security is one of the most important aspects of any organization. API authentication is one way in which APIs are secured, making sure that only authorized users access sensitive data from protected servers.&lt;/p&gt;

&lt;p&gt;JSON Web Token &lt;a href="https://jwt.io/introduction" rel="noopener noreferrer"&gt;(JWT)&lt;/a&gt; is a common method of user authentication and authorization standard used to exchange data securely. Made of three components, a header, a payload, and a signature, here are the steps involved in the authorization process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication.&lt;/strong&gt; The client sends the user credentials to the server, and the server authenticates the client and generates JWT containing the users' information&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Issuing the token.&lt;/strong&gt; The JWT created by the server is sent to the client which is stored for future use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sending the token.&lt;/strong&gt; When the client wants to access a protected resource it sends the JWT in the authorization header of the http request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verifying the token.&lt;/strong&gt; The server receives the request and uses the secret key that was used to sign in to verify the JWT. If the JWT is valid, the server extracts the information contained in it and determines which action is the client authorized to perform.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorizing the request.&lt;/strong&gt; If the user is authorized, the server responds with the data requested else it returns an error message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we are going to discuss how to authenticate a Flask API step by step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Installing Flask and JWT
&lt;/h3&gt;

&lt;p&gt;First, let's create a folder in the VS Code code editor as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir flask-auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a virtual environment and activate it inside this folder as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m venv env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source env/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's install Flask and JWT using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install flask
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install PyJWT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing Flask and JWT, let's make a file called app.py using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a folder that hold our &lt;code&gt;login.html&lt;/code&gt; and name it templates as follows:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Inside the templates folder create a file called &lt;code&gt;login.html&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch login.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After completing all these steps, your project folder 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%2F851kow8jjhdeipuhs1bv.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%2F851kow8jjhdeipuhs1bv.png" alt="Screenshot from 2025-01-31 22-29-40" width="264" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Importing necessary libraries
&lt;/h3&gt;

&lt;p&gt;Inside the &lt;code&gt;app.py&lt;/code&gt; file we had created in the previous step write 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;import jwt
from flask import Flask, request, jsonify, make_response, render_template, session, flash
from datetime import datetime, timedelta
from functools import wraps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's discuss the imports above;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;jwt&lt;/code&gt;: It imports PYjwt which is used to encode and decode JSON Web Tokens.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Flask&lt;/code&gt;: This is the core of the Flask web framework. It is used to create the instance for the web application.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;request&lt;/code&gt;: Used to create HTTPS requests.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;jsonify&lt;/code&gt;: It is used to convert Python dictionaries into JSON responses.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;make_response&lt;/code&gt;: It is used to create custom HTTP responses.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;render_template:&lt;/code&gt; It is used to render HTML templates using jinja2, which is the templating engine that flask uses.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;session:&lt;/code&gt; It allows you to store and access variables of the current user between requests.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;flash:&lt;/code&gt; It is used to display an error or success message to the user.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;datetime:&lt;/code&gt; It is a class that represents date and time.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;timedelta:&lt;/code&gt; Represents the difference between two dates and times. It is mainly used to set the expiration time of Json Web Tokens.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wraps:&lt;/code&gt; This is used to decorate functions, preserving their metadata (like name, docstring, etc.) when they are wrapped by another function. It’s often used in scenarios like creating custom decorators.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Creating Flask instance
&lt;/h3&gt;

&lt;p&gt;Before creating the Flask instance follow these steps to create a secret key.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the terminal, run the following commands:
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



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

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uuid.uuid4().hex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now copy the secret key and paste it into your app instance. Replace &lt;code&gt;secret&lt;/code&gt; with your secret key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Define the public Route
&lt;/h3&gt;

&lt;p&gt;Let's define a public route that can be accessed by anyone without authorization using 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;@app.route('/public')
def public():
    return 'This is a public page'

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

&lt;/div&gt;



&lt;p&gt;Run the application and open the following URL in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://127.0.0.1:5000/public
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should look like this in your browser;&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%2Fjc0502j4aw45o1iwf02u.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%2Fjc0502j4aw45o1iwf02u.png" alt="Screenshot from 2025-01-31 23-23-00" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Define an auth route
&lt;/h3&gt;

&lt;p&gt;Let's define an auth route that can only be accessed by users who are registered on the app. We will also use the &lt;code&gt;token_required&lt;/code&gt; decorator to make sure that users with token access the route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/auth')
@token_required
def auth():
    return 'You are verified. Welcome to your dashboard!'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's define the &lt;code&gt;token_required&lt;/code&gt; function which will be used for authorized routes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def token_required(func):
    @wraps(func)
    def decorated(*args, **kwargs):
        token = request.args.get('token')
        if not token:
            return jsonify({'Alert!': 'Token is missing'}), 401
        try:
            payload = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
        except jwt.ExpiredSignatureError:
            return jsonify({'Alert!': 'Token has expired'}), 401
        except jwt.InvalidTokenError:
            return jsonify({'Alert!': 'Invalid Token'}), 401
        return func(*args, **kwargs)  # Correctly return the wrapped function
    return decorated

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

&lt;/div&gt;



&lt;p&gt;Let's breakdown the code above;&lt;br&gt;
The &lt;code&gt;@token_required&lt;/code&gt; decorator is applied to the function to make sure that only requests with a valid JWT token can access the route.&lt;br&gt;
In line five we are retrieving the token from the request’s query parameters using &lt;code&gt;request.args.get('token').&lt;/code&gt; If the token is missing the condition becomes true hence, the 401 error is returned.&lt;/p&gt;

&lt;p&gt;Inside line seven the code tries to decode the provided token using &lt;code&gt;jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256']).&lt;/code&gt; If the token has expired, the code catches the ExpiredSignatureError and returns the 401 unauthorized error.&lt;/p&gt;

&lt;p&gt;Line 11 checks if the token is invalid and if the condition is true, it returns the 401 unauthorized error.&lt;/p&gt;

&lt;p&gt;Let's run the app on the terminal and enter the following URL in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://127.0.0.1:5000/auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should look like the one below:&lt;br&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%2Fgmc0unx40qm89cz43k4i.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%2Fgmc0unx40qm89cz43k4i.png" alt="image" width="789" height="405"&gt;&lt;/a&gt;&lt;br&gt;
We are getting the expected output that &lt;code&gt;Token is missing&lt;/code&gt; because we are not yet logged in. Let's move to the next step and see how we can get the token.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 6: Define the login method
&lt;/h3&gt;

&lt;p&gt;We need to define a login route that we will use to log in to the app and access the token. We will pass the username and the password and if they match the ones in the route the login session variable will be set to &lt;code&gt;True&lt;/code&gt;. Here is the code for the login route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/login', methods=['POST'])
def login():
    username = request.form.get('username')
    password = request.form.get('password')

    if username and password == '123456':  # Fix incorrect comparison
        session['logged_in'] = True
        token = jwt.encode({
            'user': username,
            'exp': datetime.utcnow() + timedelta(seconds=150)  # Use 'exp' for expiry
        }, app.config['SECRET_KEY'], algorithm='HS256')

        return jsonify({'token': token})  # No need for `.decode('utf-8')`
    else:
        return make_response('Unable to verify', 403, {
            'WWW-Authenticate': 'Basic realm="Authentication Failed!"'
        })

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

&lt;/div&gt;



&lt;p&gt;Let's go ahead and breakdown the code above;&lt;/p&gt;

&lt;p&gt;The condition from line sis checks if the username and the password are correct. If the two are correct, the code creates a JWT token and allows the user to log in. If the username and the password are not correct, the code from line fourteen is executed where the system returns a 403 Forbidden error, saying "Unable to verify".&lt;br&gt;
Now let's go ahead and define a login.html template that will display the login form. In the login.html folder that we had created earlier, add this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Login&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form action="/login" method ='POST'&amp;gt;
        &amp;lt;input type="username" name="username" placeholder="username"&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
        &amp;lt;input type="password" name="password" placeholder="password"&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
        &amp;lt;input type="submit" value="Login"&amp;gt;
    &amp;lt;/form&amp;gt; 

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the app once again and input any username and password as &lt;code&gt;123456&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://127.0.0.1:5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output after inputting the correct password should look like this:&lt;br&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%2Fc8o89ivh6nowkn5gx9g4.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%2Fc8o89ivh6nowkn5gx9g4.png" alt="image" width="800" height="317"&gt;&lt;/a&gt;&lt;br&gt;
After logging in we get the token as shown above.&lt;/p&gt;

&lt;p&gt;Let's use the token to access the data in the private route. Open &lt;a href="https://jwt.io/" rel="noopener noreferrer"&gt;JWT.io&lt;/a&gt; and paste the token as shown below:&lt;br&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%2Fo5q0rw9btzsjtm1g4wkc.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%2Fo5q0rw9btzsjtm1g4wkc.png" alt="image" width="800" height="391"&gt;&lt;/a&gt;&lt;br&gt;
Now we can access the data as shown above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;In this article, we have discussed the importance of API security in your application and how JWT authentication works. We also developed different routes of a Flask API and also learned how to come up with a secret key. &lt;/p&gt;

&lt;p&gt;Finally, we used a JWT token to access data on the &lt;code&gt;JWT.io&lt;/code&gt; website. In conclusion, I recommend checking out my GitHub link &lt;a href="https://github.com/limoh653/flask-auth" rel="noopener noreferrer"&gt;here&lt;/a&gt; for the code.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>web</category>
    </item>
    <item>
      <title>Mastering WebSockets with Socket.IO: A Comprehensive Guide</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Sat, 15 Feb 2025 19:21:53 +0000</pubDate>
      <link>https://dev.to/limoh653/mastering-websockets-with-socketio-a-comprehensive-guide-5c6h</link>
      <guid>https://dev.to/limoh653/mastering-websockets-with-socketio-a-comprehensive-guide-5c6h</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What is a WebSocket?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;WebSocket is a communication protocol that enables two-way communication between a server and a client over a long-lived connection. A protocol is a set of rules and conventions that dictate how data is being transmitted between two or more computer devices.&lt;/p&gt;

&lt;p&gt;Basically websockets are a set of ideas about one particular way where we work with data. Unlike traditional APIs and HTTP connections, websockets allow for real-time communication making it ideal for chart applications and sports betting sites.&lt;/p&gt;

&lt;p&gt;The major difference between HTTP and WebSockets is how the client and the server communicate. HTTP requests send a request to the server then the server sends a response to the client and then the connection ends.&lt;/p&gt;

&lt;p&gt;On the other hand, WebSockets sends a handshake request to the server and if the server is capable of supporting the websocket protocol it will send back a confirmation. Once the connection is established both parties can send and receive information without waiting for the request-response cycle.&lt;/p&gt;

&lt;p&gt;Even though web sockets are long-lived they include mechanisms for detecting and handling dropped connections and inactivity. For example, you could specify that if neither the server nor the client has sent a message in the last ten minutes the connection should be stopped.&lt;/p&gt;

&lt;p&gt;Picture this in an online chatting app. Two users are able to chat in real time with the application showing whether the other party is online, typing or has deleted a message. That is the real-world application of WebSockets.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Building a CRUD Operation using &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We are going to build a CRUD application where you can create, read, update, and delete text using &lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Setting up the environment&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Make sure node.js has been installed in your environment. If not, run the following command in your terminal globally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install -y nodejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation, run the following command to check the version of node js installed:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Setting up the backend&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In your code editor, create a folder named &lt;code&gt;server&lt;/code&gt;. After creating the folder, run the following command in your terminal, to install the necessary node modules, run this command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;To install the &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; package in your backend folder, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install socket.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Setting up the frontend&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In your code editor, create a folder named &lt;code&gt;client&lt;/code&gt;. After creating the client folder, run &lt;code&gt;npm install&lt;/code&gt; to install the necessary node modules. After installing the node modules, it's time to install the &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; package in your frontend folder using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install socket.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Implementing the frontend&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the client folder you had created earlier, create a React app using vite by running the following command:&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 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the React app we are going to write our code in the app.jsx. Here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";
import { io } from "socket.io-client";
import "./App.css";
import { v4 as uuidv4 } from "uuid";

function App() {
  const [formInputs, setFormInputs] = useState({});
  const [crudData, setCrudData] = useState([]); // Initialize as an empty array
  const [isEdit, setIsEdit] = useState(false);
  const socket = io("http://localhost:3000");

  const handleInput = (event) =&amp;gt; {
    const { name, value } = event.target;
    setFormInputs((prev) =&amp;gt; ({ ...prev, [name]: value }));
  };

  const handleSubmit = () =&amp;gt; {
    if (!formInputs.name || !formInputs.age || !formInputs.number) {
      console.error("All fields must be filled before submitting.");
      return;
    }

    const newData = {
      ...formInputs,
      id: uuidv4(), // Generate a unique ID for the new data
    };

    socket.emit("data", newData); // Send the new data to the server
    setFormInputs({}); // Clear the input fields after submission
  };

  const handleEdit = () =&amp;gt; {
    if (!formInputs.id) {
      console.error("No item selected for editing.");
      return;
    }

    console.log("Submitting edited data:", formInputs);
    socket.emit("editData", formInputs); // Emit the edited data to the server
    setIsEdit(false); // Reset edit mode
    setFormInputs({}); // Clear the form inputs
  };

  const handleDelete = (id) =&amp;gt; {
    console.log("Deleting item with ID:", id);
    socket.emit("deleteData", id); // Emit the delete request to the server
  };

  useEffect(() =&amp;gt; {
    // Listen for updated CRUD data from the server
    socket.on("crudData", (response) =&amp;gt; {
      console.log("Updated data received from server:", response);
      if (Array.isArray(response)) {
        setCrudData(response); // Update the UI with the latest data
      } else {
        console.error("Unexpected response format:", response);
      }
    });

    return () =&amp;gt; {
      socket.off("crudData"); // Clean up listener
    };
  }, []);

  const getEditData = (data) =&amp;gt; {
    setFormInputs(data);
    setIsEdit(true);
  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h1&amp;gt;CRUD Operations&amp;lt;/h1&amp;gt;
      &amp;lt;div className="form-fields"&amp;gt;
        &amp;lt;input
          onChange={handleInput}
          className="input-field"
          name="name"
          placeholder="Enter your name"
          value={formInputs.name || ""}
        /&amp;gt;
        &amp;lt;input
          onChange={handleInput}
          className="input-field"
          name="age"
          placeholder="Enter your age"
          value={formInputs.age || ""}
        /&amp;gt;
        &amp;lt;input
          onChange={handleInput}
          className="input-field"
          name="number"
          placeholder="Enter your phone number"
          value={formInputs.number || ""}
        /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;button onClick={isEdit ? handleEdit : handleSubmit}&amp;gt;
        {isEdit ? "Edit" : "Add"} Data
      &amp;lt;/button&amp;gt;
      &amp;lt;table&amp;gt;
        &amp;lt;thead&amp;gt;
          &amp;lt;tr&amp;gt;
            &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;
            &amp;lt;th&amp;gt;Age&amp;lt;/th&amp;gt;
            &amp;lt;th&amp;gt;Phone Number&amp;lt;/th&amp;gt;
            &amp;lt;th&amp;gt;Actions&amp;lt;/th&amp;gt;
          &amp;lt;/tr&amp;gt;
        &amp;lt;/thead&amp;gt;
        &amp;lt;tbody&amp;gt;
          {Array.isArray(crudData) &amp;amp;&amp;amp; crudData.length &amp;gt; 0 ? (
            crudData.map((data, index) =&amp;gt; (
              &amp;lt;tr key={index}&amp;gt;
                &amp;lt;td&amp;gt;{data.name}&amp;lt;/td&amp;gt;
                &amp;lt;td&amp;gt;{data.age}&amp;lt;/td&amp;gt;
                &amp;lt;td&amp;gt;{data.number}&amp;lt;/td&amp;gt;
                &amp;lt;td&amp;gt;
                  &amp;lt;button onClick={() =&amp;gt; getEditData(data)}&amp;gt;Edit&amp;lt;/button&amp;gt;
                  &amp;lt;button onClick={() =&amp;gt; handleDelete(data.id)}&amp;gt;Delete&amp;lt;/button&amp;gt;
                &amp;lt;/td&amp;gt;
              &amp;lt;/tr&amp;gt;
            ))
          ) : (
            &amp;lt;tr&amp;gt;
              &amp;lt;td colSpan="4"&amp;gt;No data available&amp;lt;/td&amp;gt;
            &amp;lt;/tr&amp;gt;
          )}
        &amp;lt;/tbody&amp;gt;
      &amp;lt;/table&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Let's discuss this frontend code in detail. First, we start with the imports in lines 1 to 4:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useState and useEffect&lt;/code&gt;: These are React hooks for managing state and side effects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;io&lt;/code&gt;: This is simply a &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; package used for real-time communication to the server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uuidv4&lt;/code&gt;: This is used to create a unique ID for each input in the application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's move to the initialization from lines 7 to 10:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;formInput&lt;/code&gt;: This is for storing the form inputs from the user of the app that is name, age, and phone number.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;crudData&lt;/code&gt;: This is used to store data sent to the server or received from the server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isEdit&lt;/code&gt;: It's used to check if the button is in the submit or edit mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In line 10 we are connecting the frontend to the server which is running in local host port 3000.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;handleInput&lt;/code&gt; function in line 12 is used to update the formInputs state as the user types in the values.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;handleSubmit&lt;/code&gt; function is used to submit the new data to the server. It works by checking if all the fields have been entered, if they are, it will create a new ID for the data and send it to the server. Finally, it clears the input field after the data has been submitted.&lt;/p&gt;

&lt;p&gt;After that, we move to the &lt;code&gt;handleEdit&lt;/code&gt; function from line 32. The function works by checking if an input field has been selected for editing. If so, It will emit the edited data to the server. Then it resets the edit mode and clears the input field.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handleDelete&lt;/code&gt; function is used to delete data from the UI and also submit the delete request to the server using &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; for real-time communication.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook is used to check for any updates to the crudData from the server then it updates it in our UI. By ui, I mean the frontend or what you see in the browser.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getEditData&lt;/code&gt; function enables us to edit our data by setting the isEdit state to true.&lt;/p&gt;

&lt;p&gt;The return method from lines 70 to 130 is used to render the UI. It displays the input fields for name, age, and phone number and a button that switches between edit and add. It also displays all the data we have and buttons to edit and delete each entry.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Implementing the Backend&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now let's move to the backend and discuss the nodejs code.&lt;/p&gt;

&lt;p&gt;In the server folder create a file and name it &lt;code&gt;server.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { createServer } = require("http");
const { Server } = require("socket.io");

// Create the HTTP server
const httpServer = createServer();
const io = new Server(httpServer, {
  cors: {
    origin: "http://localhost:5173", // Allow requests from the frontend
  },
});

let crudData = []; // Array to store all CRUD data

io.on("connection", (socket) =&amp;gt; {
  console.log("A client connected");

  // Send the current data to the newly connected client
  socket.emit("crudData", crudData);

  // Handle receiving new data from the client
  socket.on("data", (data) =&amp;gt; {
    console.log("Received new data:", data);

    // Add the new data to the array
    crudData.push(data);

    // Broadcast the updated data to all connected clients
    io.emit("crudData", crudData);
  });

  // Handle editing data
  socket.on("editData", (response) =&amp;gt; {
    console.log("Edit data received:", response);

    // Find the index of the item to be edited
    const currentIndex = crudData.findIndex((data) =&amp;gt; data.id === response.id);

    if (currentIndex !== -1) {
      // Update the item
      crudData[currentIndex] = { ...crudData[currentIndex], ...response };

      // Broadcast the updated data to all clients
      io.emit("crudData", crudData);
      console.log("Updated data broadcasted:", crudData);
    } else {
      console.error("Item not found for editing");
    }
  });

  // Handle deleting data
  socket.on("deleteData", (id) =&amp;gt; {
    console.log("Delete request received for ID:", id);

    // Filter out the item to delete
    crudData = crudData.filter((data) =&amp;gt; data.id !== id);

    // Broadcast the updated data to all clients
    io.emit("crudData", crudData);
    console.log("Updated data after deletion:", crudData);
  });

  // Handle client disconnect
  socket.on("disconnect", () =&amp;gt; {
    console.log("A client disconnected");
  });
});

// Start the server
httpServer.listen(3000, () =&amp;gt; {
  console.log("Server is connected and listening on http://localhost:3000");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In lines 1 and 2, we are declaring an HTTP server and initializing &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; for real-time communication.&lt;/p&gt;

&lt;p&gt;Lines 4 to 10 create an HTTP server associated with &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; and then connect to the front end which is running in port 5173 using the CORS policy which prevents it from blogging the connection. In line 12 we are declaring an empty array to store all the inputs from the frontend.&lt;/p&gt;

&lt;p&gt;The io.on event in line 14 is an event that connects to the client and emits all the data in the server to the newly connected client. Socket.on the event in line 21 handles new data from the client and publishes the data in the crudData array using the push array method. It then broadcast the newly added data to the client using io.emmit.&lt;/p&gt;

&lt;p&gt;In line 32 we have the &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; &lt;code&gt;editaData&lt;/code&gt; event which handles data editing in the server. When the user clicks edit in the client, the server searches for the data using the findIndex method. If it exists it updates the data in the crudData array then it broadcasts the edited data to the client.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; &lt;code&gt;deleteData&lt;/code&gt; event in line 52 handles the deleting of data in the server. When the client initiates a delete function, the server finds the data using its ID and deletes it from crudData array using the filter method. The event then broadcasts the updated data to the client.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Testing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I am going to show you how real-time communication works with web sockets by opening two tabs and also show the server side.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4ZlE9Nr6Btc"&gt;
&lt;/iframe&gt;
  &lt;/p&gt;

&lt;p&gt;As you can see from the video, initially, I had two entries in my applications then I added the third one and it immediately updated. Then I switched to another tab and there were also three entries same as the previous tab. I also deleted one entry and it was updated in both tabs.&lt;/p&gt;

&lt;p&gt;When I switch to the server you can see the updated data after deletion. I then edited the name of the first entry from &lt;code&gt;kiprop rono&lt;/code&gt; to &lt;code&gt;kiprop kiptoo&lt;/code&gt; and it updated both in the server and the ui.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;Socket.io&lt;/a&gt; is a very powerful tool that is used by various chat applications and betting apps for their real-time communication.&lt;/p&gt;

&lt;p&gt;In this article, we have discussed what are WebSockets, WebSockets vs HTTP protocol, and its common application. We have also built a real-time CRUD application using react, &lt;a href="http://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt;, and node Js.&lt;/p&gt;

&lt;p&gt;For more information, check out my GitHub link &lt;a href="https://github.com/limoh653/Socket.IO" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Mastering PostgreSQL: A Beginner's Guide</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Thu, 13 Feb 2025 19:23:28 +0000</pubDate>
      <link>https://dev.to/limoh653/mastering-postgresql-a-beginners-guide-4gd4</link>
      <guid>https://dev.to/limoh653/mastering-postgresql-a-beginners-guide-4gd4</guid>
      <description>&lt;h2&gt;
  
  
  What is PostgreSQL
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.postgresql.org/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt; is a free and open-source object-relational database management system. It is one of the most popular and trustable database systems that is used by most organizations, especially startups, for their backend development. PostgreSQL supports most of the backend frameworks such as Flask, Django, node js, and java springboot. &lt;/p&gt;

&lt;p&gt;The advantages of PostgreSQL is that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's open source&lt;/li&gt;
&lt;li&gt;It's robust and powerful enough to work on high-performance tasks&lt;/li&gt;
&lt;li&gt;It's backed by a strong community of developers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to install PostgreSQL
&lt;/h2&gt;

&lt;p&gt;Before you start working with PostgreSQL, you need to download it first. The installation process may vary depending on your operating system, but it supports major operating systems. &lt;/p&gt;

&lt;p&gt;You can download PostgreSQL &lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;Here&lt;/a&gt;. In this article, we are going to download it on Ubuntu, which is a Linux distribution sub-system.&lt;/p&gt;

&lt;p&gt;To download, run this command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; postgresql-common

&lt;span class="nb"&gt;sudo&lt;/span&gt; /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

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

&lt;/div&gt;



&lt;p&gt;After installation, you have to check its status using this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status postgresql

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

&lt;/div&gt;



&lt;p&gt;If everything is installed correctly, you will see a message that PostgreSQL is running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating your first PostgreSQL database
&lt;/h2&gt;

&lt;p&gt;Open your terminal and follow these steps as you create your first database&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1. Connect to the server
&lt;/h3&gt;

&lt;p&gt;Before connecting to the server, switch to the PostgreSQL user using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nb"&gt;sudo&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; postgres

psql

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

&lt;/div&gt;



&lt;p&gt;To connect to the PostgreSQL server, you need to create a user with password using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
CREATE USER myuser WITH PASSWORD &lt;span class="s1"&gt;'mypassword'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Replace "myuser" with what you want to be your username and "mypassword" with your actual password.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2. Create a database
&lt;/h3&gt;

&lt;p&gt;Create a database using the following command. Replace "sampledb" with the name of your database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
CREATE DATABASE sampledb&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The terminal 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%2F4zvcz9htdfmi59at7qsz.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%2F4zvcz9htdfmi59at7qsz.png" alt="image" width="800" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3. Switch to the created database
&lt;/h3&gt;

&lt;p&gt;Switch to the database using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="se"&gt;\c&lt;/span&gt; sampledb

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4. Create a table
&lt;/h3&gt;

&lt;p&gt;Let's create a simple table named &lt;code&gt;students&lt;/code&gt; with columns for student ID, student name, student program, and student stream using this SQL command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
CREATE TABLE students&lt;span class="o"&gt;(&lt;/span&gt;std_id INT PRIMARY KEY,std_name VARCHAR&lt;span class="o"&gt;(&lt;/span&gt;240&lt;span class="o"&gt;)&lt;/span&gt; NOT NULL, std_program VARCHAR&lt;span class="o"&gt;(&lt;/span&gt;240&lt;span class="o"&gt;)&lt;/span&gt; NOT NULL,std_stream VARCHAR&lt;span class="o"&gt;(&lt;/span&gt;120&lt;span class="o"&gt;)&lt;/span&gt; NOT NULL&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5. Insert data into the table
&lt;/h3&gt;

&lt;p&gt;Use the commands below to add data to the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
INSERT INTO students(std_id, std_name, std_program, std_stream) VALUES(1, 'Kiprop Too', 'BENG', 'North');

INSERT INTO students VALUES(2, 'Kiprono Too', 'BTECH', 'West');

INSERT INTO students VALUES(3, 'Kiprono Nano', 'BTECH','East');

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6. Query data from the table
&lt;/h3&gt;

&lt;p&gt;Use the command below to retrieve data from the &lt;code&gt;Students&lt;/code&gt; table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
SELECT * FROM students;

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

&lt;/div&gt;



&lt;p&gt;Here is the output:&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%2Fcoj4qnhgxfe1fstx3h64.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%2Fcoj4qnhgxfe1fstx3h64.png" alt="Screenshot from 2025-01-20 00-57-17" width="545" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7. Update data in the table
&lt;/h3&gt;

&lt;p&gt;We are going to update the stream in column 3 from East to South:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
UPDATE  students SET &lt;span class="nv"&gt;std_stream&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'South'&lt;/span&gt; WHERE &lt;span class="nv"&gt;std_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The output 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%2Fxj7w3xe9g1sakjhqejd9.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%2Fxj7w3xe9g1sakjhqejd9.png" alt="Screenshot from 2025-01-20 01-02-42" width="617" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8. Delete data from the table
&lt;/h3&gt;

&lt;p&gt;Let's delete the last column from the table using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
DELETE FROM students WHERE  &lt;span class="nv"&gt;std_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The output 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%2Fa09542ntjvp2f3iam8gi.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%2Fa09542ntjvp2f3iam8gi.png" alt="Screenshot from 2025-01-20 01-06-09" width="625" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Constraints in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;Constraints are rules that are declared in tables or columns of a database that are used to validate the data entered before being stored in the database. &lt;/p&gt;

&lt;p&gt;Examples are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;UNIQUE:&lt;/strong&gt; This constraint makes sure that a value is unique, for example, an email.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;FOREIGN KEY:&lt;/strong&gt; Foreign key is used to link a table with another table in one-to-many relationships or many-to-many relationships&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PRIMARY KEY:&lt;/strong&gt; This is used to show something that uniquely identifies our data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;NOT NULL:&lt;/strong&gt; This makes sure that a value is not omitted or should not be blank.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of &lt;code&gt;FOREIGN KEY&lt;/code&gt; constraint being applied&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
CREATE TABLE students(std_id INT PRIMARY KEY,std_name VARCHAR(240) NOT NULL, std_program VARCHAR(240) NOT NULL,std_stream VARCHAR(120) NOT NULL);

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

&lt;/div&gt;



&lt;p&gt;Learn more about constraints &lt;a href="https://medium.com/yavar/constraints-in-sql-postgresql-d29c849686d3" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Indexing for performance in PostgreSQL.
&lt;/h2&gt;

&lt;p&gt;Indexing is something that improves the overall performance of a database. It is a shortcut for retrieving data quickly from a database. Picture this: it's like a table of contents in a book where you can locate what you want without flipping through the pages. Common types of indexing are &lt;strong&gt;B-tree index&lt;/strong&gt; and &lt;strong&gt;Hash index&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's see examples of &lt;code&gt;B-tree&lt;/code&gt; and &lt;code&gt;Hash index&lt;/code&gt; indexes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash index&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;
-- Create a hash index on a single column

CREATE INDEX idx_column_name ON table_name USING hash (column_name);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;B-tree index&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;
-- Create a B-tree index on a single column

CREATE INDEX idx_column_name ON table_name (column_name);

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
-- Create a B-tree composite index on multiple columns

CREATE INDEX idx_multi_column ON table_name (column1, column2);

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://medium.com/autodesk-tlv/mastering-postgresql-indexes-for-optimal-performance-5e4b0dc293e5" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is an article that explains more about indexing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Restoring and backing up data in PostgreSQL.
&lt;/h2&gt;

&lt;p&gt;Regular backups are important for safeguarding data in your database. &lt;/p&gt;

&lt;p&gt;Here's a simple command to back up data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
pg_dump -U your_username -d sampledb &amp;gt; backup.sql

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

&lt;/div&gt;



&lt;p&gt;Rstore data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
psql -U your_username -d new_database &amp;amp;lt; backup.sql

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

&lt;/div&gt;



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

&lt;p&gt;PostgreSQL is a very useful database management tool which is used by both startups and well established companies for their backened. &lt;/p&gt;

&lt;p&gt;This article covered the basic setup of PostgreSQL as well as how to perform CRUD functionalities. As you continue using PostgreSQL, learn more about advanced queries, indexing, and constraints to improve your proficiency. You can reach out to me via X &lt;a href="https://x.com/limohesbon1" rel="noopener noreferrer"&gt;Here&lt;/a&gt; for more interaction.&lt;/p&gt;

&lt;p&gt;Happy coding!!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Most Important JavaScript Array Methods to Understand</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Tue, 11 Feb 2025 10:19:59 +0000</pubDate>
      <link>https://dev.to/limoh653/the-most-important-javascript-array-methods-to-understand-28a2</link>
      <guid>https://dev.to/limoh653/the-most-important-javascript-array-methods-to-understand-28a2</guid>
      <description>&lt;p&gt;An array in JavaScript is a global data structure use to store data. A global data structure is one which can be accessed everywhwere in your code.&lt;/p&gt;

&lt;p&gt;Arrays are important because it allows you to iterate over the stored data one by one because their indices are known. The biggest advantage of JavaScript arrays lies in the array methods.&lt;/p&gt;

&lt;p&gt;JavaScript array methods are built in functions that allow efficient traversal and manipulation of arrays. They provide functionalities like removing, searching and iterating through array elements, enhancing code readability and productivity.&lt;/p&gt;

&lt;p&gt;In this article we are going to cover fifteen most used array methods, which will make you a better JavaScript developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. map
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;map&lt;/code&gt; method allows you to loop on each element in the array and manipulate them according to your preference&lt;/p&gt;

&lt;p&gt;The map method creates a new array.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use the map method&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&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;timesBy2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;timesBy2&lt;/span&gt;&lt;span class="p"&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 8, 10, 12, 14, 16 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the &lt;code&gt;map&lt;/code&gt; method loops through each number in the array called numbers and multiplies it by two.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. filter
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;filter()&lt;/code&gt; array method creates a new array and returns the elements of the original array that passes a specific test implemented by a provided function.&lt;/p&gt;

&lt;p&gt;Here's a code block showing how the &lt;code&gt;filter&lt;/code&gt; method works&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&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;evens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evens&lt;/span&gt;&lt;span class="p"&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [output]
 [// output 2,4,6]

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

&lt;/div&gt;



&lt;p&gt;In the code above, we write a function which tests the array of numbers and returns only the even numbers using the filter method.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. slice
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;slice&lt;/code&gt; method returns  selected elements in an array from a start index to a (not inclusive) given end as a new array&lt;/p&gt;

&lt;p&gt;The method does not alter the original array.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 4,5 it selects from index 2 to 4 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above selects elements starting from index two to four but four will not be included in the output therefore, it will return index two and three.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. splice
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;splice&lt;/code&gt; method is used to add, remove or replace elements in an array. It overites the old array and returns an new array.&lt;/p&gt;

&lt;p&gt;Let's see how we use it to remove elements from an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2,3,4,5,6,7];

numbers.splice(2,2)

console.log(numbers)

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 2, 3, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above removes  two array elements starting from index two.&lt;/p&gt;

&lt;p&gt;The code below shows how to use &lt;code&gt;splice&lt;/code&gt; to add elements into an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// adding elements
const fruits = ['mango', 'banana', 'pawpaw'];

fruits.splice(2, 0, 'apples', 'oranges' )

console.log(fruits)

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 'mango', 'banana', 'apples', 'oranges', 'pawpaw' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we are adding two items from  index two while deleting none from the original array.&lt;/p&gt;

&lt;p&gt;Let's see how to use it to replace items&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = [ 'mango', 'banana', 'apples', 'oranges', 'pawpaw' ]

fruits.splice(1,1, 'blueberry')

console.log(fruits)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 'mango', 'blueberry', 'apples', 'oranges', 'pawpaw' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we are replacing one item from index one while also deleting an item in index one from the original array.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. forEach
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;forEach&lt;/code&gt; method loops through an array  and executes a provided function for each array element. In the code below we are printing all the elements from an array once&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const names = ['Boni', 'Alex', 'Clinton', 'Kevin'];

names.forEach((name) =&amp;gt; {console.log(name)});

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[
Boni
Alex
Clinton
Kevin
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. some
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;some&lt;/code&gt; method checks if some of the elements meets a certain condition passed in the function and returns true is some meet the condition and false if none meets the condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2,3,4,5,6,7];

const greaterThan1 = (number) =&amp;gt;  number &amp;gt; 1;

const isGreaterThan1 = numbers.some(greaterThan1)

console.log(isGreaterThan1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[true]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above checks if some numbers are greater than one and returns true because some numbers meet the condition otherwise it would have returned false.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. every
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;every&lt;/code&gt; method iterates through an array and checks if all the elements satisfies a certain condition passed via a callback function. It is used to verify if all the elements in an array satisfies a certain condition. It returns true if all elements satisfies the condition otherwise false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2,3,4,5,6,7];

const greaterThan1 = (number) =&amp;gt;  number &amp;gt; 4;

const isGreaterThan1 = numbers.every(greaterThan1)

console.log(isGreaterThan1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[false]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above returns false since all the numbers are not greater than four.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. fill
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;fill&lt;/code&gt; method fills specified elements into an array and overites the original array giving a new array. If the start and end positions are not specified it fills the whole array with the new element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Banana", "Pawpaw", "Apple", "Orange"];
fruits.fill("Avocado");

console.log(fruits)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 'Avocado', 'Avocado', 'Avocado', 'Avocado' ]

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

&lt;/div&gt;



&lt;p&gt;In the code above, the start and end position is not specified so it fills the whole array with &lt;code&gt;Avocado&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's see the one where start and end positions are specified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const fruits = ["Banana", "Pawpaw", "Apple", "Orange"];
fruits.fill("Avocado", 2,4);

console.log(fruits)


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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [output]
 [ 'Banana', 'Pawpaw', 'Avocado', 'Avocado' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above fills the array starting from index two to four but four not being included with the new element.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. push
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;push&lt;/code&gt; method adds new elements to the end of an array and changes the old array returning the new modified array. You can add a single element or many.&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;cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Toyota&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="s1"&gt;Mazda&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="s1"&gt;Honda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;cars&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mitsubishi&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="s1"&gt;Nissan&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="s1"&gt;Ford&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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;cars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 'Toyota', 'Mazda', 'Honda', 'Mitsubishi', 'Nissan', 'Ford' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. pop
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;pop&lt;/code&gt; method removes the last element of an array and returns  a new array with  a missing last element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cars = ['Toyota', 'Mazda', 'Honda','Mitsubishi', 'Nissan', 'Ford'];
cars.pop();

console.log(cars);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
 [ 'Toyota', 'Mazda', 'Honda', 'Mitsubishi', 'Nissan' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the last element which is 'Ford' has been removed.&lt;/p&gt;

&lt;h2&gt;
  
  
  11.unshift
&lt;/h2&gt;

&lt;p&gt;The method adds new elements to the beginning of an array. You can add a single element or multiple elements to the new array. It overwrites the original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const fruits = ["Pawpaw", "Orange", "Apple", "Mango"];
fruits.unshift("Cucumber","Pineapple");

console.log(fruits)

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [output]
 [ 'Cucumber', 'Pineapple', 'Pawpaw', 'Orange', 'Apple', 'Mango' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  12.shift
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;shift&lt;/code&gt; method removes the first element of an array and returns a new array by overwriting the original array.&lt;/p&gt;

&lt;p&gt;Here's is an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = [ 'Cucumber', 'Pineapple', 'Pawpaw', 'Orange', 'Apple', 'Mango' ];

fruits.shift();

console.log(fruits)

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 'Pineapple', 'Pawpaw', 'Orange', 'Apple', 'Mango' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  13. find
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;find&lt;/code&gt; method is used to search for an element in an array. I t returns the first element which satisfies the condition passed ina a callback function. If no element satisfies the conditon it returns undefined.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```javascript =&lt;br&gt;
const numbers =[1,2,3,4,5,6,7];&lt;/p&gt;

&lt;p&gt;const number = numbers.find((number) =&amp;gt; number &amp;gt; 5);&lt;/p&gt;

&lt;p&gt;console.log(number);&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;



```text=
[output]
[6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The code above returns 6 since it's first element to meet the condition greater than 5.&lt;/p&gt;
&lt;h2&gt;
  
  
  14. findIndex
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;findIndex&lt;/code&gt; returns the position of the first element that satisfies a condition that was passed. It returns -1 if no match is found. It does not overwrite the original array. It returns true or false.&lt;/p&gt;

&lt;p&gt;Here's an example&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;number&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&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;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above returns 5 because 5 is the index of the first number that satisfies the condition greater than five.&lt;/p&gt;

&lt;h2&gt;
  
  
  15. includes
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;includes&lt;/code&gt; method checks if a specific element is present in an array. It returns true if its present or false if it's not present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1,2,3,4]

console.log(numbers.includes(3))

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;JavaScript array methods are very important built-in functions used to write scalable and readable code. This article covered fifteen most used array methods. I would recommend reading Javascript oficial documentation &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;here&lt;/a&gt; in order to understand more about the array methods. &lt;a href="https://github.com/limoh653/JS-Array-methods" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is a link to my github for reference.&lt;/p&gt;

&lt;p&gt;Happy coding &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Most Important Javascript Array Methods to Understand</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Tue, 11 Feb 2025 10:00:02 +0000</pubDate>
      <link>https://dev.to/limoh653/the-most-important-javascript-array-methods-to-understand-1062</link>
      <guid>https://dev.to/limoh653/the-most-important-javascript-array-methods-to-understand-1062</guid>
      <description>&lt;p&gt;An array in JavaScript is a global data structure used to store data. A global data structure is one which can be accessed everywhwere in your code.&lt;/p&gt;

&lt;p&gt;Arrays are important because it allows you to iterate over the stored data one by one because their indices are known. The biggest advantage of JavaScript arrays lies in the array methods.&lt;/p&gt;

&lt;p&gt;JavaScript array methods are built in functions that allow efficient traversal and manipulation of arrays. They provide functionalities like removing, searching and iterating through array elements, enhancing code readability and productivity.&lt;/p&gt;

&lt;p&gt;In this article we are going to cover fifteen most used array methods, which will make you a better JavaScript developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. map
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;map&lt;/code&gt; method allows you to loop on each element in the array and manipulate them according to your preference&lt;/p&gt;

&lt;p&gt;The map method creates a new array.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use the map method&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&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;timesBy2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;timesBy2&lt;/span&gt;&lt;span class="p"&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 8, 10, 12, 14, 16 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the &lt;code&gt;map&lt;/code&gt; method loops through each number in the array called numbers and multiplies it by two.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. filter
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;filter()&lt;/code&gt; array method creates a new array and returns the elements of the original array that passes a specific test implemented by a provided function.&lt;/p&gt;

&lt;p&gt;Here's a code block showing how the &lt;code&gt;filter&lt;/code&gt; method works&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&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;evens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evens&lt;/span&gt;&lt;span class="p"&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [output]
 [// output 2,4,6]

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

&lt;/div&gt;



&lt;p&gt;In the code above, we write a function which tests the array of numbers and returns only the even numbers using the filter method.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. slice
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;slice&lt;/code&gt; method returns  selected elements in an array from a start index to a (not inclusive) given end as a new array&lt;/p&gt;

&lt;p&gt;The method does not alter the original array.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&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;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 4,5 it selects from index 2 to 4 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above selects elements starting from index two to four but four will not be included in the output therefore, it will return index two and three.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. splice
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;splice&lt;/code&gt; method is used to add, remove or replace elements in an array. It overites the old array and returns an new array.&lt;/p&gt;

&lt;p&gt;Let's see how we use it to remove elements from an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2,3,4,5,6,7];

numbers.splice(2,2)

console.log(numbers)

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 2, 3, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above removes  two array elements starting from index two.&lt;/p&gt;

&lt;p&gt;The code below shows how to use &lt;code&gt;splice&lt;/code&gt; to add elements into an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// adding elements
const fruits = ['mango', 'banana', 'pawpaw'];

fruits.splice(2, 0, 'apples', 'oranges' )

console.log(fruits)

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[ 'mango', 'banana', 'apples', 'oranges', 'pawpaw' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we are adding two items from  index two while deleting none from the original array.&lt;/p&gt;

&lt;p&gt;Let's see how to use it to replace items&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;``` JavaScript =&lt;br&gt;
const fruits = [ 'mango', 'banana', 'apples', 'oranges', 'pawpaw' ]&lt;/p&gt;

&lt;p&gt;fruits.splice(1,1, 'blueberry')&lt;/p&gt;

&lt;p&gt;console.log(fruits)&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;



```text=
[output]
[ 'mango', 'blueberry', 'apples', 'oranges', 'pawpaw' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the code above, we are replacing one item from index one while also deleting an item in index one from the original array.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. forEach
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;forEach&lt;/code&gt; method loops through an array  and executes a provided function for each array element. In the code below we are printing all the elements from an array once&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const names = ['Boni', 'Alex', 'Clinton', 'Kevin'];

names.forEach((name) =&amp;gt; {console.log(name)});

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[
Boni
Alex
Clinton
Kevin
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. some
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;some&lt;/code&gt; method checks if some of the elements meets a certain condition passed in the function and returns true is some meet the condition and false if none meets the condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2,3,4,5,6,7];

const greaterThan1 = (number) =&amp;gt;  number &amp;gt; 1;

const isGreaterThan1 = numbers.some(greaterThan1)

console.log(isGreaterThan1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[true]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above checks if some numbers are greater than one and returns true because some numbers meet the condition otherwise it would have returned false.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. every
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;every&lt;/code&gt; method iterates through an array and checks if all the elements satisfies a certain condition passed via a callback function. It is used to verify if all the elements in an array satisfies a certain condition. It returns true if all elements satisfies the condition otherwise false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2,3,4,5,6,7];

const greaterThan1 = (number) =&amp;gt;  number &amp;gt; 4;

const isGreaterThan1 = numbers.every(greaterThan1)

console.log(isGreaterThan1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[output]
[false]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above returns false since all the numbers are not greater than four.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. fill
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;fill&lt;/code&gt; method fills specified elements into an array and overites the original array giving a new array. If the start and end positions are not specified it fills the whole array with the new element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Banana", "Pawpaw", "Apple", "Orange"];
fruits.fill("Avocado");

console.log(fruits)

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
[ 'Avocado', 'Avocado', 'Avocado', 'Avocado' ]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In the code above, the start and end position is not specified so it fills the whole array with `Avocado`

Now let's see the one where start and end positions are specified.

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 JavaScript=&lt;br&gt;
 const fruits = ["Banana", "Pawpaw", "Apple", "Orange"];&lt;br&gt;
fruits.fill("Avocado", 2,4);&lt;/p&gt;

&lt;p&gt;console.log(fruits)&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
 [output]&lt;br&gt;
 [ 'Banana', 'Pawpaw', 'Avocado', 'Avocado' ]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
The code above fills the array starting from index two to four but four not being included with the new element.

## 9. push
The `push` method adds new elements to the end of an array and changes the old array returning the new modified array. You can add a single element or many.

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
JavaScript&lt;br&gt;
const cars = ['Toyota', 'Mazda', 'Honda']&lt;br&gt;
cars.push('Mitsubishi', 'Nissan', 'Ford')&lt;/p&gt;

&lt;p&gt;console.log(cars)&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
[ 'Toyota', 'Mazda', 'Honda', 'Mitsubishi', 'Nissan', 'Ford' ]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
## 10. pop
The `pop` method removes the last element of an array and returns  a new array with  a missing last element.

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
JavaScript=&lt;br&gt;
const cars = ['Toyota', 'Mazda', 'Honda','Mitsubishi', 'Nissan', 'Ford'];&lt;br&gt;
cars.pop();&lt;/p&gt;

&lt;p&gt;console.log(cars);&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
 [ 'Toyota', 'Mazda', 'Honda', 'Mitsubishi', 'Nissan' ]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
In the code above, the last element which is 'Ford' has been removed.


## 11.unshift
 The method adds new elements to the beginning of an array. You can add a single element or multiple elements to the new array. It overwrites the original array.

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
JavaScript=&lt;br&gt;
 const fruits = ["Pawpaw", "Orange", "Apple", "Mango"];&lt;br&gt;
fruits.unshift("Cucumber","Pineapple");&lt;/p&gt;

&lt;p&gt;console.log(fruits)&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
 [output]&lt;br&gt;
 [ 'Cucumber', 'Pineapple', 'Pawpaw', 'Orange', 'Apple', 'Mango' ]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
## 12.shift
The `shift` method removes the first element of an array and returns a new array by overwriting the original array.

Here's is an example

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
JavaScript=&lt;br&gt;
const fruits = [ 'Cucumber', 'Pineapple', 'Pawpaw', 'Orange', 'Apple', 'Mango' ];&lt;/p&gt;

&lt;p&gt;fruits.shift();&lt;/p&gt;

&lt;p&gt;console.log(fruits)&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
[ 'Pineapple', 'Pawpaw', 'Orange', 'Apple', 'Mango' ]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
## 13. find
The `find` method is used to search for an element in an array. I t returns the first element which satisfies the condition passed ina a callback function. If no element satisfies the conditon it returns undefined.

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
javascript =&lt;br&gt;
const numbers =[1,2,3,4,5,6,7];&lt;/p&gt;

&lt;p&gt;const number = numbers.find((number) =&amp;gt; number &amp;gt; 5);&lt;/p&gt;

&lt;p&gt;console.log(number);&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
[6]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The code above returns 6 since it's first element to meet the condition greater than 5.

## 14. findIndex

The `findIndex` returns the position of the first element that satisfies a condition that was passed. It returns -1 if no match is found. It does not overwrite the original array. It returns true or false.

Here's an example

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
javascript&lt;br&gt;
const numbers =[1,2,3,4,5,6,7];&lt;/p&gt;

&lt;p&gt;const number = numbers.findIndex((number) =&amp;gt; number &amp;gt; 5);&lt;/p&gt;

&lt;p&gt;console.log(number);&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
[5]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
The code above returns 5 because 5 is the index of the first number that satisfies the condition greater than five.

## 15. includes
 The `includes` method checks if a specific element is present in an array. It returns true if its present or false if it's not present.

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
javascript=&lt;br&gt;
const numbers = [1,2,3,4]&lt;/p&gt;

&lt;p&gt;console.log(numbers.includes(3))&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
text=&lt;br&gt;
[output]&lt;br&gt;
[5]&lt;/p&gt;



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

## Conclusion
JavaScript array methods are very important built-in functions used to write scalable and readable code. This article covered fifteen most used array methods. I would recommend reading Javascript oficial documentation [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript) in order to understand more about the array methods. [Here](https://github.com/limoh653/JS-Array-methods) is a link to my github for reference.


Happy coding 










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

&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What is Redux Toolkit: When and How to use it Effectively?</title>
      <dc:creator>Hesbon limo</dc:creator>
      <pubDate>Fri, 24 Jan 2025 05:42:45 +0000</pubDate>
      <link>https://dev.to/limoh653/what-is-redux-toolkit-when-and-how-to-use-it-effectively-pll</link>
      <guid>https://dev.to/limoh653/what-is-redux-toolkit-when-and-how-to-use-it-effectively-pll</guid>
      <description>&lt;h1&gt;
  
  
  What is redux
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://redux-toolkit.js.org/" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; is a predictable  state management tool for JavaScript apps. A state is something that keeps the app running for example when you are asked to select gender in a website, the state is either male or female.&lt;/p&gt;

&lt;p&gt;Imagine that you are a customer of four banks and in each of those banks you have two accounts. Those will make eight accounts in total. Without Redux, when you want to ask balance or deposit you will have to visit each of the four banks. But with Redux all of these will be stored in one central location called a store.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why redux
&lt;/h2&gt;

&lt;p&gt;Redux make state management easier. Managing states of small applications is not hard but once an application has a lot of components with different states it becomes harder to manage. Components are independent and reusable pieces of code.&lt;/p&gt;

&lt;p&gt;Let's return to the example of banks above. Assuming you have twenty accounts with different banks and some of the banks are in other countries managing the accounts without redux would be nearly impossible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of using redux
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Centralized state management
&lt;/h3&gt;

&lt;p&gt;One of the advantage of using Redux is the ability to manage state in a centralized manner. In React application state is managed in components. However, when an application has so many components it becomes difficult to track the components.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Predictable state changes
&lt;/h3&gt;

&lt;p&gt;Redux enforces strict rules on how state is changed. It returns a new state insted of mutating the existing one therefore making it straight forward to predict the change of a state.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Simplifies data flow
&lt;/h3&gt;

&lt;p&gt;In a React application data flow may become complex  especially when components have to share state. In React data is passed from parent to child using props. Redux simplifies this by allowing any component to access a global state from a central store.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.Makes debugging easy
&lt;/h3&gt;

&lt;p&gt;Redux offers powerful tools such as Redux DevTools which can be intergrated with the browser. These allows the developer to see states in real time and easily identify errors in the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Improved code maintainability and readability
&lt;/h3&gt;

&lt;p&gt;Redux separates states with the main UI logic making it easy to maintain and read code. Actions and reducer are also kept distinct making it easy to read code file structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of redux
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Redux store
&lt;/h3&gt;

&lt;p&gt;Its the central bucket which stores all the states of an application. It should be maintained as a single source of the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Actions
&lt;/h3&gt;

&lt;p&gt;States in redux are read only. Therefore, when you want to update or delete something in the store you need to express your intentions by emmitting or dispatching an action.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Reducer
&lt;/h3&gt;

&lt;p&gt;Reducer takes in two things, the current state and the action. They the update it to one entity the new state.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Dispatcher
&lt;/h3&gt;

&lt;p&gt;The dispatcher is a function used to send actions e.g add Todo to the redux store which is then processed by the reducer and updated accordingly.&lt;/p&gt;

&lt;p&gt;Here is a diagram showing the redux components in detail &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%2Ff00zxv5tpsh12nomlna4.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%2Ff00zxv5tpsh12nomlna4.jpg" alt="give_your_react_apps_an_edge_mastering_redux_toolkit_0_9c97663b53" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Project - To Do List Application
&lt;/h2&gt;

&lt;p&gt;Now that you know the basics of Redux, it's time to apply this knowledge in creating a real world app. We will build a simple To Do application where you can add and delete tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Prequisites
&lt;/h3&gt;

&lt;p&gt;To build this app you will need a code editor eg sublime text or VS code. For this tutorial I will use &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;&lt;br&gt;
.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Setting up the Project
&lt;/h3&gt;

&lt;p&gt;Create a new react app by running the following command in the terminal.Make sure you replace "your-project-name" with the name of your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest your-project-name -- --template react
cd your-project-name
npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command will create a new react app and install the necessary dependancies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Getting Started with Redux
&lt;/h3&gt;

&lt;p&gt;Redux requires some dependancies for it to function well, namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Redux:&lt;/strong&gt; The main library that enables the Redux architecture&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;React redux:&lt;/strong&gt; Enables connection between the React components and the Redux store&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Redux DEvTools Extension:&lt;/strong&gt; Connects your Redux application to redux Devtools&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;redux Toolkit:&lt;/strong&gt; It makes it easier to create Redux store and intergrate middleware like &lt;a href="https://www.freecodecamp.org/news/how-to-work-with-redux-thunk/" rel="noopener noreferrer"&gt;Redux-thunk&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install them using npm 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="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="o"&gt;\&lt;/span&gt;
&lt;span class="nx"&gt;redux&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;redux&lt;/span&gt;&lt;span class="o"&gt;\&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&lt;/span&gt;&lt;span class="err"&gt;\
&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;devtools&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;extension&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: How to create the redux store
&lt;/h3&gt;

&lt;p&gt;Now that we have installed everyting and our setup is ready, navigate to the &lt;code&gt;src&lt;/code&gt; folder and create a file and name it &lt;code&gt;store.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

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


export const store= configureStore({
    reducer: [],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function of the configireStore we have imported in the first line will help us create the store. We will then create the store  by declaring a function named store.&lt;/p&gt;

&lt;p&gt;Next, we create an empty reducer where all our state will be handled. Finally in line  4 we export the store so that we can use it in the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Setting up the reducers and actions in the file structure
&lt;/h3&gt;

&lt;p&gt;Now let's create the reducer in our application. for these code I will combine the reducer and the actions. An action is simply what happens in the application e.g deleting  task.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;src&lt;/code&gt; directory create a new folder called &lt;code&gt;features&lt;/code&gt;, and inside that folder create a file called &lt;code&gt;todoSlice.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/features/todoSlice.js
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
  todos: [],
};

const todoSlice = createSlice({
  name: 'todos',
  initialState,
  reducers: {
    addTodo: (state, action) =&amp;gt; {
      // Ensure you are adding the new todo with an id and text
      const newTodo = {
        id: Date.now(),  // Generate a unique id
        text: action.payload,
      };
      state.todos.push(newTodo);  // Add the new todo to the state
    },
    removeTodo: (state, action) =&amp;gt; {
      state.todos = state.todos.filter((todo) =&amp;gt; todo.id !== action.payload);
    },
  },
});

export const { addTodo, removeTodo } = todoSlice.actions;
export default todoSlice.reducer;

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

&lt;/div&gt;



&lt;p&gt;In line three, we are defining  an initial state. In our case an empty array of todos. Then we use the &lt;code&gt;createSlice&lt;/code&gt; which is an helper function from the reduxToolkit used to create a slice which is a single piece in the redux. &lt;/p&gt;

&lt;p&gt;The slice has the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initial state.&lt;/strong&gt; This is the default state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reducer.&lt;/strong&gt; This is a function that handles how a state changes in response to an action e.g addTodo&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Action.&lt;/strong&gt; It is inside the reducer and handles what actually changes e.g remove or add todo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We then export the actions so that we can use them in our components. After that we export the Reducer so that it can be used in the Redux store.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6 : Connecting the store to the main application
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Provider&lt;/code&gt; from the &lt;code&gt;react-redux&lt;/code&gt; library is all we need to connect the redux store to the ToDo application.&lt;/p&gt;

&lt;p&gt;First we import the &lt;code&gt;provider&lt;/code&gt; from the react react redux. Then we wrap our &lt;code&gt;app&lt;/code&gt; component with ths &lt;code&gt;provider&lt;/code&gt; function and pass the store as a prop in the &lt;code&gt;main.jsx&lt;/code&gt; or &lt;code&gt;index.js&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;StrictMode&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;react&lt;/span&gt;&lt;span class="dl"&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="s1"&gt;react-dom/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./index.css&lt;/span&gt;&lt;span class="dl"&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="s1"&gt;./App.jsx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&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="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&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;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;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;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;/StrictMode&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;,
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 7: Getting started with dispatch actions
&lt;/h3&gt;

&lt;p&gt;Now that we have set up the reducer and the actions we will move on and create components that dispatch the actions.&lt;/p&gt;

&lt;p&gt;Let's create a new folder named "components" inside the src directory. In this folder, we will create two files &lt;code&gt;AddTodo.js&lt;/code&gt; and &lt;code&gt;todo.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;AddTodo.js&lt;/code&gt; component will be responsible for adding tasks. Before we proceed lets import the following into the file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React library for creating react components and &lt;code&gt;useState&lt;/code&gt; a React hook that enables you to add state to a functional component&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;useDispatch&lt;/strong&gt;: A &lt;code&gt;react-redux&lt;/code&gt; hook that provides the dispatch functin which is used to send actions to redux store, triggering state updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;addTodo&lt;/strong&gt;: Imports addTodo action from todoSlice which is used to add a new todo item to the Redux store
&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&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="s1"&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;useDispatch&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;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;addTodo&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;../features/todoSlice&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./AddTodo.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Import the CSS file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's proceed to write code for &lt;code&gt;addTodo.js&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;AddTodo&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;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInput&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;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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTodoHandler&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="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="nf"&gt;preventDefault&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="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;())&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="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;form&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;addTodoHandler&lt;/span&gt;&lt;span class="p"&gt;}&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;add-todo-form&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;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;add-todo-input&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 a Todo...&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;input&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;setInput&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="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="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;add-todo-button&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;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;/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="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;AddTodo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we created a component consisting of a button and the input field. When a user clicks  "Add Todo" button , the AddTodo function is executed. The function uses the &lt;code&gt;input.trim()&lt;/code&gt; to remove leading and trailing white spaces from the input and then dispatches the addTodo action with the new task as the payload.&lt;/p&gt;

&lt;p&gt;Now, let's move on to the &lt;code&gt;todo.js&lt;/code&gt; responsible for rendering the list of tasks and also deleting tasks. &lt;/p&gt;

&lt;p&gt;Let's import the following to achieve this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React:&lt;/strong&gt;  for creating react components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;useSelector:&lt;/strong&gt; for retreiving specific pieces of state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;useDispatch:&lt;/strong&gt; for dispatching actions to redux store.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;removeTodo:&lt;/strong&gt; for deleting an item.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { removeTodo } from '../features/todoSlice'; // Adjust the import path based on your project structure
import './todo.css'; // Import the CSS file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's write the code for &lt;code&gt;todo.js&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Todos&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="nx"&gt;todos&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;todos&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;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;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;todos-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;h1&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;todos-header&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;Todos&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&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;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="p"&gt;?&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;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="o"&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="nx"&gt;div&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;todo&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;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-item&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;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-text&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;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&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&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;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;removeTodo&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;id&lt;/span&gt;&lt;span class="p"&gt;))}&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-delete-button&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;/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="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="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;no-todos&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;No&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="nx"&gt;available&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&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;/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;Todos&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above checks If todos.length &amp;gt; 0 (i.e., there are to-do items), the component maps through the todos array and renders each item.&lt;br&gt;
If todos.length === 0, a message (No todos available.) is displayed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;onClick:&lt;/code&gt; When the button is clicked, it dispatches the &lt;code&gt;removeTodo&lt;/code&gt; action with the &lt;code&gt;todo.id&lt;/code&gt; as the payload. This action will remove the specific todo from the Redux store.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, import the components into your &lt;code&gt;App.jsx&lt;/code&gt; file and render them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import AddTodo from './components/AddTodo';
import Todos from './components/todo';


function App() {
  return (
    &amp;lt;div className="container mx-auto p-6"&amp;gt;
      &amp;lt;h1 className="text-4xl font-bold text-center mb-6"&amp;gt;Todo App&amp;lt;/h1&amp;gt;


        &amp;lt;AddTodo /&amp;gt;
        &amp;lt;Todos /&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}

export default App;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 8: Getting Started with Redux Devtools
&lt;/h3&gt;

&lt;p&gt;To get started, download the Redux DevTools&lt;a href="https://chromewebstore.google.com/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en" rel="noopener noreferrer"&gt;Redux DevTools Extension &lt;/a&gt; Extension for your browser.&lt;/p&gt;

&lt;p&gt;After the istallation, your browser DevTools will add a new tab specifically for Redux.&lt;br&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%2Fokwb41gypu2pjcuaop5x.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%2Fokwb41gypu2pjcuaop5x.png" alt="Screenshot from 2025-01-08 20-25-53" width="554" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will then click on the "State" tab within the Redux DevTools to show you the entire state of your Redux store and any actions that have been dispatched and their payloads.&lt;/p&gt;

&lt;p&gt;In the Redux DevTools, click "state". This will show you the entire state of your Redux store and the dispatched actions and their payload.&lt;/p&gt;
&lt;h3&gt;
  
  
  Final Step
&lt;/h3&gt;

&lt;p&gt;After implementing everything as shown in the tutorial the final product should look like the one below.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4X3sjICpZBQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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

&lt;p&gt;Redux is a very powerful state management tool used by a lot of developers all over the world. In this article I have discussed in detail what is Redux, it's advantages and finally a demo project on how to use the Redux tool practically. You can search on YouTube more tutorials about Redux and I am confident that you will grasp the content.&lt;/p&gt;

&lt;p&gt;End ...&lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
