<?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: Neisha Rose</title>
    <description>The latest articles on Forem by Neisha Rose (@neisha1618).</description>
    <link>https://forem.com/neisha1618</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%2F345867%2F728ccf3b-7e90-470c-9a0d-629e81a6a6d8.jpeg</url>
      <title>Forem: Neisha Rose</title>
      <link>https://forem.com/neisha1618</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/neisha1618"/>
    <language>en</language>
    <item>
      <title>An Introduction to GraphQL</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Sun, 26 Jul 2020 19:12:55 +0000</pubDate>
      <link>https://forem.com/neisha1618/an-introduction-to-graphql-462p</link>
      <guid>https://forem.com/neisha1618/an-introduction-to-graphql-462p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We have been using REST architecture to build APIs. This structure is still very well used but it also has it's issues. REST comes with some problems such as: Having to use multiple urls and over fetching data. There needs to be a solution for us getting the exact data that we want instead of a huge object of items that will not get touched by us.This is where GraphQL comes into play.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is GraphQL
&lt;/h2&gt;

&lt;p&gt;GraphQL is an open-source query language developed by Facebook. &lt;br&gt;
GraphQL structures data in the form of a graph with its powerful query syntax. Because of the data being structured like a graph we can sort through the data as we would a graph by: traversing, retrieving, and modifying data. It provides us with a more efficient way to design, create, and consume our APIs. On the backend, what GraphQL does is essentially creates a middleman service to filter what we get from an api. By doing this it avoids Over-fetching of data and the use of multiple urls.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with GraphQL
&lt;/h2&gt;

&lt;p&gt;To work, GraphQL needs a server and a client. Let's talk about the server. we can attach graphQL to a node server or any other type of server. When this is attached we essentially create a node/graphql server or express/graphql server. Now let's do this using express. The first thing to do is create a basic express server. Next, we need to create an express graphql server. This&lt;br&gt;
require a plugin called express-graphql that creates a /graphql endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the server
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&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="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;graphqlHTTP&lt;/span&gt; &lt;span class="p"&gt;}&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="s1"&gt;express-graphql&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
  type Query {
    welcome: String
  }
