<?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: Shahid Alam</title>
    <description>The latest articles on DEV Community by Shahid Alam (@cjreads665).</description>
    <link>https://dev.to/cjreads665</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%2F301404%2Fa0effd51-575e-4e6f-a1cb-9d9147feacac.jpg</url>
      <title>DEV Community: Shahid Alam</title>
      <link>https://dev.to/cjreads665</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cjreads665"/>
    <language>en</language>
    <item>
      <title>Use puppeteer with Node to create PDFs!</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Fri, 06 Dec 2024 09:24:12 +0000</pubDate>
      <link>https://dev.to/cjreads665/use-puppeteer-with-node-to-create-pdfs-1bem</link>
      <guid>https://dev.to/cjreads665/use-puppeteer-with-node-to-create-pdfs-1bem</guid>
      <description>&lt;h1&gt;
  
  
  How to Create a PDF Generator Using Puppeteer and Node.js
&lt;/h1&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%2F4cwbe64yvutr0u9ogb83.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%2F4cwbe64yvutr0u9ogb83.gif" alt="Alt Text" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generating dynamic PDFs is a common requirement in web development. Whether it's for invoices, reports, or resumes, creating a robust PDF generator is an essential skill for developers. In this article, I'll walk you through building a PDF generator using &lt;strong&gt;Node.js&lt;/strong&gt; and &lt;strong&gt;Puppeteer&lt;/strong&gt;, a powerful headless browser library.&lt;/p&gt;

&lt;p&gt;As an example, I used this same method to build my &lt;strong&gt;CV Maker Project&lt;/strong&gt;, which takes user input and dynamically generates a PDF. You can see the live demo of the project here: &lt;a href="https://resmerise.netlify.app/" rel="noopener noreferrer"&gt;Live Demo&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;This guide is written in plain, beginner-friendly language. If any part feels unclear, feel free to leave a comment, and I’ll address it as soon as possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we begin, ensure you have the following:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node.js&lt;/strong&gt; installed on your machine.
&lt;/li&gt;
&lt;li&gt;A basic &lt;strong&gt;Node.js project skeleton&lt;/strong&gt; set up.
&lt;/li&gt;
&lt;li&gt;Puppeteer installed as a dependency in your project:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm &lt;span class="nb"&gt;install &lt;/span&gt;puppeteer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’d like to reference the complete source code for this tutorial, check out my &lt;a href="https://github.com/cjreads665/back-resume/blob/master/controllers/PdfController.ts" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.  &lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Create a POST Endpoint for Receiving Data
&lt;/h2&gt;

&lt;p&gt;First, create an endpoint where the client can send data to generate a PDF. For this, we’ll define a simple &lt;code&gt;POST&lt;/code&gt; route in our &lt;code&gt;index.js&lt;/code&gt; (or equivalent main server file).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;postData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Storing the data sent from the client&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This endpoint will receive the data that needs to be included in the PDF.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Create a Controller File
&lt;/h2&gt;

&lt;p&gt;To keep the code organized, create a folder named &lt;code&gt;controllers&lt;/code&gt; in your project directory. Inside this folder, create a file named &lt;code&gt;PdfController.js&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;controller file&lt;/strong&gt; is where we’ll write the logic for generating the PDF. This keeps our code modular and makes it easier to maintain.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Write Logic for PDF Generation
&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;PdfController.js&lt;/code&gt;, add the following code to generate a PDF using Puppeteer:&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;export&lt;/span&gt; &lt;span class="k"&gt;default &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;browser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;puppeteer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;headless&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;--no-sandbox&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;--disable-gpu&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;newPage&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;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;postData&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/h1&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emulateMediaType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;screen&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resume.pdf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;printBackground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PDF created&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;browser&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;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what this code does:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Launches a Puppeteer browser instance in &lt;strong&gt;headless mode&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Opens a new page and sets the content using the data provided by the user.
&lt;/li&gt;
&lt;li&gt;Converts the page content into a PDF and saves it as &lt;code&gt;resume.pdf&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;Closes the browser to free up resources.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 4: Use the &lt;code&gt;PdfController&lt;/code&gt; Function in the POST Endpoint
&lt;/h2&gt;

&lt;p&gt;Now, connect the &lt;code&gt;PdfController&lt;/code&gt; function to the &lt;code&gt;POST&lt;/code&gt; endpoint in &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="nx"&gt;PdfController&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./controllers/controller.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;postData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Storing the data sent from the client&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;PdfController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that whenever the &lt;code&gt;/data&lt;/code&gt; endpoint is called, the logic for generating the PDF will be executed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Verify the PDF Generation
&lt;/h2&gt;

&lt;p&gt;At this point, you can test the endpoint using &lt;strong&gt;Postman&lt;/strong&gt; or any other HTTP client. When you send a &lt;code&gt;POST&lt;/code&gt; request with the relevant text data, a PDF named &lt;code&gt;resume.pdf&lt;/code&gt; will be created in the project’s root directory.  &lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Send the PDF Back to the Client
&lt;/h2&gt;

