<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mayank Kashyap</title>
    <description>The latest articles on Forem by Mayank Kashyap (@mayankkashyap681).</description>
    <link>https://forem.com/mayankkashyap681</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%2F965464%2F152cdba9-2344-46b4-b576-61aa0ce56cab.jpg</url>
      <title>Forem: Mayank Kashyap</title>
      <link>https://forem.com/mayankkashyap681</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mayankkashyap681"/>
    <language>en</language>
    <item>
      <title>Multiple Checkbox Form in React Js</title>
      <dc:creator>Mayank Kashyap</dc:creator>
      <pubDate>Sun, 25 Dec 2022 14:15:44 +0000</pubDate>
      <link>https://forem.com/mayankkashyap681/multiple-checkbox-form-in-react-js-3elb</link>
      <guid>https://forem.com/mayankkashyap681/multiple-checkbox-form-in-react-js-3elb</guid>
      <description>&lt;p&gt;Multiple Checkbox list is very often seen on the websites like Amazon, Flipkart etc.&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%2Fhy8qavg31y69zrxk5hxc.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%2Fhy8qavg31y69zrxk5hxc.png" alt="Image description" width="800" height="570"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So today we are going to build multiple checkbox list from scratch using React.&lt;br&gt;
So, our final result will look like the gif 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%2Fnxrn82lgbh1iwbslvskl.gif" 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%2Fnxrn82lgbh1iwbslvskl.gif" alt="Image description" width="469" height="157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;App.js&lt;/strong&gt;, make a React project and you will find it over there.&lt;br&gt;
You can create a separate file for the Checkbox list or simple write your code in the App.js but I prefer to create a new file for Checkbox list. Import the Checkbox from the File Checkbox.js and make it a component. Styling is up to you. You can style it anyway you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./styles.css";
import Checkbox from "./Checkbox";

export default function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Checkbox /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our Checkbox.js&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";