`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// root gives us a resolver&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;welcome&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Welcome to the GraphQL World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/graphql&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;graphqlHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;rootValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;graphiql&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="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;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&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;Listening&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Makes fetching data more efficient
&lt;/h2&gt;

&lt;p&gt;Sending a request to an api can give us back more data then we need. GraphQL allows us to send a query to the api to get back what we want. It restricts data that should be fetched from the server. Let's look at an example of a query to fetch data&lt;/p&gt;

&lt;h3&gt;
  
  
  Fetch data example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// basic query&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;students&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;id&lt;/span&gt;
      &lt;span class="nx"&gt;firstName&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Result of the query&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="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;students&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
         &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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;1&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;firstName&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;Paula&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
         &lt;span class="p"&gt;},&lt;/span&gt;
         &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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;2&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;firstName&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;Greg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
         &lt;span class="p"&gt;},&lt;/span&gt;
         &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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;3&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;firstName&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;Stephanie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Avoids using multiple urls
&lt;/h2&gt;

&lt;p&gt;GraphQL Can query multiple relational data in a single request. Which decreases the need for multiple urls/endpoints. By doing this GraphQL utilizes selective endpoints. &lt;br&gt;
Let’s look at an example&lt;/p&gt;

&lt;h3&gt;
  
  
  Who needs multiple urls? Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// multiple url query&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nx"&gt;firstName&lt;/span&gt;
      &lt;span class="nx"&gt;lastName&lt;/span&gt;
      &lt;span class="nx"&gt;college&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;name&lt;/span&gt;
         &lt;span class="nx"&gt;location&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;/// result&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="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;students&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
         &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstName&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;Max&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;lastName&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;Kelly&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;college&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&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; University&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;location&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;Alabama&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normally we would need to send a request to multiple urls but with GraphQL we can do this with just one request. &lt;/p&gt;

&lt;h2&gt;
  
  
  Schema Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Query&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Students&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Students&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;createStudent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;collegeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt; 
  &lt;span class="nf"&gt;updateStudent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;collegeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;    
  &lt;span class="nf"&gt;deleteStudent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;collegeId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;   
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
   &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
   &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
   &lt;span class="nx"&gt;college&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;College&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;College&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
   &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
   &lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;
   &lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="nx"&gt;Student&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;In this schema we set up how our data will look when we request it. We also set up an optional mutation that we can implement to manipulate the data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mutations
&lt;/h3&gt;

&lt;p&gt;Mutations are optional in queries. They are equivalent to CRUD applications in REST. Mutations are how we will modify the data on the server and get updated data back. With these mutations we can: Create, Update, and Delete data entries.&lt;/p&gt;

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

&lt;p&gt;In conclusion, GraphQL makes it easier for us to be selective on the data we receive back from external API’s. GraphQL is language agnostic which makes it easy to incorporate in any type of language. GraphQL can use some of these same techniques and schema to interact with databases. It pairs well with both document and relational database even though it types seems more geared towards document databases&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>graphql</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>React and server side rendering with Next.js</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 20 Jul 2020 01:19:49 +0000</pubDate>
      <link>https://forem.com/neisha1618/react-and-server-side-rendering-with-next-js-cml</link>
      <guid>https://forem.com/neisha1618/react-and-server-side-rendering-with-next-js-cml</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
React, angular, and vue are traditionally client side rendered frameworks that are run in the browser. There are technologies that can run them server-side making them easier and faster to work with. We will talk about one of the frameworks and a brief intro to all of the different things this framework offers. Because Next.js is based off of using React js, to get started working with it a little react knowledge will be great. if you are a little fuzzy on some react concepts I have some great blogs breaking down core react concepts. Now let's talk about Next.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Whats is Next.js&lt;/strong&gt;&lt;br&gt;
Next.js is a minimalist framework for server side rendering of react applications. This framework makes using react easier because it has a lot of built in things under the hood such as:&lt;br&gt;
Server rendered react-apps, page routing, automated code splitting, hot reloading, deployment and built in css support(styles jsx). Once Next.js is installed, we don’t need to do anything on the react side because it is already built in for us. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;br&gt;
First thing you need to do is install Next.js&lt;br&gt;
Npm install next react react-dom&lt;br&gt;
After Next.js is installed we can add some scripts to our package json file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dev&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;next&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;build&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;next build&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;start&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;next start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The package json will have the start script with the server running on port 3000. There is no need to run webpack or anything else dealing with react since it's already built in, that happens behind the scenes. After we install everything and add the scripts, we can create our index.js within a folder called pages. Lets look at an example of a basic index.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;First&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"homepage-container"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
       &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My first page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;This is us creating a basic index.js file that will render a header. You must use export default inside the file such as  with react because this is essentially a react based framework. The export default could go at the bottom of the file but i chose to put it with the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Page Navigation&lt;/strong&gt;&lt;br&gt;
Next.js has a file-system based router built on the concept of pages. In next.js there's a folder called pages. This folder houses all of your react components. The pages folder has built in routing. The built in router means that react-router techniques are made easier. They take advantage of the *Link*functionality in the react router. Whenever you create a new file inside the pages folder, routing is automatically added as the path of that file. In the above example we created a index.js file inside the pages folder.The page will automatically route files with index.js to the ‘/’ root. Let's look at an example of routing by creating another page called first.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Link&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;next/link&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;First&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"homepage-container"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
       &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My first page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      This is for home &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;To homepage&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;So we can start by writing the same code we did for the index.js file. Next we need to import Link so that we can have links set up to different pages. then re need to use the &lt;em&gt;Link&lt;/em&gt; tag and give it a reference. so our reference will be set on the home page or index.js.&lt;br&gt;
We can also do the same for the homepage so our updated file  will look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Link&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;next/link&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"homepage-container"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
       &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My Next.js Home page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
       This is for first&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/first"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;To first page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;&lt;strong&gt;Code Splitting&lt;/strong&gt;&lt;br&gt;
Another built in feature of Next.js is its ability to only run code that is needed at that moment. Because of this it allows the page to load and be quicker. SO that means if you're viewing the homepage then any code that is not dealing with the homepage is not running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Api’s&lt;/strong&gt;&lt;br&gt;
API routes provide a solution to build your API with Next.js.&lt;br&gt;
You first need to create an api folder inside pages folder &lt;br&gt;
Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a Page. Let's look at an example of how this function will look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="nx"&gt;req&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="o"&gt;=&amp;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="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&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="s1"&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="s1"&gt;application/json&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&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="s1"&gt;Neisha&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;&lt;strong&gt;Typescript&lt;/strong&gt;&lt;br&gt;
Another great thing about Next.js is that it provides an integrated Typescript experience out of the box.&lt;br&gt;
Setup is simple, you can create a folder in the root called &lt;em&gt;tsconfig.json&lt;/em&gt;.&lt;br&gt;
Next you will config this file with default values.&lt;br&gt;
When you run &lt;code&gt;npm run dev&lt;/code&gt; the terminal will guide you on how to finish installation to start your typescript refactoring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployment&lt;/strong&gt;&lt;br&gt;
The creators of Next.js have a platform called vercel for deploying. If your project is on a git platform, you can create a vercel account and link the project to the account. Once the account is linked you can then import your project. Once the project is imported, vercel will automatically detect that the project is a Next.js project and do the build config. During development of your Next.js project and deployment phase, they follow the DVP model that stands for: Develop, preview, and ship. In the preview phase vercel automatically creates a new deployment environment with a unique URL where your project can be viewed to preview it when you open a pull request on github. To ship the project just merge the pull request to the master branch, vercel automatically creates a production environment for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next.js optimizes your application for the best performance by code splitting, client-side navigation, and easy deployment.&lt;br&gt;
You can create routes as files under pages and use the built-in Link component. No routing libraries are required.&lt;br&gt;
There is built in functionality that makes creating an app simple and easy which makes Next.js a great framework to learn to pair with your React knowledge. &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Getting started with our PostgreSQL database</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 13 Jul 2020 18:49:34 +0000</pubDate>
      <link>https://forem.com/neisha1618/getting-started-with-our-postgresql-database-3ihm</link>
      <guid>https://forem.com/neisha1618/getting-started-with-our-postgresql-database-3ihm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introducton&lt;/strong&gt;&lt;br&gt;
We will be talking about how to get a database set up in PostgreSQL and establishing the connection to the database and how to do some basic query functions to get us practicing using postgres. Postgresql is an object-relational database meaning it runs with an object-oriented database model: objects, classes and inheritance can be a part of the schema. Because PostgreSQL is a SQL database it makes a good switch for someone who is familiar with databases such as MySQL and mariaDB. The setup is a little different, but the queries are very similar. Which is why the switch to PostgreSQL is not a hard task. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up the database&lt;/strong&gt;&lt;br&gt;
Assuming you already have PostgreSQL installed on your local machine. There are some steps we can take to set up our database.&lt;br&gt;
1) The first thing we need to do is start the postgres server. Run the command &lt;code&gt;sudo service postgresql start&lt;/code&gt;&lt;br&gt;
2) Next, we need to connect to postgres Run the command &lt;br&gt;
&lt;code&gt;sudo -u postgres psql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you should be inside the postgres shell. Now we can create our database. To create the database, we run the command&lt;br&gt;
&lt;code&gt;CREATE DATABASE [name]&lt;/code&gt;&lt;br&gt;
Where name will be the name of the database. Next, we need to connect to the database so run the command: &lt;code&gt;\c [name]&lt;/code&gt; &lt;br&gt;
You should see a message that says the database is connected. At this point you have created the database and now can start adding things to it. If you are familiar with MySQL you can create a &lt;em&gt;schema.sql&lt;/em&gt; file in your code editor, where you can list out your schema such as tables without having to create the table in the command line. To run the &lt;em&gt;schema.sql&lt;/em&gt; file in postgres you first need to cd into the directory that the schema.sql file is located. Once in that directory run the command: &lt;code&gt;\i schema.sql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database Connection&lt;/strong&gt;&lt;br&gt;
Now that we have our schema loaded into the database, we now need to connect establish a database connection. The database connection is like setting up other SQL connections such as MySQL. You will need to know the user, password, host, database name, and port of the PostgreSQL server. The port is something that is different because with MySQL we didn’t have to know the port of the database just the server port. If you haven’t changed the port when you set up postgres its default is 5432. Assuming you are going to be use pg-promse to make queries, your database connection will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pgp&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="s1"&gt;pg-promise&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;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgp&lt;/span&gt;&lt;span class="p"&gt;({&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;DB_USER&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;DB_PASS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My postgress db name&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;Above, user will be set to whatever user you set up for the PostgreSQL database. By default, the user is postgres but if u changed it to maybe your name then user will be your name.&lt;br&gt;
password is the password you initially set for that user. Host is of course local host. database will be the name of the database you want to connect to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Queries&lt;/strong&gt;&lt;br&gt;
Now that we set up the database connection, we can see some example of basic PostgreSQL queries. Let us say your database has a animals table. and we wanted to get all the animals in the database. We could create a function that select all the animals in the database and returns them. Let’s look at an example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getAllAnimals&lt;/span&gt; &lt;span class="o"&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;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;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;animal&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM animals&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animals&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`no animals, &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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because we are using promises to handle the queries, we can use Async. In the above example we establish a function called &lt;em&gt;getAllAnimals&lt;/em&gt;. Next, we use to async keyword meaning this function will return a promise. inside the function we use the &lt;em&gt;try&lt;/em&gt; keyword which essentially means try to do this but if that is not successful, then move on. So, inside the try is where we would put our query. db.any means anything that matches this should be returned. If the query has an error, then we will have our &lt;em&gt;catch&lt;/em&gt; keyword to catch the error.&lt;/p&gt;

