<?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: Pratham</title>
    <description>The latest articles on Forem by Pratham (@pratham10).</description>
    <link>https://forem.com/pratham10</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%2F416716%2F22e1b594-67aa-41a7-a5d1-1c663570caf6.jpg</url>
      <title>Forem: Pratham</title>
      <link>https://forem.com/pratham10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pratham10"/>
    <language>en</language>
    <item>
      <title>Does GraphQL replace REST API ?</title>
      <dc:creator>Pratham</dc:creator>
      <pubDate>Sat, 05 Mar 2022 05:39:02 +0000</pubDate>
      <link>https://forem.com/pratham10/does-graphql-replace-rest-api--50fm</link>
      <guid>https://forem.com/pratham10/does-graphql-replace-rest-api--50fm</guid>
      <description>&lt;p&gt;GraphQL is gaining popularity because of its ability to develop APIs more efficiently, powerful, and flexible as compared to REST.&lt;br&gt;
We all know REST is the traditional method to work with APIs but after the launch of GraphQL in 2015 it has really taken off with developers.&lt;/p&gt;

&lt;p&gt;GraphQL is the query language for your API which allows you to get exactly the data you need (no more, no less) whenever you need it. It was developed by Facebook &amp;amp; now it's maintained by a large open-source community.&lt;/p&gt;
&lt;h2&gt;
  
  
  GraphQL Advantages
&lt;/h2&gt;