&lt;p&gt;To send the generated PDF back to the client as a response, install the &lt;code&gt;fs-extra&lt;/code&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;fs-extra
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then update the &lt;code&gt;POST&lt;/code&gt; endpoint as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;PdfController&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./controllers/controller.js&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;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs-extra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;postData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Storing data sent from the client&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;PdfController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Read the generated PDF file&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./resume.pdf&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;stat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;statSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./resume.pdf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Set response headers for the file&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Length&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/pdf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Disposition&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;attachment; filename=resume.pdf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Send the file back to the client&lt;/span&gt;
    &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PDF download sent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code sends the &lt;code&gt;resume.pdf&lt;/code&gt; file as a downloadable attachment to the client.&lt;/p&gt;




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

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

&lt;p&gt;And that’s it! 🎉 You’ve built a complete PDF generator using &lt;strong&gt;Node.js&lt;/strong&gt; and &lt;strong&gt;Puppeteer&lt;/strong&gt;. This setup dynamically generates PDFs based on user input and sends them back as downloadable files.&lt;/p&gt;

&lt;p&gt;Here’s what you achieved:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a Node.js server with a &lt;code&gt;POST&lt;/code&gt; endpoint.
&lt;/li&gt;
&lt;li&gt;Used Puppeteer to generate PDFs dynamically.
&lt;/li&gt;
&lt;li&gt;Sent the generated PDF back to the client.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;If you’d like to generate &lt;strong&gt;well-structured and formatted PDFs&lt;/strong&gt; (e.g., resumes or invoices with tables and styles), let me know in the comments. I’d love to write another tutorial for advanced use cases.  &lt;/p&gt;

&lt;p&gt;Also, feel free to check out the &lt;a href="https://github.com/cjreads665/back-resume" rel="noopener noreferrer"&gt;complete project code&lt;/a&gt;. It’s well-documented and beginner-friendly.&lt;/p&gt;

&lt;p&gt;Have a nice day!&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Node.js 20.6.0: Say Goodbye to 'dotenv'</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Sat, 09 Sep 2023 05:55:22 +0000</pubDate>
      <link>https://dev.to/cjreads665/nodejs-2060-say-goodbye-to-dotenv-2ijl</link>
      <guid>https://dev.to/cjreads665/nodejs-2060-say-goodbye-to-dotenv-2ijl</guid>
      <description>&lt;h2&gt;
  
  
  Unveiling Node.js 20.6.0
&lt;/h2&gt;

&lt;p&gt;If you're a Node.js developer, you're probably familiar with the 'dotenv' package. It's been a trusty companion for managing environment variables in your Node.js applications. However, with the recent release of Node.js version 20.6.0, managing environment variables just got a whole lot easier, and the need for 'dotenv' is becoming a thing of the past.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's New in Node.js 20.6.0?
&lt;/h2&gt;

&lt;p&gt;Node.js is constantly evolving, and version 20.6.0 brings some exciting updates. One of the most notable changes is the built-in support for .env files. This means you no longer need to rely on external packages like 'dotenv' to handle your environment variables. Node.js itself can now seamlessly load them from a .env file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Is This a Big Deal?
&lt;/h2&gt;

&lt;p&gt;🔒Enhanced Security: Now you don't have to depend on third-party packages for importing and using .env variables in your code eliminating the risk of any kind of vulnerability.&lt;/p&gt;

&lt;p&gt;🌐Streamlined Workflow: With built-in .env file support, your development workflow becomes more straightforward. You don't need to install and configure 'dotenv' or any other third-party packages. Node.js takes care of it for you, reducing the complexity of your project setup(and lighter!).&lt;/p&gt;

&lt;p&gt;💼Cleaner and More Maintainable Code: Hardcoding environment variables into your code can make it messy and hard to maintain. It's also a hassle to change them when needed. By using .env files, you keep your codebase clean and make it easier to update configurations as your project evolves.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use .env Files in Node.js 20.6.0
&lt;/h2&gt;

&lt;p&gt;Using .env files in Node.js 20.6.0 is straightforward:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a .env File: Start by creating a .env file in the root directory of your project. In this file, you can define your environment variables, each with its corresponding value. For example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API_KEY=mysecretkey
DATABASE_URL=mongodb://localhost/mydb
DEBUG=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run Your Application: To load the environment variables from your .env file, simply run your Node.js application with the --env-file flag, like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--env-file&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, why wait? Upgrade to Node.js 20.6.0 and start enjoying the benefits of a cleaner, more secure, and streamlined development process. Your Node.js projects will thank you!&lt;/p&gt;

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

</description>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>How To Use Redux Toolkit with React - The Easy Way</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Thu, 16 Mar 2023 16:21:59 +0000</pubDate>
      <link>https://dev.to/cjreads665/redux-toolkit-the-easy-way-2c8c</link>
      <guid>https://dev.to/cjreads665/redux-toolkit-the-easy-way-2c8c</guid>
      <description>&lt;h2&gt;
  
  
  Redux seemed way too complicated (skippable)
&lt;/h2&gt;