const Checkbox = () =&amp;gt; {

  const list = [
    "checkbox1",
    "checkbox2",
    "checkbox3",
    "checkbox4",
    "checkbox5"
  ];

  return (
    &amp;lt;div
      className="selector-bg"
      style={{ background: "aliceblue", height: "100vh", margin: "20px" }}
    &amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;h1
          style={{
            textAlign: "center",
            fontWeight: "bold",
            textTransform: "uppercase",
            fontSize: "20px"
          }}
        &amp;gt;
          Select List
        &amp;lt;/h1&amp;gt;
        &amp;lt;form&amp;gt;
          &amp;lt;div style={{ padding: "20px" }}&amp;gt;
            {list.map((val) =&amp;gt; (
              &amp;lt;div
                style={{
                  display: "flex",
                  backgroundColor: "yellowgreen",
                  padding: "10px",
                  margin: "12px"
                }}
                key={val}
              &amp;gt;
                &amp;lt;input
                  type="checkbox"
                  id={val}
                  value={val}
                  name={val}
                  style={{ marginRight: "10px" }}
                /&amp;gt;
                &amp;lt;label for={val}&amp;gt;{val}&amp;lt;/label&amp;gt;
              &amp;lt;/div&amp;gt;
            ))}
          &amp;lt;/div&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Checkbox;

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

&lt;/div&gt;



&lt;p&gt;So in the Checkbox.js we have firstly made the list of all the tags which we want to display and to keep everything simple I have named them checkbox1, checkbox2, checkbox3 and so on you can also replace them with some real tags like brand names, color names or whatever you like.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;const list = [ "checkbox1", "checkbox2", "checkbox3", "checkbox4", "checkbox5"];&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;we are making this list so that we don't need to hardcode tags and write repetitive code, instead of that we can simply use map on the list of the tags and display them by writing code once.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div&lt;br&gt;
      className="selector-bg"&lt;br&gt;
      style={{ background: "aliceblue", height: "100vh", margin: "20px" }}&lt;br&gt;
    &amp;gt;&lt;br&gt;
      &amp;lt;div&amp;gt;&lt;br&gt;
        &amp;lt;h1&lt;br&gt;
          style={{&lt;br&gt;
            textAlign: "center",&lt;br&gt;
            fontWeight: "bold",&lt;br&gt;
            textTransform: "uppercase",&lt;br&gt;
            fontSize: "20px"&lt;br&gt;
          }}&lt;br&gt;
        &amp;gt;&lt;br&gt;
          Select List&lt;br&gt;
        &amp;lt;/h1&amp;gt;&lt;br&gt;
        &amp;lt;form&amp;gt;&lt;br&gt;
          &amp;lt;div style={{ padding: "20px" }}&amp;gt;&lt;br&gt;
            {list.map((val) =&amp;gt; (&lt;br&gt;
              &amp;lt;div&lt;br&gt;
                style={{&lt;br&gt;
                  display: "flex",&lt;br&gt;
                  backgroundColor: "yellowgreen",&lt;br&gt;
                  padding: "10px",&lt;br&gt;
                  margin: "12px"&lt;br&gt;
                }}&lt;br&gt;
                key={val}&lt;br&gt;
              &amp;gt;&lt;br&gt;
                &amp;lt;input&lt;br&gt;
                  type="checkbox"&lt;br&gt;
                  id={val}&lt;br&gt;
                  value={val}&lt;br&gt;
                  name={val}&lt;br&gt;
                  style={{ marginRight: "10px" }}&lt;br&gt;
                /&amp;gt;&lt;br&gt;
                &amp;lt;label for={val}&amp;gt;{val}&amp;lt;/label&amp;gt;&lt;br&gt;
              &amp;lt;/div&amp;gt;&lt;br&gt;
            ))}&lt;br&gt;
          &amp;lt;/div&amp;gt;&lt;br&gt;
        &amp;lt;/form&amp;gt;&lt;br&gt;
      &amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the code written above we have only done some styling and created a form, iterated over the list of tags and displayed them. After all this we will get the output something like the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs42k2qqp9xn0mo3m96wy.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%2Fs42k2qqp9xn0mo3m96wy.png" alt="Image description" width="800" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, lets add state and add logic to make things work&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Import useState() from React&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;import {useState} from 'react'&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A). Add a state checked to keep track of all the tags which are checked.&lt;br&gt;
&lt;em&gt;const [checked, setChecked] = useState([]);&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;B). Add onChange Event handler on the input field &lt;br&gt;
&lt;em&gt;onChange={() =&amp;gt; handleToggle(val)}&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;C). Write the logic for the handleToggle function, so that whenever a box is checked or unchecked the handleToggle function get triggered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleToggle = (val) =&amp;gt; {
    var currentIndex = checked.indexOf(val);
    var newArr = [...checked];

    if (currentIndex === -1) {
      newArr.push(val);
    } else {
      newArr.splice(currentIndex, 1);
    }

    setChecked(newArr);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Handle Toggle function doing following things&lt;br&gt;
1). It is finding the index of the tag in the checked array,&lt;br&gt;
if the tag is present then we will get the index, otherwise we will get -1&lt;br&gt;
2). we are creating another array newArray and initializing it with all the tags which are currently in the checked array, in simple words we are making copy.&lt;br&gt;
3). If the currentIndex is -1, it means the tag is not present in the array so we are pushing it in the newArr by using push() method.&lt;br&gt;
4). If the currentIndex is not equal to -1 it means the tag is already checked thus present in the checked array, so we are removing it using splice() method.&lt;br&gt;
5). At last we are setting the checked array using setChecked(newArr);&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To display all the checked tags we are adding another div which get diplayed only when the size of checked array is greater than 0 means the checked array is having atleast one element&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; &amp;lt;div style={{ display: "flex", backgroundColor: "gray" }}&amp;gt;
        {checked.map((item) =&amp;gt; (
          &amp;lt;p
            key={item}
            style={{
              margin: "10px",
              color: "whitesmoke",
              border: "2px solid greenyellow",
              padding: "2px"
            }}
          &amp;gt;
            {item}
          &amp;lt;/p&amp;gt;
        ))}
      &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;The link for the final code of Checkbox.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/MaYaNkKashyap681/React-Native-Features-Implementation/blob/main/Selection%20Checkbox%20in%20React.js" rel="noopener noreferrer"&gt;https://github.com/MaYaNkKashyap681/React-Native-Features-Implementation/blob/main/Selection%20Checkbox%20in%20React.js&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Pagination Concept using JavaScript</title>
      <dc:creator>Mayank Kashyap</dc:creator>
      <pubDate>Thu, 22 Dec 2022 20:01:32 +0000</pubDate>
      <link>https://forem.com/mayankkashyap681/pagination-concept-using-javascript-5coo</link>
      <guid>https://forem.com/mayankkashyap681/pagination-concept-using-javascript-5coo</guid>
      <description>&lt;p&gt;&lt;strong&gt;Pagination&lt;/strong&gt; is basically dividing the entire data into parts of some specific size and showing one part or set of data on one page. To view other set of data we simply change the page.&lt;/p&gt;