&lt;p&gt;To add something into the database we can take similar steps. We create the function and inside the function handle the query and data. The difference will be is how we receive the data. this will normally be done in a post request with an incoming body object with the data that needs to be added. Let us look at an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createAnimal&lt;/span&gt; &lt;span class="o"&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;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INSERT INTO animals (name, color, legCount, 
      diateryNeeds) VALUES ( ${animalName}, ${animalColor}, 
      ${AnimalLegs}, ${animalFood}&lt;/span&gt;&lt;span class="dl"&gt;'&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;body&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;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Animal added&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;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;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;Sorry, no animals&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;Inside our query, we can see where we are inserting values into and then we see some template literals that whole the key values of our incoming object and at the end we put &lt;em&gt;req.body&lt;/em&gt; because that will be where our object is held in the incoming response. We are essentially saying this object will have these keys and insert the value of the keys into the specific database columns.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Conclusion&lt;/em&gt;&lt;br&gt;
In conclusion, we have set up a database in PostgreSQL by creating it and connecting to it. We can of course manually create tables in the terminal, but I feel that having a schema already set up in the file and running it through postgres will save a lot of time. After setting up the tables we then set up the database connection so that we could start creating functions that would execute our queries. And we have set up some basic query functions to get us started with playing with PostgreSQL.&lt;/p&gt;

</description>
      <category>node</category>
      <category>database</category>
      <category>sql</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introduction to Socket.Io</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 06 Jul 2020 01:54:23 +0000</pubDate>
      <link>https://forem.com/neisha1618/a-brief-intorduction-to-socket-io-4lo8</link>
      <guid>https://forem.com/neisha1618/a-brief-intorduction-to-socket-io-4lo8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
When building an application, we can have a real-time way that allow the client and the server to communicate. such as implementing a chat application between multiple users. To implement this messages, need to be sent constantly without the page refreshing. We can implement this type of client/server communication by using web sockets. The problem with web sockets is that not every browser is compatible and web sockets do not wok well with a firewall. So, for this reason we can use an alternative library called Socket.io. This library is built off web sockets but can be used with less restrictions. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is socket.io&lt;/strong&gt;&lt;br&gt;
Socket.IO is a JavaScript library for real time web applications. This Library utilizes the Client/Server Architecture to allow communication between the client and server. It is great to add real-time communication to a website and is based on an event-driven system that listens for specific events to be triggered.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server-side&lt;/strong&gt;&lt;br&gt;
First, we need to install Socket.Io. and then we will need some way to connect to the server, so we can also install express. &lt;br&gt;
&lt;em&gt;npm install socket.io&lt;/em&gt;&lt;br&gt;
&lt;em&gt;npm install express --save&lt;/em&gt;&lt;br&gt;
     or &lt;br&gt;
&lt;em&gt;npm install socket.io express --save&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once we have out dependencies installed, we will now create our server and establish a connection.&lt;br&gt;
In the example below we start off require what we need to set up a basic http server. We give it a port of 8080 and a we set a variable called server. this variable is us setting up the node server instance which we will need require Socket.Io because it must have a server connection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&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;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express &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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8080&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;server&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;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;createServer&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;io&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;socket.io&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socket&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="cm"&gt;/*
the socket param is the bridge that connects the user to the server so where data will be transferred through
*/&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="s2"&gt;Connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;homepage&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;Server is connected to homepage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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="s2"&gt;Server Is Running on Port: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;port&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;the next thing we see is us establishing the connection between the http server and the socket. The first param takes is an event. and in this case the event is a "connection" meaning once something connects to it, then it will activate a callback function that will first log that its connected. The next thing we do is to set up a custom event on the server using &lt;em&gt;socket.emit&lt;/em&gt;. In this example whenever the homepage event is connected, we will have it send a message to the user or client "Server is connected to the homepage".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client-side&lt;/strong&gt;&lt;br&gt;
We have established a server and have a way for the server and client to communicate. Now we need to set up the client to complete the connection. The first thing to do is to install the Socket.IO client library. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm install socket.io-client&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to require the library and establish a socket variable which we be the connection to the server. Remember we have that &lt;em&gt;socket&lt;/em&gt; param in the callback function on the sever side, so we need to keep the name the same. this is where the bridge between the client and server is set up. &lt;br&gt;
The socket variable will be set to the host and port that our server will be listening on so they must be the same. Let us look at the example below to visualize this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;io&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;socket.io-client&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;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;homepage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;  &lt;span class="nx"&gt;when&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;homepage&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;triggered&lt;/span&gt; &lt;span class="nx"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;we&lt;/span&gt; &lt;span class="nx"&gt;have&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;will&lt;/span&gt; &lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="nx"&gt;triggered&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&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;we received data from the server&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="nx"&gt;param&lt;/span&gt; &lt;span class="nx"&gt;will&lt;/span&gt; &lt;span class="nx"&gt;hold&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="nx"&gt;sends&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;us&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;emit&lt;/span&gt; &lt;span class="nx"&gt;we&lt;/span&gt; &lt;span class="nx"&gt;saw&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="nx"&gt;side&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;which&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;will&lt;/span&gt; &lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is connected to homepage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;In&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;instance&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="nx"&gt;was&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;but&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;IO&lt;/span&gt; &lt;span class="nx"&gt;can&lt;/span&gt; &lt;span class="nx"&gt;send&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt; &lt;span class="nx"&gt;different&lt;/span&gt; &lt;span class="nx"&gt;types&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="nx"&gt;such&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;images&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;videos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;audios&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt; &lt;span class="nx"&gt;etc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
We have set up a basic socket connection between the client and server that can eventually be used to implement some real time communication such as chat features and even other real time features of communication. With Socket.Io we do not have to worry about the connection being interrupted due to different browsers or firewalls such as if we were to use strictly web sockets.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>Navigating single page applications with React Router.</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 29 Jun 2020 03:23:07 +0000</pubDate>
      <link>https://forem.com/neisha1618/navigating-single-page-applications-with-react-router-3lgh</link>
      <guid>https://forem.com/neisha1618/navigating-single-page-applications-with-react-router-3lgh</guid>
      <description>&lt;p&gt;When developing an application, we may want to start with a single page application (SPA). A SPA is a website that re-renders its content to a new set of content without making a request to the server to fetch new HTML. A single page application utilizes navigation to give the user the feel of multiple pages in one. One way to incorporate navigation into our SPA is to use react router&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting started with React Router&lt;/strong&gt;&lt;br&gt;
If using React as a front-end framework, we will not be able to route through different parts of our application solely with react. We will need the help of another library called react-router. If using the node package manager, we First we need to &lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm install react-router-dom&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After installing the dependency, we can now create a component that will house these navigation links and routes. We can put these routes in the parent component of the app., but these routes may be larger and space consuming so let us just create a component called Navigation.jsx. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using React Router&lt;/strong&gt;&lt;br&gt;
Next, we will need to get access to the library we just installed. We need to Import react-router-dom and all the built-in goodies it gives us access to. HashRouter or its alias Router will be the outer tags that all our routes and links will be contained in. For now, we will import &lt;em&gt;Link&lt;/em&gt; in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;HashRouter&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;,&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-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have what we need imported in we can create a class or functional component. Here I chose to you functional. Inside the return we add our router tags and inside them some structural tags such as &lt;em&gt;divs&lt;/em&gt;, &lt;em&gt;li&lt;/em&gt;, and ul. Next, we will put a &lt;em&gt;Link to&lt;/em&gt; tag. this is equivalent to putting a &lt;em&gt;href&lt;/em&gt; tag, it creates a clickable link to the multiple pages we will render. With this tag we create a path that we will use later and the name of the link e.g. Home, Login, Profile. Tak a look at the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Navigation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/login"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Login&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/profile"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Profile&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Switching between pages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The next two things to add to the imports is the &lt;em&gt;Switch&lt;/em&gt; and &lt;em&gt;Route&lt;/em&gt; tag. Under the link tags we can add the switch tag. the switch tags will allow us to switch between different pages. so, within the switch tag we will set up our routes to the multiple pages we will have. You will also notice that we imported some other components. We will be setting up the routes to these components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;HashRouter&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;,&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-router-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="nx"&gt;Profile&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;./Profile.jsx&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;Home&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;./Explore.jsx&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;Login&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;./Login.jsx&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;Navigation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/login"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Login&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/profile"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Profile&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/login"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Login&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/profile"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&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;Above, in the &lt;em&gt;switch&lt;/em&gt; tag we put a &lt;em&gt;route&lt;/em&gt; tag and here we will set the route for a certain path. the path is the path to the page you want. So, in our case we need a path to the home page, profile page, and explore page. Once we set the paths, we will render those components. So right now in our application, when we click on the  link to either page it should render what ever is inside that component. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have just set up a basic routing system that makes it so that we can have multiple pages in our single page application. React router comes with more build in things that allows us to make our navigation more dynamic the more complicated our components get. There is ae some built in hooks that also makes our life much easier with navigation.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>In Sync with Asynchronous Request Methods: Axios</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 01 Jun 2020 01:52:31 +0000</pubDate>
      <link>https://forem.com/neisha1618/staying-in-sync-with-asynchronous-request-methods-axios-2ilh</link>
      <guid>https://forem.com/neisha1618/staying-in-sync-with-asynchronous-request-methods-axios-2ilh</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;When developing a full stack application with outside resources such as an API, we need a way for the front end to communicate with the server in the back end to get some data. There are multiple ways to do this such as using the reacts built in fetch API method and the XMLHttpRequest or we could use jQuery’s AJAX requests. The issue with using AJAX is that there are a lot of components that go with making a request and most of all we would love to distance ourselves from jQuery. Today we will be discussing an alternative, using Axios.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Axios
&lt;/h2&gt;

&lt;p&gt;Axios is a promise-based HTTP request library that allows us to interact with rest API's. Because Axios is a promise-based library it supports making asynchronous request so that your browser can function normally while waiting for the request to be handled. We will now talk about how to make HTTP requests using Axios.&lt;/p&gt;

&lt;h2&gt;
  
  
  GET Request
&lt;/h2&gt;

&lt;p&gt;When we want to get some data we can make an &lt;em&gt;axios.get()&lt;/em&gt; request. The first parameter is a URL to perform the request on. The response that comes back, if the promise is resolved, will be contained in the &lt;em&gt;.then()&lt;/em&gt; and will be an object that consists of keys of data, status, status text, and headers.  The data key will contain the data we set out to request. Let us look at an example.&lt;/p&gt;

&lt;h3&gt;
  
  
  One way to make a GET request
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxmrtg65vx7j92rqg7u6p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxmrtg65vx7j92rqg7u6p.jpg" alt="Alt Text" width="576" height="278"&gt;&lt;/a&gt;&lt;br&gt;
In the example above, we see the first way we can get data using the axios get method. It consists of and object with keys for the method and endpoint for the URL we want to get the data from. &lt;/p&gt;

&lt;h3&gt;
  
  
  Another way to make a GET request
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3bxzkbtqg2vi82k5hwjm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3bxzkbtqg2vi82k5hwjm.jpg" alt="Alt Text" width="582" height="277"&gt;&lt;/a&gt;&lt;br&gt;
In this example, we can attach the HTTP method on axios and just pass in the URL as the parameters then chain on our then and catch on to it. This way is much easier to read and does not take up a lot of space.&lt;/p&gt;

&lt;h2&gt;
  
  
  POST Request
&lt;/h2&gt;

&lt;p&gt;If we want to post some data to a specific endpoint, we can use the &lt;em&gt;axios.post()&lt;/em&gt; method. The first parameter is a specific URL or endpoint. the second parameter is and object containing the data we want to send to our server. &lt;/p&gt;

&lt;h3&gt;
  
  
  One way to make a POST request
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fozkicmpm4s3dt7hsb87e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fozkicmpm4s3dt7hsb87e.jpg" alt="Alt Text" width="548" height="291"&gt;&lt;/a&gt;&lt;br&gt;
In the above example we pass in a second parameter which is an object of the data we want to send to the server&lt;/p&gt;

&lt;h3&gt;
  
  
  Another way to make a POST request
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftnjp3qitf5um85sfetvm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftnjp3qitf5um85sfetvm.jpg" alt="Alt Text" width="695" height="330"&gt;&lt;/a&gt;&lt;br&gt;
In this example we see an easier wat to write the same request &lt;/p&gt;

&lt;h2&gt;
  
  
  Axios.all
&lt;/h2&gt;

&lt;p&gt;Because axios utilizes promises, it uses a familiar promise method. We can have multiple request happen simultaneously with the &lt;em&gt;axios.all()&lt;/em&gt; method. This method takes in an array of requests and returns and object if both request resolves. even though it can take multiple request we only need to chain on one &lt;em&gt;then()&lt;/em&gt; and one &lt;em&gt;catch()&lt;/em&gt; to it. Let us look at an example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Axios.all example
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faw61gwh988omvvhg825a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faw61gwh988omvvhg825a.jpg" alt="Alt Text" width="703" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In conclusion, Axios is a library used to make HTTP requests. Because of its asynchronous nature we can be sure that our browser can function without interruption while waiting for the request to be resolved. Axios comes with a lot of built in methods that make making these request easy to write.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Express Routing</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 25 May 2020 03:25:25 +0000</pubDate>
      <link>https://forem.com/neisha1618/express-routing-52e2</link>
      <guid>https://forem.com/neisha1618/express-routing-52e2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
When we use the GPS in our car we put in a certain destination. the GPS then gives us a route to take to get to our destination. If we deviate from that route the GPS re-routes us to get to that same endpoint. Webpages are the same in that, when we put in a specific endpoint on a site the server routes the request to a certain file in our code to handle that request. We will look at a framework that makes this possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Express&lt;/strong&gt;&lt;br&gt;
Express is a free unopinionated node.js framework whose features allow for building out an application. Some features of using express includes: routing, handling middleware, creating API's, rendering HTML view, and many more things. The thing we will focus on today is routing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routing&lt;/strong&gt;&lt;br&gt;
When a client makes a request for information, it is usually to a specific endpoint. For example, if we search &lt;a href="http://www.google.com/" rel="noopener noreferrer"&gt;www.google.com/&lt;/a&gt; we are routed to googles home page. if we add &lt;em&gt;'/imghp'&lt;/em&gt; to the endpoint then google servers will reroute us to the google image home page. Routing is used to determine how an application responds to a client request to an endpoint and a specific HTTP request methods such as get, post, put, and patch. We can have different http methods on a specific endpoint, but we can have the same methods on the same endpoint. for example, two GET methods both routing to the same endpoint. Let us look at an example of establishing a route in a index.js file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routing example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwe57ewtfq2slt3aw3mel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwe57ewtfq2slt3aw3mel.png" alt="Alt Text" width="727" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we first need to require express in our files. Once express is required we can set a variable like app to express and use that whenever we want to utilize a method. Each route can have one or more handler functions, which tells our server what to do when a request to the specific endpoint is made. most of the time we can route it to a specific file that will handle all the functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Route Chaining&lt;/strong&gt;&lt;br&gt;
Having multiple routes to various endpoints can become space consuming. Let us look at an example of having multiple multiple routes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F89r04e51edocuqjmigix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F89r04e51edocuqjmigix.png" alt="Alt Text" width="561" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example we have multiple routes set up. As we can see some of these routes are to the same endpoint. Express gives us a method called &lt;em&gt;route&lt;/em&gt; that gives us a way to chain on HTTP methods that go with the same endpoint. Let’s look at how we would chain on these methods using the express &lt;em&gt;route&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzoeaukjmsow4cihhwhfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzoeaukjmsow4cihhwhfj.png" alt="Alt Text" width="690" height="732"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we have our &lt;em&gt;app.route()&lt;/em&gt; method. Inside of that we can put our route. Next, we can chain on multiple HTTP methods to that one route. This frees up a lot of space and allows us to reuse code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In conclusion, Express gives us a way to organize our code in a way that we can handle requests to endpoints in a certain way that benefits our MVC style. We start of by importing express into our files. and organizing our in a file. when the request is routed to the handler it can execute according to the code we built. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>express</category>
    </item>
    <item>
      <title>React LifeCycle Methods</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Sun, 17 May 2020 20:46:33 +0000</pubDate>
      <link>https://forem.com/neisha1618/react-lifecycle-methods-5ci4</link>
      <guid>https://forem.com/neisha1618/react-lifecycle-methods-5ci4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
Just like we have stages in our life, react components has their own stages. There are different methods dependent on what stage the component is in. The method in these stages happen in a sequential order so it is best to be comfortable with them to best utilize them and make our code cleaner. With these different stages come a structured way to manipulate our elements. We can break a react lifecycle into three main parts: Mounting – When it is initializing, Updating– When the component changes and Unmount – When it is destroyed. Before we start talking about the lifecycle, we will briefly discuss components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a React Component&lt;/strong&gt; &lt;br&gt;
The structure of an app is broken down into smaller specialized elements called components. Each component is made to contribute to a feature of an eventual User Interface (UI). There are two types of components: class components and functional components. We will focus on the class component in taking a journey through Reacts Lifecycle methods. Below we will show and example of a simple component for creating a fake background component based on the weather. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F63lkclto94bsghmka6tf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F63lkclto94bsghmka6tf.png" alt="Alt Text" width="648" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;here in our simple component we have a state with a weather value and a color value. this is the start of this component life cycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mounting&lt;/strong&gt;&lt;br&gt;
This is the stage of a component being initialized. In this stage some prominent lifecycle Methods include: &lt;em&gt;render()&lt;/em&gt; and &lt;em&gt;componentDidMount()&lt;/em&gt;.&lt;br&gt;
Render is one of the most used lifecycles methods because it is the only one needed to make the component class work. We use render to put elements on the user interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxgoxepr1piqrtm24qprg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxgoxepr1piqrtm24qprg.png" alt="Alt Text" width="623" height="385"&gt;&lt;/a&gt;       &lt;/p&gt;

&lt;p&gt;Whatever is inside the render function will be shown on the user interface (UI).&lt;br&gt;
This method happens during the mounting and updating stages of your component. Whenever something needs to be shown on the screen or changes be made, render will always be called. One thing we can't do is change the state inside the render function so we need other ways to do this that won’t produce problems in our component. Now we will look at another method in the mounting lifecycle called &lt;em&gt;componentDidMount()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;componentDidMount &lt;/p&gt;

&lt;p&gt;This method is immediately invoked, it renders before the browser comes on the screen. Because of this, if ever we needed to initiate api calls, they can be placed in this method and we can guarantee that the data we need will be displayed on the screen before it loads. This method also allows the use of &lt;em&gt;setState()&lt;/em&gt;, so it is also ideal for changing the state inside this method. Let us look at an example of changing the state in this method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftgs62x26k5aalad5rpz8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftgs62x26k5aalad5rpz8.png" alt="Alt Text" width="593" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updating&lt;/strong&gt;&lt;br&gt;
Just like it sounds, these are methods for updating a change on the DOM. One popular method to use is &lt;em&gt;componentDidUpdate()&lt;/em&gt;. This methods usually consist of a conditional to check if a change occurred and if true what needs to be shown on the screen. for example:&lt;br&gt;
   we will check if the previous color doesn’t equal the current color then we will change the color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5w1p8b76a05w7y9k69y8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5w1p8b76a05w7y9k69y8.png" alt="Alt Text" width="667" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;having this method is useful when a website or app have user interactions. we can have update methods to be able to maintain the UI based on these possible interactions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Un-mounting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a component has met the end of its journey, we have methods to cleanup code before they're destroyed or removed. Things like setIntervals and api calls do not have to constantly run. If they do it can cause constant rendering which can affect our page. Common cleanup activities performed in this method include, clearing timers, cancelling api calls, or clearing any caches in storage. let's look at &lt;em&gt;componentWillUnmount()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0469c0zpaed6f8scz1g6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0469c0zpaed6f8scz1g6.png" alt="Alt Text" width="693" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s say we added a set Interval function that fetch the weather for us every 30secs. the only way this will stop is with a clear Interval function. The best place to put a clear Interval function is in a componentWillUnmount function which will trigger the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In conclusion, react components comes with some built in lifecycle methods that will make it easier to manipulate elements in our components and certain points. As we progress through a components lifecycle there is an order of execution. These methods allow our code to be much cleaner. &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Client/Server Architecture</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 11 May 2020 00:21:39 +0000</pubDate>
      <link>https://forem.com/neisha1618/client-server-architecture-efe</link>
      <guid>https://forem.com/neisha1618/client-server-architecture-efe</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How do I boil an egg? Why is the sky blue? Why can’t I sneeze with my eyes open? These are philosophical questions we ask google all day when we think about it.  When we get the answer in a millisecond, we may not realize the complexity of requesting and getting that information. Where do the information come from? The sky of course. There is an architecture that describes the way we request and receive this information and the ends and out of how it happens. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Client&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A client is a machine or program such as: A Desktop computer, laptop, tablets, and smartphones that allows users to make request for information through the web. A single client can connect to numerous servers at a single time, where each server provides a different set of services to that specific client. when we type &lt;a href="http://www.facebook.com" rel="noopener noreferrer"&gt;www.facebook.com&lt;/a&gt; our computer is the client now request the Facebook page to load up. now when we see the page pop up on our device that is a response gifted to us from facebook’s server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just like a client a server is a computer or computer program. A server manages, store, send and process data 24-hours a day. A server can contain web resources, host web applications, and store user or program data. Most servers have a one-to-many relationship with clients, meaning a single server can provide resources to multiple clients at one time. Even though servers can handle multiple clients sometimes so many clients can overwhelm a server so a platform may use multiple servers to handle the traffic. for example, google, when you are typing in the search through google you are making a request to googles 900,000 servers. And since we may want to know how many legs a snake has at 11:00pm a server stays listening for a request&lt;br&gt;
                         &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3hqpzfvy6ikh29r14n6m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3hqpzfvy6ikh29r14n6m.jpg" alt="Alt Text" width="500" height="650"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we distinguished the difference between a client and server let’s talk about the structure in which they interact with each other&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client/Server Model&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;A client/Server model is an architecture on the web that splits computers into two sections. Computers that ask for and request services (Clients) and Computer's that service or give a response (Servers). The client sends across the request to the Server in the form of XML or JSON which both entities (Client and Server) understand. After understanding the request Server responds with appropriate data by sending back a Response. The client and servers can be two different computers in different parts of the world that are connected through the Internet. The client and the server could also reside miles apart or within the same building as well. But they also can be running on the same machine. The client and server model allows a separation that can make data storage and retrieval more efficient. Next, we will discuss hot the client and serve communicate &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hyper Text Transfer Protocol Secure (HTTP)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we can see in the name HTTP is a protocol or set of rules used to communicate and exchange data between the client and server. HTTP is a specific set of languages along with a communication standard, for the interaction of two systems. HTTP is connectionless meaning, after making the request the client disconnects from the server. Then the server re-connects to the client to give the response. Clients establish a connection to the server via HTTP protocol. We know these protocols as GET, POST, PUT, PATCH, and DELETE. &lt;/p&gt;

&lt;p&gt;Let’s say we want to request information about pets our HTTP verbs will look like this          &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%2Fi%2Fivwlmwyfvq2mqg20qskz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fivwlmwyfvq2mqg20qskz.png" alt="Alt Text" width="509" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we are talking about now is the request/response cycle with the client/server architecture&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request/Response Cycle&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fszsv6ja5gqqbhrqxoy20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fszsv6ja5gqqbhrqxoy20.png" alt="Alt Text" width="573" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The client server model work through a request/response cycles via HTTP methods&lt;br&gt;
In the client-server architecture, the client computer sends a request for data to the server through the internet in the form of an HTTP message, The server accepts the requested, processes it and deliver the data packets requested back to the client also in the form of an HTTP message. The data packet sent back from the server includes a status code on if the request was successful or not, and if successful the requested material contained in a message body. A successful request would be a status code of “200” and an unsuccessful request would be a “404”. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, even though its complex the client/server architecture at its core is not as complicated as it seems. Taking a deep dive there are many layers to it that makes it easy for us to get information quickly and efficiently.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Inheritance: Prototypal vs Pseudoclassical</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 04 May 2020 08:17:18 +0000</pubDate>
      <link>https://forem.com/neisha1618/prototypal-vs-pseudoclassical-inheritance-4p5k</link>
      <guid>https://forem.com/neisha1618/prototypal-vs-pseudoclassical-inheritance-4p5k</guid>
      <description>&lt;p&gt;When we are conceived, we inherit genes from both of our parents while being able to function as our own being. In functions there's a way to do the same. there are a handful of methods we used to create inheritance paterns. Prototypical instantiation requires that the object be created and returned, where as the pseudoclassical instantiation pattern does this by insertion of the keyword 'new' in front of the class name. &lt;/p&gt;

&lt;p&gt;Pseudoclassical vs Prototypal&lt;/p&gt;

&lt;p&gt;here we have a chart that shows the different ways on how prototypal differentiate from  pseudoclassical we will later discuss more detail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb06etggvt8cbbasf1rad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb06etggvt8cbbasf1rad.png" alt="Alt Text" width="730" height="851"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Prototypal Instantiation &lt;/p&gt;

&lt;p&gt;This type of instantiation pattern is different then functional-shared that you may have learned about before this. Instead of initializing a variable to an empty object, you initialize it to equal Object.create which creates a new empty object that will then be use to have a prototype to the methods. Basically the classes will all have a special relationship called prototype delegation where if there’s a class method invocation then the class will check the prototype if there are any methods that match.&lt;/p&gt;

&lt;p&gt;Prototypal inheritance uses:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;'Object.create' (to create a new object)&lt;/li&gt;
&lt;li&gt;A parameter object (the prototype for the new object)
As mentioned above, here we are creating new objects directly from existing ones, without any notion of classes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of Prototypal&lt;/p&gt;

&lt;p&gt;In this example we use a method called Object.create to create a new object that has reference to prototpes that our function can use if needed. We also add the name variable to reference back to using the this keyword.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1jzgd0fowo4326e27z42.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1jzgd0fowo4326e27z42.png" alt="Alt Text" width="602" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pseudoclassical Inheritance&lt;/p&gt;

&lt;p&gt;Same as Prototypal except that it doesn’t even need an object to be created. You can use the “this” keyword and it will automatically know that you’re using the Pseudoclassical style and thus will generate a “var this — Object.create(whatever-name-of-class.prototype)”&lt;br&gt;
This pattern involves creating pseudoclasses using constructor functions, then it uses the 'new' keyword to create instances of the pseudoclass. The prototype property is inherited by all instances and contains the methods common to it.&lt;/p&gt;

&lt;p&gt;Pseudoclassical inheritance uses:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The 'constructor' function to create objects&lt;/li&gt;
&lt;li&gt;The 'new' operator to create objects&lt;/li&gt;
&lt;li&gt;The 'prototype' property to build the inheritance chain&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of Pseudoclassical&lt;/p&gt;

&lt;p&gt;In this example we can see that we dont need the Object.create and also we dont need to return the object. The 'new keyword handles both operations for us. But the way we handle the prototype is the same. There is another we we can manipulate pseudoclassical and thats with the es6 classes&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff59ur36i7tefwzes9dlv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff59ur36i7tefwzes9dlv.png" alt="Alt Text" width="697" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example of es6 Pseudoclassical Classes&lt;/p&gt;

&lt;p&gt;In this example we replace the const keyword with the class keyword and add a constructor inside. Also we change the we we write out our methods without having to use the prototype keyword&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5ao0o2l8v492su17wttx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5ao0o2l8v492su17wttx.png" alt="Alt Text" width="657" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;In conclusion, Looking back at our tree constructor, the two most important parts were creating the object and returning it. Without creating the object with Object.create, we wouldn’t be able to delegate to the function’s prototype on failed lookups. Without the return statement, we wouldn’t ever get back the created object. With Prototypal the 'new' keyword handles that and also protect against failed lookups.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Callbacks vs Promises</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Sun, 05 Apr 2020 21:03:57 +0000</pubDate>
      <link>https://forem.com/neisha1618/callbacks-vs-promises-4mi1</link>
      <guid>https://forem.com/neisha1618/callbacks-vs-promises-4mi1</guid>
      <description>&lt;p&gt;&lt;strong&gt;The Goal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The goal is to achieve asynchronous code. Async code allows multiple things to happen at the same time. When you start an action, your program continues to run. When the action finishes, the program is informed and gets access to the result. We can achieve async code using two methods: callbacks and promises. With callback we pass a callback into a function that would then get called upon completion. With promises, you attach callbacks on the returned promise object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A callback is a function that is to be executed after another function has finished executing. Async callbacks are functions that are passed as arguments. and when that function is called it will start executing code in the background. When the background code finishes running, it calls the callback function to let you know the work is done. We use these callbacks because we want to avoid executing things out of order. If we want to wait for something in JavaScript, we need to use a callback.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s make that pb&amp;amp;J from scratch using callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;synchronous code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7r5paazf1e95v27qidog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7r5paazf1e95v27qidog.png" alt="Alt Text" width="530" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This synchronous Peanut Butter &amp;amp; Jelly function runs in order, one function after another. but what if we had a function that needed to be ran first and other functions couldn't be ran until after this function finishes. Let’s think of making bread from scratch. you can’t put the peanut butter and jelly on the bread until it’s made so you must wait until its done. With synchronous code it doesn’t wait it just does it. How can we fix it?&lt;/p&gt;

&lt;p&gt;Async Callbacks&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj7433hli4ayef180sml1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj7433hli4ayef180sml1.png" alt="Alt Text" width="565" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We make an async callback so that we can make sure no other function runs until our bread is made. Let’s picture inside all the other functions there’s ample amounts of code to run. This can cause an issue because you can have plenty of nested callbacks inside one another. That leads to what we call callback hell. Callback hell can riddle code with bugs that are hard to catch. For this we need a way to make aync code while avoiding so many nested callbacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Promises&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Promises are native to JavaScript, but you can also install promises libraries such as: Bluebird and Q. Promises are JavaScript objects that represent an eventual completion or failure of an asynchronous operation. A promise is a returned object where you attach callbacks, instead of passing callbacks into a function. the place where you attach the callback after a successful completion of a task is called, .then(). inside this you pass a callback through. What makes promises a way to avoid callback hell is that you can chain multiple .then() on each other which avoid nested callbacks and a neater line of code. For the failure of completing a task you can pass it through a .catch().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s change our callback to a promise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs262moo7d6asczrlbwny.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs262moo7d6asczrlbwny.png" alt="Alt Text" width="527" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we take our PB&amp;amp;J function and turn it into a promise. We will first return the makeBread function and then on the successful completion of that function we will return a promise that will pass in the next callback to be ran. Next, we will chain on the other function that will be ran after that in order. thus, making async function. As you can see the code is neither and we avoid callback hell.&lt;br&gt;
We can also chain on an error message to the .catch() method and on that message "ewww crunchy peanut butter" because that will just ruin my sandwich.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Promise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s make our promise a little bit neater by just passing in the callbacks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyangenumh9g9l9aqngno.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyangenumh9g9l9aqngno.png" alt="Alt Text" width="557" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Both callbacks and promises help make our code asynchronous. Making callbacks async can cause issues such as callback hell, so to avoid this we can use promises instead, doing this helps us avoid this pitfall while keeping our code async and neat.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callbacks</category>
      <category>promises</category>
    </item>
    <item>
      <title>React: Passing Data between Components</title>
      <dc:creator>Neisha Rose</dc:creator>
      <pubDate>Mon, 30 Mar 2020 15:51:18 +0000</pubDate>
      <link>https://forem.com/neisha1618/react-passing-data-between-components-f2p</link>
      <guid>https://forem.com/neisha1618/react-passing-data-between-components-f2p</guid>
      <description>&lt;p&gt;&lt;strong&gt;React Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React is a JavaScript library for building user interfaces (UI). What makes react so popular for developers is its ability to build UI's more easily and manageable. React uses a syntax called JSX to describe how the UI will look.&lt;br&gt;
instead of putting JavaScript into HTML, JSX allows us to put HTML into JavaScript. react needs a trans compiler called babel to bridge the gap between the languages to something the computer understands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State/Props&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React manages its data through a state. State is a JavaScript object whose values are mutable. State can only be used inside of the parent component so if we wanted access to the state data in other components, we will need props. Props like state is also a JavaScript object but values are immutable. the props are what other components will use to have access to the data within the state. and props is how we will pass data from a parent component to a child component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passing data from a Parent to a Child&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To pass data from a parent component to a child component we will need access to the state data outside of the state. so, for this we will use props. let's see an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwtgsulv19h3y47z08dio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwtgsulv19h3y47z08dio.png" alt="Alt Text" width="570" height="407"&gt;&lt;/a&gt;&lt;br&gt;
                                                 here we declare child one and give it values equal to {this.props.value} &lt;/p&gt;

&lt;p&gt;child 1&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2kv2c1vlhg93z78bo7rw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2kv2c1vlhg93z78bo7rw.png" alt="Alt Text" width="488" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passing data from a child to a parent&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             **Let’s add a second component**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;new parent&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq2vtu0pcmrwkqkx98sdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq2vtu0pcmrwkqkx98sdl.png" alt="Alt Text" width="647" height="451"&gt;&lt;/a&gt;&lt;br&gt;
                                            We added child two as a div and gave &lt;br&gt;
                                            it props from the state&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;child2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foykaxym2psb3ro1ywc8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foykaxym2psb3ro1ywc8b.png" alt="Alt Text" width="578" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this new component we want to change the value of my prop, but as stated earlier, props are immutable so to change the value of my prop we need a way to change the value in the state. The easiest way to do that is to add a function in the component to manipulate the state in the parent component. to do this we added a method that onclick will trigger the change of state int he parent component so we can have a new value to color. let’s see how the second component and new parent component will look&lt;/p&gt;

&lt;p&gt;Component 2 with added function&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjbhsy030y0ocjvw7ipry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjbhsy030y0ocjvw7ipry.png" alt="Alt Text" width="670" height="286"&gt;&lt;/a&gt;&lt;br&gt;
                                          here you can see we bind the function &lt;br&gt;
                                          to the scope we want.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New parent function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvwdpvz47d3bsisz2my0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvwdpvz47d3bsisz2my0a.png" alt="Alt Text" width="656" height="562"&gt;&lt;/a&gt;&lt;br&gt;
                                         In the parent function we added a &lt;br&gt;
                                     function to the state that will be triggered&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The ladder&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Passing data from a child to a parent is fine when there is one child component. When there is more than one, we have to make sure the data is also passed to those components from the siblings. We have an order in which we change the parent component. we can’t just jump from component 3 to the parent while skipping pass component 2Lets add the new prop to component one so that it can take in the new value of the state also.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Child 1&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flhg7ntkj3qccgecm0si1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flhg7ntkj3qccgecm0si1.png" alt="Alt Text" width="521" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s easy to pass props from a parent to a child but to pass values from a child to a parent is a little extra, we must create a function. We must create a function in the component that will trigger a change to the state. then create that function in the parent component's state. It is also very easy for us to get to pass data between siblings to make sure they get the same interactions&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