&lt;p&gt;Redux a popular tool that's often mentioned in tech Discord groups and numerous job posts. In fact, nowadays if you're familiar with React, it's practically a given that you should also know Redux.&lt;/p&gt;

&lt;p&gt;When I decided to learn Redux, I quickly realized that setting it up could be a bit of a headache. I spent hours watching 15-minute-long videos that attempted to explain what a reducer, store, and Redux actually are. But the more I watched, the more confused I became.&lt;/p&gt;

&lt;p&gt;After reading a variety of articles, I finally managed to set up a Redux store and begin to understand it. However, I realized that it would be helpful to simplify the process of learning Redux for others. That's why I'm writing this post - to provide a clear and straightforward explanation of Redux Toolkit that anyone can use in their application. If you have any feedback or notice any errors in my code, please don't hesitate to leave a comment below. I'll be sure to address it as soon as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; - in case you are overwhelmed with my explanation, feel free to copy paste them and then go through the explanation. Be sure to use console.log whenever you want to!&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1 - Installing Redux and Redux Toolkit
&lt;/h3&gt;

&lt;p&gt;Simply copy paste and install the following to your React application.&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="nx"&gt;redux&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;reduxjs&lt;/span&gt;&lt;span class="sr"&gt;/toolki&lt;/span&gt;&lt;span class="err"&gt;t
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2 - Creating a Store
&lt;/h3&gt;

&lt;p&gt;what is store? what is action? what is dispatch?&lt;br&gt;
&lt;strong&gt;Store&lt;/strong&gt; is what it sounds like - a store of reducers and states which can be used throughout the application without the hassle of being passed as props. &lt;/p&gt;

&lt;p&gt;For example - if you are building a social media app, you would like the details of the user to be accessed in any component. You can simply do this by "calling" the details in that particular page or component(like setting, profile page,etc.) &lt;em&gt;from&lt;/em&gt; the store file. &lt;/p&gt;