&lt;p&gt;For Example: &lt;br&gt;
Let say we have a website which shows information of the users.&lt;br&gt;
So, when we make an API request we get data of all the users, if we display all the users data on single page then it is not a good practice thus we use pagination in which we display fixed numbers of users on one page and if we want to see other users the we change the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Paginations&lt;/strong&gt;&lt;br&gt;
There are two ways to do pagination &lt;br&gt;
1). &lt;em&gt;At Client Side&lt;/em&gt; -&amp;gt; The example above is client side pagination. In client side pagination we fetch all the data from the server and put pagination logic on client side. But if the dataset is very large then we require large space to store it.&lt;/p&gt;

&lt;p&gt;2). &lt;em&gt;At Server Side&lt;/em&gt; -&amp;gt; &lt;br&gt;
In server side pagination we put the logic of pagination on the server side and get fixed amount of data from the server itself for every page, but the main issue with server side pagination is calling an API for every page change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this blog we look the concept of pagination using Javascript.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let say we have an array of 20 items &lt;br&gt;
data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20].&lt;br&gt;
consider this &lt;strong&gt;data&lt;/strong&gt; array the data which we are getting form the Api [Client Side] or the data which is stored in the database[Server Side].&lt;/p&gt;

&lt;p&gt;we will try to understand the logic of pagination using this data array.&lt;br&gt;
For pagination we need to have some variables&lt;br&gt;
a). &lt;strong&gt;total_no_of_items&lt;/strong&gt; -&amp;gt; It refers to total number of items, which in this case is &lt;strong&gt;20&lt;/strong&gt; [data.length = 20].&lt;br&gt;
b). &lt;strong&gt;items_per_page&lt;/strong&gt; -&amp;gt; Items which we want to be displayed on one page, let say we want 5 items per page.&lt;br&gt;
c). &lt;strong&gt;no_of_pages&lt;/strong&gt; -&amp;gt; It refers in how many pages we can divide our entire data.&lt;/p&gt;

&lt;p&gt;In this case&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;no_of_pages = total_no_of_items / items_per_page&lt;br&gt;
[20 / 5 = 4]&lt;br&gt;
no_of_pages = 4&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;d). &lt;strong&gt;page_number&lt;/strong&gt; -&amp;gt; The page number which we are currently on.&lt;/p&gt;

&lt;p&gt;These are the items which a particular page number will have for the above case&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Page 1 -&amp;gt; [1 - 5]&lt;br&gt;
Page 2 -&amp;gt; [6 - 10]&lt;br&gt;
Page 3 -&amp;gt; [11 - 15]&lt;br&gt;
Page 4 -&amp;gt; [16 - 20]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we are on page_number 3, so we have [11 - 15] displayed.&lt;br&gt;
Let's look at the logic&lt;br&gt;
&lt;strong&gt;items_to_skip&lt;/strong&gt; =  (page_number - 1) * items_per_page &lt;br&gt;
[(3 - 1)*5 = 10] &lt;br&gt;
Thus we need to skip 10 items, and we have to show 5 items per page. &lt;br&gt;
So we can do,&lt;br&gt;
items = data.slice(items_to_skip, items_to_skip + items_per_page)&lt;br&gt;
items array will have the items numbered from [11 - 15].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