&lt;p&gt;Let's take a look at an example: Suppose we want to make a blog app &amp;amp; we want to display the name, title, followers of a specific user. With REST API, we might have to send the request to multiple endpoints in order to fulfill our data needs.&lt;/p&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/users/{id}/name
/users/{id}/posts/title
/users/{id}/followers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With REST API you will end up sending 3 requests to the server. In the case of GraphQL, we just have to send a single query to the GraphQL server &amp;amp; then the server will respond with JSON data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query {
 User {
   name
      posts {
        title
        comments {
          comment
        }
     }
     followers {
       name
     }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and we will get a response like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "data": 
  {
    "user": {
      "name": "John Doe",
      "posts": [
      {
        "title": "How to fetch data from an API",
        "comments": [
        {
          "comment": "Great post."
        }
       ]
     },
     {
       "title": "How to build REST API with Node.js",
       "comments": [
        {
          "comment": "So neat &amp;amp; precisely written."
        }
       ]
     }
    ]
    "followers": [
    {
      "name": "Ben Smith"
    }
   ]
  }
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  No over and under-fetching
&lt;/h2&gt;

&lt;p&gt;Over-fetching means the client downloads more data than required &amp;amp; Under-fetching means that the specific endpoint doesn't provide enough data which the client needs.&lt;/p&gt;

&lt;p&gt;In REST API each endpoint has a fixed data structure which means most of the time we fetch more data than we actually need or we need to call multiple endpoints to fulfill our data needs.&lt;/p&gt;

&lt;p&gt;In the case of GraphQL, it allows clients to request data from several resources with a single request which solves the problem of overfetching &amp;amp; underfetching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speeds up the development process
&lt;/h2&gt;

&lt;p&gt;Suppose you're working on a project whose product requirements change suddenly &amp;amp; the worst case happens: the REST API requires a new endpoint. Now In this case front-end developer team will get blocked on their work &amp;amp; will be entirely dependent on the backend developer team.&lt;/p&gt;

&lt;p&gt;GraphQL makes front-end developers' lives easier because the data consumed on the client is no longer coupled to an endpoint's resource which speeds up the development process for both frontend &amp;amp; backend developer teams.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing GraphQL with REST
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Popularity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;GraphQL is gaining popularity exponentially over the last few years.&lt;br&gt;
According to the State of API Integration Report 2021,&lt;br&gt;
75% of respondents expect GraphQL to be the dominant future query language for APIs. For reference, in 2020 only 40% said GraphQL would be the predominant approach in the future&lt;/p&gt;

&lt;p&gt;But REST remains the most popular among the respondents of API report 2021 as they believe GraphQL is lagging behind industry adoption.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usability&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's simple to get the precise data you need from your database using GraphQL's queries. This makes data consumption much easier as you get predictable results.&lt;/p&gt;

&lt;p&gt;Compared to REST It returns all the data available from the specific endpoint. This can lead to data dumps where the client has to download unnecessary data which is not used.&lt;br&gt;
Arguably GraphQL is easier to use as compared to REST.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Customized queries in GraphQL improve efficiency in a variety of ways, from lowering the number of API requests to ensuring the proper amount of data is returned.&lt;br&gt;
But wait… REST API might be better when it comes to performance&lt;br&gt;
To return cached results quicker, REST APIs use the built-in HTTP caching mechanism which can perform better in circumstances where caching is required to fast-track API calls. Caching is also available in GraphQL, but not to the same extent as it is in REST.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pros and Cons
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;GraphQL pros&lt;/strong&gt;&lt;br&gt;
Provides consistent, uniform data.&lt;br&gt;
Eliminates over-fetching &amp;amp; under-fetching.&lt;br&gt;
Speeds up the development process&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GraphQL cons&lt;/strong&gt;&lt;br&gt;
Lacks built-in caching.&lt;br&gt;
Complicated error handling.&lt;br&gt;
Lacks industry adoption &amp;amp; support.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REST API pros&lt;/strong&gt;&lt;br&gt;
Supports different data formats (Html, JSON, etc.)&lt;br&gt;
Popular &amp;amp; has great adoption &amp;amp; support from the community.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REST API cons&lt;/strong&gt;&lt;br&gt;
Multiple requests to the server to fetch all the data needed.&lt;br&gt;
No specific method for structuring API.&lt;/p&gt;

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

&lt;p&gt;I am not an API expert but I find it really interesting &amp;amp; fascinating to work with GraphQL. If you're looking for an answer to which one is better you probably are at the wrong place. It really depends on every application's use-cases to decide which API form is better.&lt;/p&gt;

&lt;p&gt;I suggest just trying experimenting with them both &amp;amp; see if that fits your development workflow.&lt;/p&gt;

&lt;p&gt;Comment down below If you got an opinion that you want to share.&lt;br&gt;
I hope you liked the article. &lt;br&gt;
Keep learning!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>graphql</category>
      <category>node</category>
    </item>
    <item>
      <title>How to use Styled Components with React.js</title>
      <dc:creator>Pratham</dc:creator>
      <pubDate>Sun, 06 Feb 2022 09:12:21 +0000</pubDate>
      <link>https://forem.com/pratham10/how-to-use-styled-components-with-reactjs-59bh</link>
      <guid>https://forem.com/pratham10/how-to-use-styled-components-with-reactjs-59bh</guid>
      <description>&lt;p&gt;Styled Components is react library that allows us to style react components individually.&lt;/p&gt;

&lt;p&gt;In this tutorial, you will learn about styled-components &amp;amp; how to use styled-components in your React application.&lt;/p&gt;

&lt;p&gt;We can use styled-components in React &amp;amp; react-native applications but this guide will focus only on using styled-components with React.&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; What are Styled-components ?&lt;/li&gt;
&lt;li&gt; But why Styled-components ?&lt;/li&gt;
&lt;li&gt; Installing styled-components&lt;/li&gt;
&lt;li&gt; Getting started&lt;/li&gt;
&lt;li&gt; Props in styled-components&lt;/li&gt;
&lt;li&gt; Make it Responsive&lt;/li&gt;
&lt;li&gt; Overriding Styles&lt;/li&gt;
&lt;li&gt; Global styling &lt;/li&gt;
&lt;li&gt; Conclusion&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What are Styled-components ?
&lt;/h2&gt;

&lt;p&gt;Styled Components allows us to write CSS in JavaScript&lt;/p&gt;

&lt;p&gt;It is a component-based framework specially developed for react &amp;amp; react-native applications which uses component-based approach to manage our styles.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It removes the mapping between components and styles. This means that when you're defining your styles, you're actually creating a normal React component, that has your styles attached to it.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  But why Styled-components ?
&lt;/h2&gt;

&lt;p&gt;Reusable styles: You're writing CSS in a component-based approach which means once you define your styles you can use that anywhere inside your project.&lt;/p&gt;

&lt;p&gt;No class name bugs: In styled-components you don't have to worry about naming conventions, the library provides unique class name that eliminates duplication, overlap or misspellings errors.&lt;/p&gt;

&lt;p&gt;Easy to manage CSS: You never have to find different files affecting the styling which makes it easier to manage CSS.&lt;/p&gt;

&lt;p&gt;Eliminates unused code: If any specific styled-component is unused it automatically deletes all its styles.&lt;/p&gt;




&lt;h2&gt;
  
  
  Installing styled-components
&lt;/h2&gt;

&lt;p&gt;Start by creating react application&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app styled-components-tutorial&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;To use styled-components, you first have to install it using npm or yarn.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    # with npm
    npm install --save styled-components

    # with yarn
    yarn add styled-components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or if you're not using any package manager you can use styled-components with CDN, just add this to bottom of your html file.&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;script src="https://unpkg.com/styled-components/dist/styled-components.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;styled-components uses tagged template literals to style your components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import React from "react";
    import styled from "styled-components";

    // Creating a Heading styled-component that'll render an &amp;lt;h1&amp;gt; tag with some additional styles
    const Heading = styled.h1`
      font-size: 1.5em;
      color: #2F9ED8;
    `;

    // Creating a Container styled-component that'll render an &amp;lt;section&amp;gt; tag with some additional styles
    const Container = styled.section`
      padding: 4em;
      background: #B6585F;
    `;

    function App() {
      return (
        &amp;lt;div&amp;gt;
          &amp;lt;Container&amp;gt;
            &amp;lt;Heading&amp;gt;Styled Components&amp;lt;/Heading&amp;gt;
          &amp;lt;/Container&amp;gt;
        &amp;lt;/div&amp;gt;
      );
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can visit &lt;a href="https://codesandbox.io/s/determined-wildflower-0sb1l?file=/src/App.js"&gt;codesandbox&lt;/a&gt; &amp;amp; see the output.&lt;/p&gt;

&lt;p&gt;In the above example we created styled-components inside existing component file. we can also create a separate file for styled-components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Heading.js
    import styled from 'styled-components';

    const Heading = styled.h1`
      font-size: 1.5em;
      color: #2F9ED8;
    `;

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

&lt;/div&gt;



&lt;p&gt;Now you can use Heading Component in any component of the project.&lt;br&gt;
&lt;/p&gt;

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

    // import the styled component:
    import Heading from './Heading';

    const App = () =&amp;gt; {
      return (
         &amp;lt;div&amp;gt;
            &amp;lt;Heading&amp;gt;Styled Components&amp;lt;/Heading&amp;gt;
        &amp;lt;/div&amp;gt;
      )
    }
    export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Props in styled components
&lt;/h2&gt;

&lt;p&gt;Styled components are functional components which means we can style components dynamically.&lt;br&gt;
To make our components styles dynamic we use props.&lt;/p&gt;

&lt;p&gt;Let's take a look at example &lt;/p&gt;

&lt;p&gt;Suppose you want different buttons in your application (for ex. Primary, Danger, etc)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const Button = styled.button`
      background: ${props =&amp;gt; props.primary ? "blue" : "white"};
      color: ${props =&amp;gt; props.primary ? "white" : "blue"};

      font-size: 1em;
      margin: 1em;
      padding: 0.25em 1em;
      border: 2px solid blue;
      border-radius: 3px;
    `;

    return(
      &amp;lt;div&amp;gt;
        &amp;lt;Button&amp;gt;Normal&amp;lt;/Button&amp;gt;
        &amp;lt;Button primary&amp;gt;Primary&amp;lt;/Button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are rendering two button components, one normal button &amp;amp; one with "primary" as prop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Live code example &lt;a href="https://codesandbox.io/s/determined-wildflower-0sb1l?file=/src/App.js"&gt;here&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Make it Responsive
&lt;/h2&gt;

&lt;p&gt;To make your components responsive we can use media-queries just as we use them in plain CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const Button = styled.button`
      background: ${props =&amp;gt; props.primary ? "blue" : "white"};
      color: ${props =&amp;gt; props.primary ? "white" : "blue"};

      font-size: 1em;
      margin: 1em;
      padding: 0.25em 1em;
      border: 2px solid blue;
      border-radius: 3px;

      @media (min-width: 768px) { 
        padding: 1rem 2rem;
        width: 11rem;
      }

      @media (min-width: 1024px) { 
        padding: 1.5rem 2.5rem;
        width: 13rem;
      }
    `;

    return(
      &amp;lt;div&amp;gt;
        &amp;lt;Button&amp;gt;Normal&amp;lt;/Button&amp;gt;
        &amp;lt;Button primary&amp;gt;Primary&amp;lt;/Button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Overriding styles
&lt;/h2&gt;

&lt;p&gt;To change a component slightly we can extend the style for a single use case.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const Button = styled.button`
      color: blue;
      font-size: 1em;
      margin: 1em;
      padding: 0.25em 1em;
      border: 2px solid palevioletred;
      border-radius: 3px;
    `;

    const TomatoButton = styled(Button)`
      color: tomato;
      border-color: tomato;
    `;

    return(
      &amp;lt;div&amp;gt;
        &amp;lt;Button&amp;gt;Normal Button&amp;lt;/Button&amp;gt;
        &amp;lt;TomatoButton&amp;gt;Tomato Button&amp;lt;/TomatoButton&amp;gt;
      &amp;lt;/div&amp;gt;
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Global Styling
&lt;/h2&gt;

&lt;p&gt;Thankfully, we have a bult-in function &lt;code&gt;createGlobalStyle&lt;/code&gt; in styled-components to apply global styles to our component.&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;createGlobalStyle&lt;/code&gt; to set same font-family, override default-browser styling, etc. (you got the point.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // App.js
    import React from 'react';
    import styled, { createGlobalStyle } from 'styled-components';
    import { Container, Nav, Content } from '../components';

    const GlobalStyle = createGlobalStyle`
      body {
        margin: 0;
        padding: 0;
        background: teal;
        font-family: Open-Sans, Helvetica, Sans-Serif;
      }
    `;

    const App = () =&amp;gt; {
      return (
        &amp;lt;&amp;gt;
          &amp;lt;GlobalStyle /&amp;gt;
          &amp;lt;Container&amp;gt;
            &amp;lt;Nav /&amp;gt;
            &amp;lt;Content /&amp;gt;
          &amp;lt;/Container&amp;gt;
        &amp;lt;/&amp;gt;
      )
    }
    export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Styles created with &lt;code&gt;createGlobalStyle&lt;/code&gt; do not accept any children&lt;/p&gt;

&lt;p&gt;and that's it!&lt;/p&gt;

&lt;p&gt;give yourself a pat on the back if you've made it till the end.&lt;/p&gt;




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

&lt;p&gt;We covered the fundamentals &amp;amp; some basic concepts of styled-components &amp;amp; they are enough to get you rolling.&lt;/p&gt;

&lt;p&gt;Now don't stop here use styled-components in your next project and see how it goes.&lt;/p&gt;

&lt;p&gt;Don't forget to check out styled-components documentation.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>css</category>
    </item>
    <item>
      <title>Everything about React JS.</title>
      <dc:creator>Pratham</dc:creator>
      <pubDate>Sun, 30 Jan 2022 18:39:58 +0000</pubDate>
      <link>https://forem.com/pratham10/everything-about-react-js-a-comprehensive-guide-about-react-including-concepts-and-examples-1534</link>
      <guid>https://forem.com/pratham10/everything-about-react-js-a-comprehensive-guide-about-react-including-concepts-and-examples-1534</guid>
      <description>&lt;p&gt;React is free, open-source &amp;amp; component-based front end library which is responsible for the frontend of the application. It is developed and maintained by Facebook.&lt;/p&gt;

&lt;p&gt;React was the most popular JavaScript library in 2021.&lt;/p&gt;

&lt;p&gt;React is flexible, easy to learn &amp;amp; has a great community support.&lt;/p&gt;

&lt;p&gt;If you want to learn the framework that will give you the best odds of landing a job in 2022, your best bet is still React.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://nodejs.org" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; installed on your computer&lt;/li&gt;
&lt;li&gt;Basics of HTML, CSS &amp;amp; JavaScript&lt;/li&gt;
&lt;li&gt;Basic understanding of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;DOM&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In this blog
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Essential React Concepts (creating react app, Components, JSX, Props, State, Hooks)&lt;/li&gt;
&lt;li&gt;Counter Application&lt;/li&gt;
&lt;li&gt;How to fetch data from an API in React&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup &amp;amp; Installation
&lt;/h2&gt;

&lt;p&gt;Open your favorite text editor &amp;amp; let's get started.&lt;br&gt;
(I will be using Visual Studio Code)&lt;/p&gt;

&lt;p&gt;Open your command prompt (windows) or terminal (Mac).&lt;/p&gt;

&lt;p&gt;Make sure you have installed Node JS (npx comes with Node JS)&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%2Fo0jk9v5lgerl0tx740us.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%2Fo0jk9v5lgerl0tx740us.PNG" alt="Node JS installed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-basic-concepts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new react app with name &lt;em&gt;react-basic-concepts&lt;/em&gt;&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%2Fj4p9zv0zoaa1zplz34jb.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%2Fj4p9zv0zoaa1zplz34jb.PNG" alt="create-react-app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see React app is installed in my ADMIN folder.&lt;/p&gt;

&lt;p&gt;Now, let's run our application. In your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd react-basic-conecpts
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run our app on localhost:3000&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%2Fehv1a591sddeeswjt9ms.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%2Fehv1a591sddeeswjt9ms.PNG" alt="localhost:3000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now open &lt;em&gt;react-basic-concepts&lt;/em&gt; folder in your text editor&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%2F0acvpzkg388a542feqk3.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%2F0acvpzkg388a542feqk3.PNG" alt="app.js file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand the project structure, you'll see a &lt;em&gt;&lt;strong&gt;/public&lt;/strong&gt;&lt;/em&gt; and &lt;strong&gt;&lt;em&gt;/src&lt;/em&gt;&lt;/strong&gt; directory with the regular &lt;strong&gt;&lt;em&gt;node_modules&lt;/em&gt;&lt;/strong&gt; (conatins all dependencies), &lt;strong&gt;&lt;em&gt;.gitignore&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;README.md&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;package.json&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;&lt;em&gt;/public&lt;/em&gt;&lt;/strong&gt;, our important file is &lt;strong&gt;&lt;em&gt;index.html&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;&lt;em&gt;/src&lt;/em&gt;&lt;/strong&gt; directory will contain all our React code.&lt;/p&gt;

&lt;p&gt;Let's do some cleanup &amp;amp; remove unnecessary files.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;&lt;strong&gt;src&lt;/strong&gt;&lt;/em&gt; directory keep these files &amp;amp; delete rest of them.&lt;/p&gt;

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

&lt;p&gt;Your &lt;strong&gt;&lt;em&gt;index.js&lt;/em&gt;&lt;/strong&gt; file should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5w65aci6x7mze4b6uzsm.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%2F5w65aci6x7mze4b6uzsm.PNG" alt="index.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Delete everything from &lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Open &lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt; file &amp;amp; let's create our first component.&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";  // importing React

const App = () =&amp;gt; {         // creating App component
  return (
    &amp;lt;div&amp;gt;                   // JSX (we'll look into this later)
      &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Save your file &amp;amp; go to your browser, you'll see &lt;strong&gt;&lt;em&gt;Hello, world!&lt;/em&gt;&lt;/strong&gt;&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%2Fr72yp2nvd4k9wgm971nk.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%2Fr72yp2nvd4k9wgm971nk.PNG" alt="Hello, world!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations on creating your first React Component!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;React components let you break up the UI into separate pieces that can then be reused in your entire project.&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%2F20xe5obd6cds5b8p3ek0.jpeg" 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%2F20xe5obd6cds5b8p3ek0.jpeg" alt="Components"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Components are independent &amp;amp; reusable pieces of code.&lt;/p&gt;

&lt;p&gt;There are two ways of creating a component in React&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function Components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A React functional components is nothing but a regular JavaScript function that returns JSX.&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 App = () =&amp;gt; {   component
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Class Components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A class component is JavaScript class with render method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default class HelloMessage extends React.Component {
  render() {
    return (
      &amp;lt;div&amp;gt;
        Hello, world!
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Functional components are short, simple, easy to read &amp;amp; maintain.&lt;/p&gt;

&lt;p&gt;However, the rendering time and performance in either components do not make a lot of differences.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Always start component names with a capital letter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  JSX: JavaScript + XML
&lt;/h3&gt;

&lt;p&gt;Instead of putting JavaScript into HTML,&lt;br&gt;
JSX allows us to put HTML into JavaScript.&lt;/p&gt;

&lt;p&gt;Take a look at the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jsx = &amp;lt;h1&amp;gt;This is JSX&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSX stands for JavaScript XML.&lt;br&gt;
It is simply a syntax extension of React.&lt;br&gt;
It allows us to directly write HTML in React.&lt;/p&gt;

&lt;p&gt;To add JavaScript inside JSX, we need to write it in curly brackets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
 const name = "Pratham";
 return (
  &amp;lt;div className="App"&amp;gt;
   &amp;lt;p&amp;gt;Hello, {name}&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
 );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.&lt;br&gt;
For example, class becomes className in JSX, and tabindex becomes tabIndex.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Props
&lt;/h3&gt;

&lt;p&gt;Props is short for properties and they are used to pass data between React components.&lt;/p&gt;

&lt;p&gt;To use props, you have to pass props as argument in our JS function.&lt;br&gt;
Take a look at example below:&lt;/p&gt;

&lt;p&gt;/src/App.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";
import Names from "./Components/Names";

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Names name="Pratham" /&amp;gt;
      &amp;lt;Names name="Pratik" /&amp;gt;
      &amp;lt;Names name="Saif" /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Create new folder in /src &amp;amp; name it components, then create Name.js file in /src/components&lt;/p&gt;

&lt;p&gt;/src/Components/Names.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 Names = (props) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Hello, {props.name}&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Open localhost:3000 you should see:&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%2Fj9wz03fz3f4h7el9u32e.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%2Fj9wz03fz3f4h7el9u32e.PNG" alt="Hello, {name}"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Hooks
&lt;/h3&gt;

&lt;p&gt;Hooks allow us to add state to add state to functional components and to share logic across components.&lt;/p&gt;

&lt;p&gt;(Note: Hooks can only be used inside function components.)&lt;/p&gt;

&lt;p&gt;React have few built-in hooks like useEffect, useState, etc. You can also create your own custom hooks in React.&lt;/p&gt;

&lt;p&gt;Let's take a look at &lt;em&gt;useState&lt;/em&gt; hook by creating a counter application&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Counter application
&lt;/h3&gt;

&lt;p&gt;Create new component in /src/Components&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, { useState } from 'react';

function Counter() {
    // Declare a new state variable, which we'll call "count"
    const [count, setCount] = useState(0);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;You clicked {count} times&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;
        Click me
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;count&lt;/em&gt; stores the initial value &amp;amp; &lt;em&gt;setCount&lt;/em&gt; is used to update that value.&lt;/p&gt;

&lt;p&gt;Here, the initial value of the &lt;em&gt;count&lt;/em&gt; is 0, then &lt;em&gt;setCount&lt;/em&gt; function is used to update the value of &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Render Counter in App.js &amp;amp; open localhost:3000 in your browser&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import Counter from "./Components/Counter";

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Counter /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;You can learn more about React hooks &lt;a href="https://www.freecodecamp.org/news/introduction-to-react-hooks/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with API in React
&lt;/h2&gt;

&lt;p&gt;To fetch data from an API in react we will be using &lt;em&gt;useEffect&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;To understand the useEffect hook you first need to understand lifecycle of the component. The lifecycle of the component contains three main parts i.e. mounting, updating and unmounting.&lt;/p&gt;

&lt;p&gt;mounting: when the page loads&lt;br&gt;
updating: when the state updates&lt;br&gt;
unmounting: when user leaves the page&lt;/p&gt;

&lt;p&gt;In this case, we will fetch our data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
 // data fetching here
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will fetch data when component mouts, that means, on the first render when the page loads.&lt;/p&gt;

&lt;p&gt;Let's fetch data from books API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// + add the useEffect import
import { useState, useEffect } from "react";

function App() {
  const [books, setBooks] = useState(null);

  // + adding the use
  useEffect(() =&amp;gt; {
    getData();

    // we will use async/await to fetch this data
    async function getData() {
      const response = await fetch(
        "https://www.anapioficeandfire.com/api/books"
      );
      const data = await response.json();

      // store the data into our books variable
      setBooks(data);
    }
  }, []);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Game of Thrones Books&amp;lt;/h1&amp;gt;

      {/* display books from the API */}
      {books &amp;amp;&amp;amp; (
        &amp;lt;div className="books"&amp;gt;
          {/* loop over the books */}
          {books.map((book, index) =&amp;gt; (
            &amp;lt;div key={index}&amp;gt;
              &amp;lt;h2&amp;gt;{book.name}&amp;lt;/h2&amp;gt;
            &amp;lt;/div&amp;gt;
          ))}
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Now open localhost:3000 &amp;amp; see the magic&lt;/p&gt;

&lt;h2&gt;
  
  
  Some resources to learn React
&lt;/h2&gt;

&lt;p&gt;&lt;a href="//https:reactjs.org"&gt;Official documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtube.com/playlist?list=PLN3n1USn4xlntqksY83W3997mmQPrUmqM" rel="noopener noreferrer"&gt;Practical React series by Ben awad&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Asabeneh/30-Days-Of-React" rel="noopener noreferrer"&gt;30 days of React&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That was hell lot of a knowledge.&lt;br&gt;
I'm proud of you if you've managed to make it till here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IF YOU ENJOYED THIS, THEN YOU CAN BUY ME MY FIRST COFFEE EVER, THANK YOU&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/pratham10" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Node Js for beginners + First Node Js program</title>
      <dc:creator>Pratham</dc:creator>
      <pubDate>Mon, 22 Nov 2021 17:12:44 +0000</pubDate>
      <link>https://forem.com/pratham10/introduction-to-nodejs-172</link>
      <guid>https://forem.com/pratham10/introduction-to-nodejs-172</guid>
      <description>&lt;p&gt;So recently I started learning Node.js and I was looking for some blogs that can help me get started with Node.js and I didn't find any good blogs that give a brief introduction to Node.js So I thought why not write one.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;In this article, we will look at:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introducing Node.js&lt;/li&gt;
&lt;li&gt;The Node architecture&lt;/li&gt;
&lt;li&gt;How Node.js works&lt;/li&gt;
&lt;li&gt;Installing Node.js&lt;/li&gt;
&lt;li&gt;Your first Node.js program&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction to Node.js
&lt;/h2&gt;

&lt;p&gt;Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on Chrome's V8 engine and executes JavaScript code outside a web browser.&lt;br&gt;
Node. js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.&lt;/p&gt;

&lt;p&gt;(Chrome's V8 Engine is a fast Javascript to the machine-code compiler to load, optimize and run Javascript code which is written in C++)&lt;/p&gt;

&lt;p&gt;Node is single-threaded, based on event-driven architecture, and non-blocking based on the I/O model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why we use Node.js ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We use Node to build backend services such as Application Programming Interface(API) and the backend of our web apps.&lt;/p&gt;

&lt;p&gt;Node.js is ideal for building data-intensive, highly scalable, and real-time applications such as Chat apps, E-commerce apps, Streaming apps, etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Node Architecture
&lt;/h2&gt;

&lt;p&gt;Node.js uses the “Single Threaded Event Loop” architecture to handle multiple concurrent clients. Node.js Processing Model is based on the JavaScript event-based model along with the JavaScript callback mechanism.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625593753798%2FN9QyHaClA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625593753798%2FN9QyHaClA.png" alt="figure-1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Node Runtime&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node API: JavaScript utilities like file and network I/O, and a whole host of others, like cryptography and compression&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Node core: a set of JavaScript modules that implement the Node API. (Apparently, some of the modules depend on libuv and other C++ code but that’s an implementation detail).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript engine: Chrome’s V8 Engine: A fast JavaScript-to-machine code compiler to load, optimize, and run your JavaScript code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The event loop: implemented using an event-driven, non-blocking I/O library called libuv to make it lightweight and efficient (and scalable)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Event Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Event-loop is the place where all our asynchronous code is executed.&lt;/p&gt;

&lt;p&gt;Node uses  &lt;a href="https://libuv.org/" rel="noopener noreferrer"&gt;Libuv&lt;/a&gt; as the event loop implementation. To use a Node asynchronous API, you pass a callback function as an argument to that API function, and during the event loop, your callback is executed.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Node.js works
&lt;/h2&gt;

&lt;p&gt;Node JS is a single-threaded javascript runtime environment. Basically, Node JS creator( Ryan Dahl ) concern was that parallel processing using multiple threads is not the right way or too complicated.&lt;/p&gt;

&lt;p&gt;If node.js doesn't use threads how does it handle concurrent requests in parallel?&lt;/p&gt;

&lt;p&gt;Ans: It's a completely wrong sentence when you say it doesn't use threads, Node Js use threads but in a smart way. It uses a single thread to serve all the HTTP requests &amp;amp; multiple threads in the thread pool(in libuv) for handling any blocking operation&lt;/p&gt;

&lt;p&gt;Libuv: A library to handle asynchronous I/O.&lt;/p&gt;

&lt;p&gt;What does the event I/O model mean?&lt;/p&gt;

&lt;p&gt;Ans: The right term is non-blocking I/O. It almost never blocks as the Node.js official site says. When any request goes to the Node server it never queues the request. It takes request and starts executing if it's blocking operation then it's been sent to the working threads area and registered a callback for the same as soon as code execution gets finished, it triggers the same callback and goes to the event queue and processed by the event loop again after that create a response and send to the respective client.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing Node.js
&lt;/h2&gt;

&lt;p&gt;Installing Node on your machine is pretty much easy. If you are on windows, open up your command prompt, and if you use a Mac, open up your terminal. First things first. Let's check if you already have node installed on your machine:&lt;/p&gt;

&lt;p&gt;On your command prompt/terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node --version
&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625594825011%2FtGr7KEIpR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625594825011%2FtGr7KEIpR.png" alt="nodejs-1-logo.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the picture above, I'm running node version 14.4.0 In this tutorial, I'll run you through how to install the latest version of Node on Windows.&lt;/p&gt;

&lt;p&gt;How to install Node on windows&lt;br&gt;
The first step in building any Node.js application is by installing Node on the client system. To perform the installation of Node.js on a Windows machine, follow the steps below:&lt;/p&gt;

&lt;p&gt;Step1) Navigate to  &lt;a href="//nodejs.org"&gt;Nodejs.org&lt;/a&gt; in a web browser. Click the Windows Installer button to download the latest default version.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625594952784%2FwTlTxe3Pt.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625594952784%2FwTlTxe3Pt.jpeg" alt="-jwVJ-JOf.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step2) Once downloaded, double-click on the downloaded.msi file to start the installation. On the first screen, click the Run button to begin the installation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625594997995%2FhcCfsR2vD.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625594997995%2FhcCfsR2vD.jpeg" alt="yYOprr_eY.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step3) You will be welcomed to the Node.js set-up wizard- click Next&lt;/p&gt;

&lt;p&gt;Step4) Following that, you will be directed to review the license agreement. Click Next if you agree to the terms and conditions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625595053069%2Fr6uxRIopR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625595053069%2Fr6uxRIopR.png" alt="latest.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step5) The installer will prompt you to choose a default location. Click Next if you're okay with the default location or change it if need be.&lt;/p&gt;

&lt;p&gt;Step6) The wizard will prompt you to install some other components. Again, click on Next if you have no specific needs&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625595091112%2Fv5bTzfOPH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625595091112%2Fv5bTzfOPH.png" alt="wECvqLkzH__.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step7)Click on the install button to complete the installation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625595132583%2FsdkRkfqPH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1625595132583%2FsdkRkfqPH.png" alt="pFOkCbVkj....png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Your first Node.js program
&lt;/h2&gt;

&lt;p&gt;In our first Node program, we will be creating a simple &lt;em&gt;hello world&lt;/em&gt; program&lt;/p&gt;

&lt;p&gt;Step1) Go to your command prompt (for Windows users) or your terminal (for Mac users) and type the following lines of code:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The first command will create a new folder inside the directory that you're currently in, mkdir = "make directory". While the latter, cd = "change directory", in this case, to navigate to the new dir helloworld&lt;/p&gt;

&lt;p&gt;Step2) Still on your command prompt/terminal, let's initialize our project and link it to npm-acronym for (Node Package Manager) in our folder.&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;Running npm init creates a package.json file in our helloworld folder. This file contains references to all npm packages you will download for use on your application.&lt;/p&gt;

&lt;p&gt;Step5) Next, go to your preferred text editor and create a file- let's call it index.js&lt;/p&gt;

&lt;p&gt;Inside index.js, write the following lines of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var http = require("http");

http.createServer(function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's quickly explain the code snippet above.&lt;/p&gt;

&lt;p&gt;Step 1 - Import Required Module&lt;br&gt;
We use the require directive to load the HTTP module and store the returned HTTP instance into an HTTP variable&lt;/p&gt;

&lt;p&gt;Step 2 - Create Server&lt;br&gt;
We use the created HTTP instance and call http.createServer() method to create a server instance and then we bind it at port 8081 using the listen method associated with the server instance. Pass it a function with parameters request and response. Write the sample implementation to always return "Hello World".&lt;/p&gt;

&lt;p&gt;Step6) Run the app Now to run the app, go back to your command prompt/terminal, and run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now you will see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server running at http://127.0.0.1:8081/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make a Request to the Node.js Server&lt;br&gt;
Open &lt;a href="http://127.0.0.1:8081/" rel="noopener noreferrer"&gt;http://127.0.0.1:8081/&lt;/a&gt; in any browser and observe the result.&lt;/p&gt;

&lt;p&gt;Find this article useful? Kindly like/leave a comment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN BUY ME MY FIRST COFFEE EVER, THANK YOU&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/pratham10" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;.)&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>All you need to know about React Hooks.</title>
      <dc:creator>Pratham</dc:creator>
      <pubDate>Wed, 17 Nov 2021 13:48:23 +0000</pubDate>
      <link>https://forem.com/pratham10/all-you-need-to-know-about-react-hooks-54p0</link>
      <guid>https://forem.com/pratham10/all-you-need-to-know-about-react-hooks-54p0</guid>
      <description>&lt;h3&gt;
  
  
  What are React Hooks ?
&lt;/h3&gt;

&lt;p&gt;React hooks are nothing but JavaScript functions that let you hook into React state and lifecycle features from function components.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: Hooks can only be used inside function components.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hooks allow you to add state to add state to functional components and to share logic across components.&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%2Fiu45jrtu2o5hpej75u8u.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%2Fiu45jrtu2o5hpej75u8u.png" alt="Basics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of using React hooks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are three key advantages to using React Hooks: reusability, readability, and testability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hooks made the React lifecycle easier&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When using lifecycle in class components, the componentDidMount(), componentDidUpdate(), componentWillUnmount() methods are handled individually, but when using React hooks, you can just do everything through the useEffect hook. &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%2Fphu4s1tjqwucspw22g7q.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%2Fphu4s1tjqwucspw22g7q.png" alt="Hooks: under the hood"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rules of Hooks.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can use React Hooks inside function components only.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only call hooks at top level. You cannot call hooks inside loops, conditions, or nested functions, they should always be called at the top of function component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You cannot call hooks from regular JavaScript functions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Built-in and Custom hooks.
&lt;/h3&gt;

&lt;p&gt;React have few built-in hooks like useEffect, useState. You can also create your own custom hooks in React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some common built-in hooks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;useState hook allows you to add state variables in functional components. In useState you pass the initial state to the function and it returns a variable with a value and one function to update the value.&lt;/p&gt;

&lt;p&gt;Let's look at the example below &lt;/p&gt;

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

import { useState } from 'react';

function Example() {
    // Declare a new state variable, which we'll call "count"
    const [count, setCount] = useState(0);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;You clicked {count} times&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;
        Click me
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}


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

&lt;/div&gt;

&lt;p&gt;The above example increments the counter on the handler function parameter itself.&lt;br&gt;
Here, the initial value of the counter is 0, then setCount function is used to update the value of counter.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useEffect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To understand the useEffect hook you first need to understand lifecycle of the component. The lifecycle of the component contains three main parts i.e. mounting, updating and unmounting.&lt;/p&gt;

&lt;p&gt;mounting: when the page loads&lt;br&gt;
updating: when the state updates&lt;br&gt;
unmounting: when user leaves the page&lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

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

import { useState, useEffect } from "react";
import ReactDOM from "react-dom";

function Example() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    setTimeout(() =&amp;gt; {
      setCount((count) =&amp;gt; count + 1);
    }, 1000);
  }, []);

  return &amp;lt;h1&amp;gt;I've rendered {count} times!&amp;lt;/h1&amp;gt;;
}



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

&lt;/div&gt;

&lt;p&gt;Read more here: &lt;a href="https://reactjs.org/docs/hooks-overview.html" rel="noopener noreferrer"&gt;useEffect hooks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional built-in Hooks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useContext&lt;/li&gt;
&lt;li&gt;useReducer&lt;/li&gt;
&lt;li&gt;useCallback&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;li&gt;useRef&lt;/li&gt;
&lt;li&gt;useImperativeHandle&lt;/li&gt;
&lt;li&gt;useLayoutEffect&lt;/li&gt;
&lt;li&gt;useDebugValue&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Thank you for reading, If you enjoyed this post drop a like and comment your thoughts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN BUY ME MY FIRST COFFEE EVER, THANK YOU&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/pratham10" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow me on twitter.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/pratham__10" rel="noopener noreferrer"&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%2Fg3hk69dlodu18eckmzyx.png"&gt;&lt;/a&gt;&lt;/p&gt;

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

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Set up a Node.js Express Server for React</title>
      <dc:creator>Pratham</dc:creator>
      <pubDate>Sun, 25 Jul 2021 12:12:49 +0000</pubDate>
      <link>https://forem.com/pratham10/how-to-set-up-a-node-js-express-server-for-react-2fja</link>
      <guid>https://forem.com/pratham10/how-to-set-up-a-node-js-express-server-for-react-2fja</guid>
      <description>&lt;p&gt;React is a JavaScript library used to build user interfaces and develop single-page applications using reusable UI components. React is used for building front-end apps that can communicate with the back-end server.&lt;/p&gt;

&lt;p&gt;A React front-end connected to a Node back-end is a rock-solid combination for any application you want to build.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This guide is designed to help you create full-stack projects with React as easily as possible.&lt;/em&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;A text editor&lt;/li&gt;
&lt;li&gt;Node.js installed on your computer&lt;/li&gt;
&lt;li&gt;Prior knowledge of React, Node, and npm&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tools You Will Need
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Make sure Node and NPM are installed on your computer. You can download both at  &lt;a href="//nodejs.org"&gt;nodejs.org&lt;/a&gt;  (NPM is included in your Node installation)&lt;/li&gt;
&lt;li&gt;Use a code editor of your choice. I am using and would personally recommend using VSCode. You can download VSCode at  &lt;a href="//code.visualstudio.com"&gt;code.visualstudio.com.&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Make sure you have Git installed on your computer. You can get it at  &lt;a href="//git-scm.com"&gt;git-scm.com&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  STEP 1: BACKEND
&lt;/h2&gt;

&lt;p&gt;First, create a folder of your project, name it as &lt;em&gt;react-node-app&lt;/em&gt; , open this folder in your text editor&lt;/p&gt;

&lt;p&gt;To create our Node project open your terminal and run the following command&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;By running this you will see package.json file has been created in our folder, This file will help us to manage dependencies our app needs.&lt;/p&gt;

&lt;p&gt;Now, in our terminal we will install express as a dependency&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;After that, we will create a script in package.json that will start our web server when we run it with npm start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"scripts": {
  "start": "node server/index.js"
},
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our package.json file should look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "react-node-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "start": "node server/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, create a new folder in our project and name it server.&lt;br&gt;
Inside server create index.js file.&lt;/p&gt;

&lt;p&gt;We'll use Express to create a simple web server for us which runs on port 3001&lt;br&gt;
&lt;/p&gt;

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

const express = require("express");

const PORT = process.env.PORT || 3001;

const app = express();

app.listen(PORT, () =&amp;gt; {
  console.log(`Server listening on ${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now run our app using this script &lt;em&gt;npm start&lt;/em&gt; in our terminal&lt;br&gt;
&lt;/p&gt;

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

&amp;gt; node server/index.js

Server listening on 3001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Create an API Endpoint
&lt;/h2&gt;

&lt;p&gt;We want to use our Node and Express server as an API, so that it can give our React app data.&lt;/p&gt;

&lt;p&gt;set a GET route that we will, later on, fetch from our client-side React App.&lt;br&gt;
&lt;/p&gt;

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

app.get("/api", (req, res) =&amp;gt; {
  res.json({ message: "Hello from Express!" });
});

app.listen(PORT, () =&amp;gt; {
  console.log(`Server listening on ${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you put this above app.listen&lt;/p&gt;

&lt;p&gt;Here, we are simply sending "Hello from Express" to our react app&lt;/p&gt;

&lt;p&gt;Since, we have made changes to our server we have to restart our server&lt;br&gt;
To do that, open your terminal press commmand/ctrl + c, amd then restart the server by running npm start&lt;/p&gt;

&lt;p&gt;Now we can simply visit &lt;a href="http://localhost:3001/api" rel="noopener noreferrer"&gt;http://localhost:3001/api&lt;/a&gt; in our browser and see our message:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;{"message": "Hello from Express"}&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Creating React front-end
&lt;/h2&gt;

&lt;p&gt;Let's move to our front-end part now&lt;/p&gt;

&lt;p&gt;Open another terminal tab and use create-react-app to create a new React project with the name client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In this step, the Webpack development server was auto-generated when we ran the create-react-app command.&lt;/p&gt;

&lt;p&gt;Webpack Development Server (WDS) is a tool that helps the developers make changes to the web app’s frontend and renders them to the browser with no need to refresh the browser.&lt;/p&gt;

&lt;p&gt;The only change we have to make is to add a property called proxy to our package.json file.&lt;/p&gt;

&lt;p&gt;This will allow us to make requests to our Node server without having to provide the origin it is running on (&lt;a href="http://localhost:3001" rel="noopener noreferrer"&gt;http://localhost:3001&lt;/a&gt;) every time we make a network request to it:&lt;br&gt;
&lt;/p&gt;

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

...
"proxy": "http://localhost:3001",
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The modified package.json file will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      "&amp;gt;0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "http://localhost:3001"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can start up our React app by running its start script, which is the same as our Node server. First make sure to cd into the newly-created client folder.&lt;/p&gt;

&lt;p&gt;After that, will start up on localhost:3000:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd client
npm start

Compiled successfully!

You can now view client in the browser.

http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Make HTTP Requests from React to Node
&lt;/h2&gt;

&lt;p&gt;Now that we have a working React app, we want to use it to interact with our API.&lt;/p&gt;

&lt;p&gt;Let's see how to fetch data from the /api endpoint that we created earlier.&lt;/p&gt;

&lt;p&gt;To do so, we can head to the App.js component in our src folder and make an HTTP request using useEffect.&lt;/p&gt;

&lt;p&gt;We will make a simple GET request using the Fetch API to our backend and then have our data returned as JSON.&lt;/p&gt;

&lt;p&gt;Once we have the data returned to us, we will get the message property (to grab our greeting that we sent from the server) and then put it in a state variable called data.&lt;/p&gt;

&lt;p&gt;This will allow us to display that message in our page if we have it. We are using a conditional in our JSX to say that if our data is not there yet, show the text "Loading...".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// client/src/App.js

import React from "react";
import logo from "./logo.svg";
import "./App.css";

function App() {
  const [data, setData] = React.useState(null);

  React.useEffect(() =&amp;gt; {
    fetch("/api")
      .then((res) =&amp;gt; res.json())
      .then((data) =&amp;gt; setData(data.message));
  }, []);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;header className="App-header"&amp;gt;
        &amp;lt;img src={logo} className="App-logo" alt="logo" /&amp;gt;
        &amp;lt;p&amp;gt;{!data ? "Loading..." : data}&amp;lt;/p&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



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

&lt;p&gt;A lot can be done with Express backend, like make calls to the database, but on this guide, we have concentrated on how to quickly connect to the backend Express server from client-side React App.&lt;/p&gt;

&lt;p&gt;The code snippets and files used in this guide can be accessed at the &lt;a href="https://github.com/bhagatpratham/react-node-boilerplate" rel="noopener noreferrer"&gt;GitHub repository here&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IF YOU LIKED THE POST, THEN YOU CAN BUY ME MY FIRST COFFEE EVER, THANK YOU&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/pratham10" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

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

</description>
      <category>react</category>
      <category>node</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