&lt;p&gt;Now, you would like to manipulate the value of this state according to the user's actions. For example when the user logs out, you want to empty the "user" state/object (let's call this function removeUser). Or populate it with data when user logs in (let's call this function addUser). These are two different functions for two different scenarios for the same object. These types of functions are called &lt;strong&gt;Reducers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Go to your src folder of your React project and create a folder called "redux". In it, create a file called store.jsx (or js if you have created your project using create-react-app) and paste the following code.&lt;/p&gt;

&lt;p&gt;src/redux/store.jsx&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="c1"&gt;//configureStore will help us set up our store&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;configureStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//we are exporting the store by default&lt;/span&gt;
&lt;span class="c1"&gt;//if you want, you can also assign it to a variable and then export it&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="c1"&gt;//all our reducer functions will go here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3 - Making our store available throughout the application
&lt;/h3&gt;

&lt;p&gt;Go to index.jsx, index.tsx or main.tsx and paste the following. Depending on whether you have created your react app using create-react-app or vite or any other building tool, this may vary. &lt;/p&gt;

&lt;p&gt;We import the Provider wrapper here that helps "distribute" the store we created throughout the application. Then, we pass the store object we created (after importing it of course) as props to it so that it knows which file to be made available globally.&lt;br&gt;
src/App.jsx or src/main.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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="nx"&gt;ReactDOM&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&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;./index.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;store&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;./redux/store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// importing the store we created&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StrictMode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.StrictMode&amp;gt;&lt;/span&gt;&lt;span class="err"&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="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4 - Creating a Slice
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Slice&lt;/strong&gt; file contains a state variable and different functions(reducers) for that particular state, that will become a part of the redux store we previously created. Think of them as files that keep states separately from one another.&lt;/p&gt;

&lt;p&gt;For analogy - think of them as rooms of your house. Bedroom - there is bed, your regular furniture and your wardrobe. These are all "states" inside a "slice"(room). You perform various functions(reducers) here - sleep, lay down, cry because your life sucks or simply lay down and watch a movie or two.&lt;/p&gt;

&lt;p&gt;src/redux/userSlice.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createSlice&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//import PayloadAction in case you're using Typescript&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PayloadAction&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;userInfo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;initialState&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="na"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
&lt;span class="c1"&gt;//in case of typescript - &lt;/span&gt;
&lt;span class="c1"&gt;//adding the type of payload action(data) we need to pass to this reducer (function). &lt;/span&gt;
        &lt;span class="na"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;:(&lt;/span&gt;&lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;PayloadAction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;//pushing the login data to the state object in this slice&lt;/span&gt;
    &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;//keep adding the reducers' names to make them available globally&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userSlice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;we import the reducers that we exported in the slice and add it to our store&lt;/p&gt;

&lt;p&gt;src/redux/store.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;configureStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;userReducer&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;./userSlice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// the "user" key will be used to identify the slice we are dealing with&lt;/span&gt;
&lt;span class="c1"&gt;//in case of multiple slices, we can create different keys for different reducers(of those relevant slices)&lt;/span&gt;
        &lt;span class="na"&gt;user&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4 - Accessing and updating the state inside our slice file/files
&lt;/h3&gt;

&lt;p&gt;Go to the file where you want to add the login data to your store. For this example, we are going to send the email id and password to our userSlice state using useDispatch hook. This hook will allow us to "push" or send our data to the slice state using its reducer(in this case - addUser).&lt;/p&gt;

&lt;p&gt;login.tsx&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;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;addUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../../redux/userSlice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Login&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;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;handleLogin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Perform login logic here, e.g. send login request to server using fetch or axios&lt;/span&gt;

    &lt;span class="c1"&gt;// Assuming login is successful, add user to state&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;johndoe@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;passowrd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;passwordlol&lt;/span&gt;&lt;span class="dl"&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;addUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="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="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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Login&lt;/span&gt; &lt;span class="nx"&gt;Form&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="cm"&gt;/* Login form here */&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;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="nx"&gt;handleLogin&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;Login&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when the user clicks on the Login button, the store will execute the reducer addUser and add the information as a &lt;strong&gt;action.payload&lt;/strong&gt; object to the user state we created in userSlice. You can check the code in step 4 for what we did to the data sent to the slice.&lt;/p&gt;

&lt;p&gt;Now, how can we access the data? We simply use the useSelector hook. This hook allows us to access the slices and the states inside them.&lt;/p&gt;

&lt;p&gt;dashboard.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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;useSelector&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ExampleComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This hook allows us to extract data from the Redux store state&lt;/span&gt;
&lt;span class="c1"&gt;//notice how we are using the key for accessing the userSlice state given in the store&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;email&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&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="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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;email&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;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ExampleComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;Aaaand that's it! Now all you have to do is add more reducers and import useDisptach or useSelector hook to use the states in the store throughout the application. Easy right?&lt;/p&gt;

&lt;p&gt;Feel free to copy paste the code to speed up your development. Let me know in case you encounter any error or if you have any feedback for this article.&lt;/p&gt;

&lt;p&gt;See you next time!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>webperf</category>
      <category>ux</category>
    </item>
    <item>
      <title>Blockchain and smart contracts basics in 2 minutes - part 2</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Fri, 28 Oct 2022 14:00:42 +0000</pubDate>
      <link>https://dev.to/cjreads665/blockchain-and-smart-contracts-basics-in-2-minutes-part-2-cko</link>
      <guid>https://dev.to/cjreads665/blockchain-and-smart-contracts-basics-in-2-minutes-part-2-cko</guid>
      <description>&lt;h2&gt;
  
  
  Another part?
&lt;/h2&gt;

&lt;p&gt;yes. There were some terms that I thought were very important which I missed in my previous post. If you haven't checked out my previous post, it's okay. As long as you have at least knowledge of what blockchain is, you won't feel loss. However, if it's your first time, please read the part 1 first. Let's go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Mainnet and TestNet
&lt;/h2&gt;

&lt;p&gt;Mainnet is the blockchain network on which most “real world” applications and transactions take place. It is here that real money(in the form of crypto) is transacted.&lt;br&gt;
Testnet is the blockchain that behaves like mainnet except there is no real money involved, and it is mainly used for deploying test projects and doing test transactions. You can send and receive fake money on it, but you cannot transfer it to mainnet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gas
&lt;/h2&gt;

&lt;p&gt;Every time a transaction is made, a node will have to solve a complex mathematical problem to approve it. The measurement of computational power required to approve the transaction is called &lt;strong&gt;gas&lt;/strong&gt;. This depends on the complexity of the problem, demand and supply capacity of the network at the time of the transaction.&lt;/p&gt;

&lt;p&gt;To make a transaction happen on a blockchain, the nodes or miners will charge a small fee for running the blockchain. The fees depend on the amount of gas required for the transaction. The more gas used, the more fees you’ll have to pay. On top of it, some coins(or crypto) are also “burnt”. This means during the transaction, some ether/coin/crypto is lost forever. This is done to decrease the supply of the crypto and increase its price.&lt;/p&gt;

&lt;h2&gt;
  
  
  How blockchain works
&lt;/h2&gt;

&lt;p&gt;A blockchain is a chain of blocks.&lt;/p&gt;

&lt;p&gt;Each block has the following data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block number &lt;/li&gt;
&lt;li&gt;Nonce - the solution to the problem&lt;/li&gt;
&lt;li&gt;The actual data we want to deploy - usually contains a list of transactions.&lt;/li&gt;
&lt;li&gt;It's hash&lt;/li&gt;
&lt;li&gt;Previous block’s hash - the hash of the block before it
Hash&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lE96nTyz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/il90rz31jsq01i592tbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lE96nTyz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/il90rz31jsq01i592tbh.png" alt="block" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the data, block number and nonce is put through a “hash algorithm”, a string of fixed length is created that helps us identify the data.&lt;/p&gt;

&lt;p&gt;When miners try to verify this block, they will try to figure out the nonce of the block again and again until they get it right for the above hash.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why blockchain is immutable
&lt;/h4&gt;

&lt;p&gt;If any one block in a blockchain changes any value, its hash will also change (since the hashing algorithm will change it based on data). This breaks the chain, since all the blocks after it will have its previous hash.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FKcpHky5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aux35cqff08g24w88bw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FKcpHky5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aux35cqff08g24w88bw1.png" alt="blockchainImage" width="800" height="431"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.google.com/url?sa=i&amp;amp;url=https%3A%2F%2Fmoney.com%2Fwhat-is-blockchain%2F&amp;amp;psig=AOvVaw2NAwPAvebmM-EMdQIUmn5I&amp;amp;ust=1667051542931000&amp;amp;source=images&amp;amp;cd=vfe&amp;amp;ved=0CA4QjhxqFwoTCJDSwoqJg_sCFQAAAAAdAAAAABAE"&gt;Image Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in a distributed blockchain, each node, containing a copy of the blockchain that acts as a ledger of transactions, is connected with one another(peer-to-peer). Now, if any data is altered in any of the copy of the blockchain, that whole record will become invalid and the node will be thrown out of the network and denied mining rewards. This is why blockchains are “immutable”. This is similar to keeping, say, 30 copies of ledgers(or record of transactions) among your friends. If any one friend tries to be smart and alter any data for his greed, he will be caught as his data does not tally with others.&lt;/p&gt;

&lt;p&gt;If you change data in one block, the entire copy of the blockchain gets invalidated. And even if you somehow are able to change the hash of each and every block in the (copy)blockchain, which is an extreme computation heavy task, that node will still be invalid since the hashes will not match with the hashes of block of its peers (nodes). &lt;/p&gt;

&lt;h2&gt;
  
  
  Keys
&lt;/h2&gt;

&lt;p&gt;Keys are a string of characters that is used for verifying a transaction.&lt;br&gt;
There are 2 types of keys:&lt;br&gt;
Private - these are our “passwords” that should be kept secret. This is used for signing or confirming a transaction.&lt;br&gt;
Public key - this key is generated from the private key itself that is used for verifying any transaction. This can be displayed in public. This acts as an "ID" that lets you connect your wallet on different website/dApps.&lt;/p&gt;

</description>
      <category>bitcoin</category>
      <category>blockchain</category>
      <category>webdev</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>Blockchain and smart contracts basics in 2 minutes</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Sun, 23 Oct 2022 16:37:15 +0000</pubDate>
      <link>https://dev.to/cjreads665/blockchain-and-smart-contracts-in-3-minutes-46bh</link>
      <guid>https://dev.to/cjreads665/blockchain-and-smart-contracts-in-3-minutes-46bh</guid>
      <description>&lt;p&gt;&lt;em&gt;This article aims to give you the necessary "bare minimum" knowledge to get started in web3. These are enough if you are just starting out your journey in web3&lt;/em&gt;&lt;br&gt;
Let's get started! Blockchain and smart contract basics in less than 2 minutes. Go!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is blockchain?
&lt;/h2&gt;

&lt;p&gt;It's a "book" or ledger that is kept by everyone who run the blockchain. These people/machines are known as nodes and every node has a copy of the ledger to verify any transfer of assets. That's it. No one person owns the blockchain.&lt;/p&gt;

&lt;p&gt;Blockchain is the tech behind bitcoin that allows people to do peer-to-peer (one person to another) transactions without any company in between.&lt;/p&gt;

&lt;p&gt;Since no other company or person is between people when transacting (like credit card companies or e-wallets), it was seen as a huge improvement that allowed privacy (since no one can see who sent who), great security (powered by miners by solving complex mathematical problems) and no reliance on third party (like your bank or credit card companies).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Smart Contract?
&lt;/h2&gt;

&lt;p&gt;Smart contracts are a set of instructions executed in a decentralized way between parties. It’s an agreement, contract or a set of instructions that is deployed on a decentralized network.&lt;/p&gt;

&lt;p&gt;Bitcoin also has this feature, however it is not optimized for it. Ethereum is a better candidate. &lt;/p&gt;

&lt;p&gt;For smart contract to work and replace the traditional use cases and contracts, we need to push data to it using an external system. However, it  cannot get data from the real world directly(like getting cricket scores or weather reports). This is known as the &lt;strong&gt;“Oracle Problem”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Oracles are devices or systems that help feed data to smart contracts from the real world(“off-chain”). For the blockchain to be decentralized, we cannot work with only one oracle. We need a decentralized oracle network (such as Chainlink) as well. This combination of off-chain data with the on-chain logic gives rise to Hybrid Smart Contracts. They help smart contracts to execute a contract with the help of external data by Oracle.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the value of smart contract?
&lt;/h2&gt;

&lt;p&gt;Smart contracts create “Trust Minimized Agreements”. They create unbreakable promises or agreements. This helps in speed and transparency. For example, when you buy something online, they promise to deliver the product to you. When you deposit money in your bank account, the bank promises to keep the money safe and in turn you promise to pay the fees they charge.&lt;/p&gt;

&lt;p&gt;Sometimes, the people who we put our trust in for work, break them and in most cases, get away with it. This is where smart contracts come into the picture.&lt;/p&gt;

&lt;p&gt;Now, there are already systems that solve or prevent these problems in our society, like courts. However, frauds do happen, trusts are broken, and the terms are not met. And as already mentioned, in some cases, they get away with it or take YEARS to make justice happen. Examples - the recession of 1980s when banks refuse withdrawal of its own customers’ funds, the demonetization of some Indian currency notes that caused a lot of controversies and death of a lot of people due denied access to their own money, insurance companies refusing claims because of “hospital partnership agreements” when you really need it.&lt;/p&gt;

&lt;p&gt;Here, you trust the BRAND and hope that they do as they have promised.&lt;/p&gt;

&lt;p&gt;The traditional alternative is to go to another entity that provides the same service and hope to not get scammed or screwed over.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In contrast, smart contracts are :&lt;/li&gt;
&lt;li&gt;Immutable&lt;/li&gt;
&lt;li&gt;Executes automatically&lt;/li&gt;
&lt;li&gt;transparent (anyone can see the terms of agreements)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes sure that not one central entity can alter the terms in his/her favor. In this case, we trust the MATH and CODE that only has a “single truth” and will execute on fulfillment of the promise ONLY.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of smart contract
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Decentralized - no entity or single person controls the network. The blockchain is run by individuals/machines who are also known as “node operators”. It is the combination of the node operators and the algorithms they run that make the network decentralized.&lt;/li&gt;
&lt;li&gt;Transparency &amp;amp; flexibility - anyone, even you, can see everything on the chain. This allows the public to review the terms and confirm if they are being ripped off or mislead.&lt;/li&gt;
&lt;li&gt;Speed &amp;amp; efficiency - sending or receiving money happens within minutes (and sometimes seconds!) where traditional methods such as wire transfer overseas take several days to complete. Hacking the blockchain is nearly impossible since to tamper a simple contract, the hacker will need to change the ledger (or history of transactions) on thousands of nodes.
Personal opinion - this works great for someone sending money     internationally. For domestic, I think UPI is a better alternative.&lt;/li&gt;
&lt;li&gt;Removes third-party reliance.&lt;/li&gt;
&lt;li&gt;Trust minimized agreements - It's less trust, more truth reliant.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Applications of smart contract so far
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;DeFi - decentralized finance that removes the reliance on banks and financial institutions to keep our money safe and engage in financial activities.&lt;/li&gt;
&lt;li&gt;DAO- Decentralized Autonomous Organization are groups/ organizations that are governed by a set of instructions or smart contracts. They have no central leadership and are governed by the set of instructions and code written in smart contracts.&lt;/li&gt;
&lt;li&gt;NFTs - Non-Fungible Tokens are pieces of arts (not the ugly apes, please) that are like special collectable cards or Pokémon toys you used to buy because they were limited. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out part 2 &lt;a href="https://dev.to/cjreads665/blockchain-and-smart-contracts-in-3-minutes-46bh"&gt;here&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>useNavigate,Navigate and useLocation in React Router v6</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Sun, 07 Aug 2022 15:33:02 +0000</pubDate>
      <link>https://dev.to/cjreads665/usenavigatenavigate-and-uselocation-in-react-router-v6-lip</link>
      <guid>https://dev.to/cjreads665/usenavigatenavigate-and-uselocation-in-react-router-v6-lip</guid>
      <description>&lt;h2&gt;
  
  
  What is useLocation?
&lt;/h2&gt;

&lt;p&gt;useLocation is hook that allows us to get the current location(or URL) of the web app.&lt;/p&gt;

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

const currentLocation = useLocation()
console.log(currentLocation)


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ciqf87i1yvvbse718j8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ciqf87i1yvvbse718j8.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useNavigate?
&lt;/h2&gt;

&lt;p&gt;useNavigate is a hook that allows to create a function that'll help a user navigate to a particular page (based on an action). You can customize it to redirect the user to the login page or user Dashboard.&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 { useNavigate } from "react-router-dom";

const Home = () =&amp;gt; {
  const navigate = useNavigate();

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Visit my profile&amp;lt;/h2&amp;gt;
      &amp;lt;button
        onClick={() =&amp;gt; {
          console.log("redirecting.....");
          navigate("/profile");
        }}
      &amp;gt;
        My Profile
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Home;



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

&lt;/div&gt;

&lt;p&gt;Now what if you want to go back to the Home page without clicking the browser "Back" button? We provide a button.&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 { useNavigate } from "react-router-dom";

const Profile = () =&amp;gt; {
  const navigate = useNavigate();

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;You are awesome!&amp;lt;/h2&amp;gt;
      &amp;lt;button
        onClick={() =&amp;gt; {
          console.log("redirecting.....");
          navigate(-1);
        }}
      &amp;gt;
        Home
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Home;



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

&lt;/div&gt;
&lt;h2&gt;
  
  
  What is Navigate?
&lt;/h2&gt;

&lt;p&gt;Navigate is basically useNavigate() converted into a React component. This makes it easy to implement in our React apps.&lt;/p&gt;

&lt;p&gt;props it takes -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;state - optional -&amp;gt; stores state and can be used to store the location of the current or previous page&lt;/li&gt;
&lt;li&gt;replace - optional -&amp;gt; helps in redirecting to the location specified in the state.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 return (
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;Container maxWidth="lg"&amp;gt;
        &amp;lt;Navbar /&amp;gt;
        &amp;lt;Routes&amp;gt;
          &amp;lt;Route exact path="/"  element ={&amp;lt;Home/&amp;gt;} /&amp;gt;
          &amp;lt;Route path="/auth"  element={&amp;lt;Auth/&amp;gt;} /&amp;gt;
          &amp;lt;Route path="/posts" exact element={&amp;lt;Home/&amp;gt;} /&amp;gt;
{/*
the Navigate component redirects the user to posts section on getting rendered.
This happend IF the user is logged in(user object is present).
*/}
          &amp;lt;Route path="/auth" exact element={() =&amp;gt; (!user ? &amp;lt;Auth /&amp;gt; : &amp;lt;Navigate to="/posts" /&amp;gt;)} /&amp;gt;
        &amp;lt;/Routes&amp;gt;
      &amp;lt;/Container&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  )
};


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

&lt;/div&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Context API in human language</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Fri, 15 Jul 2022 08:08:31 +0000</pubDate>
      <link>https://dev.to/cjreads665/context-api-in-human-language-2de6</link>
      <guid>https://dev.to/cjreads665/context-api-in-human-language-2de6</guid>
      <description>&lt;h2&gt;
  
  
  What is Context API?
&lt;/h2&gt;

&lt;p&gt;Think of it as a container in which you keep all your states, variables and whatnot. Then, later you can simply import whatever variable or state you need from it thus, saving you writing long  lines of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Want a dumb down version?
&lt;/h2&gt;

&lt;p&gt;Imagine yourself back in high school.&lt;br&gt;
Don't panic! you're just imagining it(it was hard, I know).&lt;br&gt;
A teacher is marking the answer sheets for an examination, noting the marks on a paper sheet. &lt;/p&gt;

&lt;p&gt;When the teacher is ready to declare the results, he goes to the house of each student and tell them the marks/grades scored. You lived the farthest, so he tells your friend to pass down your grade to you. This friend will have to pass down your grade to other friends who live near your house and it keeps going on until the grade reaches you.&lt;/p&gt;

&lt;p&gt;See how inefficient it is? Instead of just posting the results online or on the notice board, we just increased the time taken for the result to get and the complexities for the teacher and your friends. &lt;br&gt;
Now imagine 1000 students who study in this school. Doesn't look good for our dear teacher now, does it? &lt;/p&gt;

&lt;p&gt;You can think of the first method as the method by which we pass data as props to different components and second method as using the Context API of React. It is acceptable if you're working on small projects(a small group of students) but it becomes more complex as your project gets bigger and bigger(around 1000 or more).&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Context API
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt; First, we create a context file inside a new folder called context . We're gonna name it &lt;code&gt;Store.jsx&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j1Lm-eDk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxrjh4quifuhtojh4o2s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j1Lm-eDk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxrjh4quifuhtojh4o2s.png" alt="creating-store-file" width="236" height="107"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tyxt6iqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krgbnszbselmwcaqyng8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tyxt6iqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krgbnszbselmwcaqyng8.png" alt="store-component" width="703" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will be the place where all your variables and state reside. For testing purpose, we'll want to make user and userLoading states available to all our components in our app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we create a StoreProvider component. Think of these components as transportation of sort which helps transport the store we created in our previous steps to the whole app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s3IWMIeU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr8jp09qfmx3xfcko4ex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s3IWMIeU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr8jp09qfmx3xfcko4ex.png" alt="storeprovider-file" width="248" height="65"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the createContext hook to create a context(what we discussed above). Whatever we put inside the Provider component .i.e the children of Provider, should have access to the Store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NpF_-EXs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iawaiw0ihvvqrvlk7bdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NpF_-EXs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iawaiw0ihvvqrvlk7bdl.png" alt="store-provider-comp" width="697" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the useContext hook, we tell the context API which store should be available globally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lastly, we create a useStore component which will allow us to "pull" the required states/variables from our Store&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LXxTtDIP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7ev0djynm7gkua1smr6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LXxTtDIP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7ev0djynm7gkua1smr6.png" alt="create-use-store-file" width="231" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bUpfelGQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4vpug208gtel7e92hs4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bUpfelGQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4vpug208gtel7e92hs4.png" alt="filling-usestore" width="446" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we use the useContext hook and specify which context component we want to use (which we import from context provider component)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;&lt;br&gt;
Now you can use the states, objects, blah blah anywhere in your app! Just import useStore and use object destructuring to "pull" the required value from the store we created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---co27FGX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3do7oxts726pynfzxt4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---co27FGX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3do7oxts726pynfzxt4.png" alt="pulling-usestore" width="403" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Context API is great for your application if you want to reduce code in each component and keep track of reusable variables and states in one place. There is another tool for managing and storing states in an app but that's something for another day.&lt;/p&gt;

&lt;p&gt;See ya!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What the heck is State in React?</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Wed, 13 Jul 2022 13:10:56 +0000</pubDate>
      <link>https://dev.to/cjreads665/what-the-heck-is-state-in-react-44c5</link>
      <guid>https://dev.to/cjreads665/what-the-heck-is-state-in-react-44c5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;After building numerous projects using only HTML, CSS and Javascript, you find yourself ready to tackle the most popular front-end framework - React.js&lt;/p&gt;

&lt;p&gt;There are literally more than 100s of job postings daily for good React developers. So you have a pretty good reason to move from static pages.&lt;/p&gt;

&lt;p&gt;And you are met with your first challenge - State.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;So what is state exactly?&lt;/p&gt;

&lt;p&gt;An oversimplification will be - a variable that re-renders(reloads without refreshing) the parent component. That's all it is.&lt;/p&gt;

&lt;p&gt;In class component : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bylOm2eS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sflz5neo64nujkqwub2d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bylOm2eS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sflz5neo64nujkqwub2d.png" alt="states-in-class-component" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In functional component :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lst55CmY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5c6mb6ulssn1vusqb7i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lst55CmY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5c6mb6ulssn1vusqb7i.png" alt="states-in-functional-components" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And as any other variable, you are free to add whatever you want in it. Some examples in functional component. (they are applicable in class component as well) : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M0fyIBpH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlvcw07sjtia1tab3gvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M0fyIBpH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlvcw07sjtia1tab3gvg.png" alt="state-example" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Changing values of state
&lt;/h3&gt;

&lt;p&gt;Unlike variables where we can directly change the value by reassigning, we change values in set using a function called setState and NOT by re-assigning like a variable.&lt;/p&gt;

&lt;p&gt;In class component:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MDEMDdth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnf6uncxlewmv8ny8k3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MDEMDdth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnf6uncxlewmv8ny8k3s.png" alt="using-setState" width="800" height="824"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In functional component :&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O940k77s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuvd3z94ygn0gte8bcay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O940k77s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuvd3z94ygn0gte8bcay.png" alt="using-setState" width="800" height="841"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Changing specific values in state
&lt;/h3&gt;

&lt;p&gt;Whenever you initialize a state with an object, most of the time you might want to change a specific value only and not the whole object. How do we achieve it?&lt;/p&gt;

&lt;p&gt;We do it by copying the previous data from the state and modify the value using the property name.&lt;/p&gt;

&lt;p&gt;Here, I'm using functional component (same can be done in a class component) :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--615x8zDB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j62imsd2o820rybufow7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--615x8zDB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j62imsd2o820rybufow7.png" alt="changing-value-of-state-object" width="800" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;So that is what state is in React and how you can go about initializing, changing and using it. &lt;/p&gt;

&lt;p&gt;States are very helpful and "control" the component.&lt;/p&gt;

&lt;p&gt;I often share react related content on my twitter so be sure to check it out!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is a class in JavaScript?</title>
      <dc:creator>Shahid Alam</dc:creator>
      <pubDate>Tue, 13 Jul 2021 13:09:29 +0000</pubDate>
      <link>https://dev.to/cjreads665/what-is-a-class-in-javascript-2190</link>
      <guid>https://dev.to/cjreads665/what-is-a-class-in-javascript-2190</guid>
      <description>&lt;h2&gt;
  
  
  Stuck in tutorial hell
&lt;/h2&gt;

&lt;p&gt;You may have come across this word "classes" which is often referred as a blueprint of different objects. we can create "instances" of it using the new keyword. but sometimes you don't really get what exactly is class even after reading several articles and getting trapped in tutorial hell.&lt;/p&gt;

&lt;p&gt;Fret not. i'll try to explain it as easy as possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe9klg38uyy0pt7uh7nb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe9klg38uyy0pt7uh7nb0.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of classes as a stamp, like above.&lt;/p&gt;

&lt;p&gt;Imagine you have to sign some documents for verification.&lt;br&gt;
Now imagine there are 400 pages of documents on which your signature is needed(400 similar objects are needed). It'll take a lot of time if you do them one by one(keep declaring new objects with similar properties).&lt;/p&gt;

&lt;p&gt;So, what do you do to save time and effort? You use a stamp(class) which will already have your name(object properties/functions/values) on it. Now you can simply use the stamp to validate different documents(create objects with same properties) much faster.&lt;/p&gt;

&lt;p&gt;in code we do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Sign(){ // creating a class / stamp
      constructor(signature){ // engraving your signature on the stamp
            this.sign = signature; // assigning the signature to a property this.sign which will hold the value

                            }
         get thesign(){
            return this.sign
         }

}


const mySign = new Sign("cj")
console.log(mySign.sign) // cj
console.log(mySign.thesign) // cj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;I've tried my best to explain classes in JavaScript. If you find anything that's incorrect, please let me know. Thank you for reading!&lt;/p&gt;

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