const total_no_of_items = data.length;
const items_per_page = 5;
let no_of_pages = total_no_of_items / items_per_page;
let page_number = 3;

const items_to_skip = (page_number - 1) * items_per_page;
const items = data.slice(items_to_skip, items_per_page + items_to_skip);
console.log(items)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[11, 12, 13, 14, 15]&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Promises and Asynchronous Javascript - Part 1</title>
      <dc:creator>Mayank Kashyap</dc:creator>
      <pubDate>Wed, 21 Dec 2022 21:11:40 +0000</pubDate>
      <link>https://forem.com/mayankkashyap681/promises-and-asynchronous-javascript-part-1-151</link>
      <guid>https://forem.com/mayankkashyap681/promises-and-asynchronous-javascript-part-1-151</guid>
      <description>&lt;p&gt;As we know Javascript is Single Threaded programming language, it means the code is executed line by line and one after another.&lt;br&gt;
Thus, we can say javascript is Synchronous in nature.&lt;/p&gt;

&lt;p&gt;Asynchronous javascript enables us to run the tasks which do not take much time and keeping the tasks aside which take much longer time for there execution, because of this technique we don't have to wait for the tasks which require more time for there execution and stopping the program at same point till the execution of that that function is not over, instead we run the other tasks without stopping the program at single point.&lt;/p&gt;

&lt;p&gt;Now let's see some code examples to understand nature of javascript in detail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hi)
console.log("How are you?")
console.log("Bye)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Output of the code above is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hi&lt;br&gt;
How are you?&lt;br&gt;
Bye&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Same code using setTimeout().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hi)
setTimeout(()=&amp;gt;{
console.log("How are you?")
},1000);
console.log("Bye)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the output will be&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hi&lt;br&gt;
Bye&lt;br&gt;
How are you?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now this may seems little confusing to you, &lt;strong&gt;is it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets find out why we are getting this output&lt;/p&gt;

&lt;p&gt;So we are using setTimeout() function in the code above, setTimeout() is a function which we get from the browser's webApi.&lt;br&gt;
Whatever is written inside the setTimeout() function is executed after the milli seconds which we mention in it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setTimeout(() =&amp;gt; {console.log("Hello")}, 1000);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you run the code above then &lt;strong&gt;&lt;em&gt;Hello&lt;/em&gt;&lt;/strong&gt; will be printed on the console after 1000ms or 1s &lt;strong&gt;[1000ms = 1s]&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Let's see one more code example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myName(name) {
setTimeout(() =&amp;gt; {
return "My name is" + name;
}, 1000)
}
console.log("Hi)
const _name = myName("John");
console.log(_name);
console.log("Bye)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Output of the above code is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hi&lt;br&gt;
undefined&lt;br&gt;
Bye&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you may think why we are getting undefined &lt;strong&gt;yes?&lt;/strong&gt;&lt;br&gt;
we are using setTimeout() inside the myName() function thus the myName() function will return &lt;strong&gt;&lt;em&gt;My name is John&lt;/em&gt;&lt;/strong&gt; after 1s.&lt;br&gt;
and as we know GEC is created inside the javascipt which maps all the variables initially to undefined in the memory allocation phase and assign values to them in the code execution phase when the line in which the value is assigned to the variable in the code is encountered. Thus initially the variable _name is undefined and after 1s it get the value &lt;strong&gt;My name is John&lt;/strong&gt; but due to synchronous nature of javascript &lt;strong&gt;undefined&lt;/strong&gt; is printed on the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is end of the Part-1&lt;/strong&gt;, I hope you got an idea about the nature of the javascript and what is the issue with synchronous nature of the javascript, we will learn about &lt;strong&gt;callbacks and promises&lt;/strong&gt; in javascript in part-2. &lt;/p&gt;

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