<?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: tywenk</title>
    <description>The latest articles on Forem by tywenk (@tywenk).</description>
    <link>https://forem.com/tywenk</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%2F820853%2Fc704b83b-5f2d-4284-94b8-f17cf021c8d6.png</url>
      <title>Forem: tywenk</title>
      <link>https://forem.com/tywenk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tywenk"/>
    <language>en</language>
    <item>
      <title>Javascript Shorthands: Ternary, Logical || and &amp;&amp; Assignments</title>
      <dc:creator>tywenk</dc:creator>
      <pubDate>Tue, 12 Apr 2022 19:37:25 +0000</pubDate>
      <link>https://forem.com/tywenk/javascript-shorthands-ternary-logical-and-assignments-14ea</link>
      <guid>https://forem.com/tywenk/javascript-shorthands-ternary-logical-and-assignments-14ea</guid>
      <description>&lt;h1&gt;
  
  
  Gotta go fast
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Why waste time say lot word when few word do trick?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is perhaps the insidious mantra of programmers the world over. Too busy hacking into the mainframe they have no time for multisyllabic variable names. They don't have the bandwidth in their own minds to cache long winded expressions. They would rather use --nay, they NEED to use-- terse, to-the-point, language to snipe their bits out of the sky. Like a painter at an easel, a programmer at a greasy keyboard feels the gravitational pull, the irresistible urge, the hypnotic Siren's song... to use &lt;em&gt;shorthand expressions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Wise programmers often warn young programmers to not adopt this mentality, especially while learning how to code. It can become a bad habit that can in cases lead to harder to read, harder to maintain, and harder to update code. That said, in my explorations of the topic, I found it useful to discover (to my delight) that a) there are proper places to use it, and b) people do use them and you should know how to read them. This blog is about the ternary expression and its tangent cousins -- the AND and the OR operators -- and how they can be used to write succinct yet still straightforward code.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Ternary Expression
&lt;/h1&gt;

&lt;p&gt;Ternary expressions are a shorthand way to write &lt;code&gt;if else&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;Ternary expressions threw me off early on. The semantic structure of &lt;code&gt;if else&lt;/code&gt; statement was easy to understand. If this expression returns truthy, then execute this expression. If it is not truthy, execute this other expression. Why fix something that ain't broke?&lt;/p&gt;

&lt;p&gt;The strange syntax of a &lt;code&gt;?&lt;/code&gt; and the trailing &lt;code&gt;:&lt;/code&gt; were confusing. I think it's because when reading a line of code, the &lt;code&gt;?&lt;/code&gt; and &lt;code&gt;:&lt;/code&gt; can be easily lost in the jumble of text and numbers.&lt;/p&gt;

&lt;p&gt;Here's a breakdown of it, in plain English:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if this is true &lt;code&gt;?&lt;/code&gt; then do this &lt;code&gt;:&lt;/code&gt; otherwise do this&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The usefulness of ternary expressions comes in handy when you want a single-line way to assign a variable a value dependent on another variable. Let's take these three lines that return a &lt;code&gt;isFullyCharged&lt;/code&gt; as &lt;code&gt;true&lt;/code&gt; if our battery level is at 100%.&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;batteryLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&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;isFullyCharged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;batteryLevel&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;100&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="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isFullyCharged&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Written out as an &lt;code&gt;if else&lt;/code&gt; statement, and assuming we still want to return a new boolean variable, it comes out much longer.&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;batteryLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isFullyCharged&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batteryLevel&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;isFullyCharged&lt;/span&gt; &lt;span class="o"&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;isFullyCharged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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="nx"&gt;isFullyCharged&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ternary expressions are useful then for concise single-line checks that are actually quite readable once their at first strange syntax can be overcome.&lt;/p&gt;

&lt;p&gt;And, as I've read more about them, they can also simplify more complex situations, like nested checks, in which case using &lt;a href="https://medium.com/javascript-scene/nested-ternaries-are-great-361bddd0f340"&gt;chained ternary expressions&lt;/a&gt; results in cleaner and less bug prone code.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Logical OR Assignment
&lt;/h1&gt;

&lt;p&gt;The logical OR operator can be used in many ways, but the one I will discuss here is its use as a way to check a value before assignment to a variable.&lt;/p&gt;

&lt;p&gt;The OR operator, denoted as &lt;code&gt;||&lt;/code&gt;, can be used in the primary statement to return a &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;||&lt;/code&gt; operator typically takes two expressions on either side of it. Take this 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;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This short program returns a &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; boolean. The OR operator is executed left to right. First &lt;code&gt;b &amp;gt; a&lt;/code&gt; is checked to be true. In this case, &lt;code&gt;b&lt;/code&gt; is smaller than &lt;code&gt;a&lt;/code&gt;, so the expression returns &lt;code&gt;false&lt;/code&gt;. Upon this return the &lt;code&gt;||&lt;/code&gt; operator then returns the value of the expression on its right. In this case, &lt;code&gt;a &amp;gt; b&lt;/code&gt; returns true, because 5 is a larger number than 1, so as a result the entire expression returns true.&lt;/p&gt;

&lt;p&gt;When it comes to &lt;strong&gt;logical OR assignment&lt;/strong&gt; can be used as a checker to ensure a value is empty before assigning it. For instance, this 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;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;||=&lt;/code&gt; operator only assigns &lt;code&gt;name&lt;/code&gt; to &lt;code&gt;"No name"&lt;/code&gt; if &lt;code&gt;name&lt;/code&gt; is falsy.&lt;/p&gt;

&lt;p&gt;The output of this program will be "No name". The &lt;code&gt;||=&lt;/code&gt; shorthand is a way to check if the value on the left is falsy before assigning it to the value on the right. In this case, &lt;code&gt;name&lt;/code&gt; is declared but undefined, so has the value &lt;code&gt;undefined&lt;/code&gt;. In Javascript, &lt;code&gt;undefined&lt;/code&gt; is considered a falsy value, so as an primitive value it returns &lt;code&gt;false&lt;/code&gt;. Because the left expression is false, the right expression is executed.&lt;/p&gt;

&lt;p&gt;This might become more clear if the &lt;code&gt;name ||= "No name"&lt;/code&gt; expression is pulled apart, into its non-shorthand components. That expression is equivalent to &lt;code&gt;name || (name = "No name")&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;This is worth reiterating, the following two expressions are equivalent, and both assign "No name" to &lt;code&gt;name&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No name&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;This is easier to read, as it more closely resembles the typical OR expression. In essence, this shorthand is a quick way to check if the value of &lt;code&gt;name&lt;/code&gt; is falsy before deciding to assign it a value. This can be useful when you don't want to overwrite a variable or if you don't want to return &lt;code&gt;undefined&lt;/code&gt; and only return a legible string instead.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;||=&lt;/code&gt; is also completely different from assigning a variable to an OR expression. For instance, &lt;code&gt;name = "No name" || "Other name&lt;/code&gt; is valid, and will assign name to &lt;code&gt;"No name"&lt;/code&gt;, but it does so through a different means than the &lt;code&gt;||=&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  The Logical AND Assignment
&lt;/h1&gt;

&lt;p&gt;The logical AND is much like the OR. Instead denoted as &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, it is also an operator that takes in two values, one of the left and one on the right. In this case, both the left &lt;em&gt;and&lt;/em&gt; right values must convert into truthy values for the second value to be returned. Otherwise the first value is returned. This evaluation is executed left to right. If the left value converts to falsy, the right value is never evaluated and the entire AND expression returns false. However if both execute as truthy, then the &lt;em&gt;second&lt;/em&gt; value will be returned.&lt;/p&gt;

&lt;p&gt;So take the 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;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// name is assigned ''&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jill&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// name is assigned "Jill&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first expression, &lt;code&gt;''&lt;/code&gt; is an empty string and considered falsy. The entire &amp;amp;&amp;amp; expression then returns the first value, which is falsely, and short circuits the entire expression. In the second expression, &lt;code&gt;name&lt;/code&gt; is assigned to &lt;code&gt;'Jill'&lt;/code&gt; because both strings &lt;code&gt;'Bob'&lt;/code&gt; and &lt;code&gt;'Jill'&lt;/code&gt; return truthy, and so both are evaluated. Because both evaluate true, the second value is returned.&lt;/p&gt;

&lt;p&gt;Now we go onto the &lt;strong&gt;logical AND assignment&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Exists&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tywen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;lastname&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kelly
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When it comes to the logical AND assignment, denoted by &lt;code&gt;&amp;amp;&amp;amp;=&lt;/code&gt;, the expression only assigns &lt;code&gt;name&lt;/code&gt; to &lt;code&gt;"Tywen"&lt;/code&gt; if name is truthy. &lt;code&gt;name&lt;/code&gt; is actually falsy, as it is undefined, so name is not assigned the value &lt;code&gt;"Tywen"&lt;/code&gt;. &lt;code&gt;lastname&lt;/code&gt; on the other hand is assigned a truthy value of &lt;code&gt;"Exists"&lt;/code&gt;, so when it comes time to assign it with a logical AND assignment, it successfully assigns it to &lt;code&gt;"Kelly"&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React: How to use useState()</title>
      <dc:creator>tywenk</dc:creator>
      <pubDate>Tue, 12 Apr 2022 19:36:39 +0000</pubDate>
      <link>https://forem.com/tywenk/react-how-to-use-usestate-1c4e</link>
      <guid>https://forem.com/tywenk/react-how-to-use-usestate-1c4e</guid>
      <description>&lt;p&gt;&lt;em&gt;In this blog I will be strictly discussing React in the context of using it with functional components and hooks.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is useState?
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is a built-in method of the React library that allows developers to store variables that persist throughout the rendering lifecycle of a componenet. Much like a variable, state can be called and mutated throughout a component. State can also be passed down as a prop to children components. State has a unique property which is that it requires a &lt;em&gt;setter&lt;/em&gt; function to change the state, rather than in Vanilla Javascript where you can reassign a variable declared with &lt;code&gt;let&lt;/code&gt; elsewhere within appropriate scope. This gochya does add a little syntactical weight to &lt;code&gt;useState&lt;/code&gt; but it can be easily managed.&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;useState&lt;/code&gt; you have to import it at the top of your component, like so:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note that if you are using a React version older than version 17, then your syntax for import will look like: &lt;code&gt;import React, { useState } from "react"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To implement &lt;code&gt;useState&lt;/code&gt; you will insert the useState assignment call &lt;em&gt;inside&lt;/em&gt; of your functional component. For example say we wanted to store the &lt;code&gt;health&lt;/code&gt; of a player of our game made in React:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&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="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;Hello, Player&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Inventory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's focus in on the line of code that is setting state.&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is a method that returns an array, or more precisely, a &lt;em&gt;tuple&lt;/em&gt;. A tuple is an array of set length. While Javascript does not officially have tuples, colloquially we can say that useState returns a tuple because it always returns an array with length two.&lt;/p&gt;

&lt;p&gt;The array &lt;code&gt;useState&lt;/code&gt; returns a specific ordering. The first index is reserved for the current state of the variable, and the seconds index is reserved for a setter function that can mutate the value of the In this case, &lt;code&gt;health&lt;/code&gt; references the current value of the player's health. &lt;code&gt;setHealth&lt;/code&gt; references a function which takes as either a) and argument or b) a callback that sets the value of &lt;code&gt;health&lt;/code&gt;. By convention the setter function is prepended with &lt;code&gt;set&lt;/code&gt; although it is not required. Also by convention when one calls &lt;code&gt;useState&lt;/code&gt; they reference it using array destructuring, as we did in our example.&lt;/p&gt;

&lt;p&gt;Finally, the number &lt;code&gt;100&lt;/code&gt; in &lt;code&gt;useState&lt;/code&gt; is used to initialize &lt;code&gt;health&lt;/code&gt; to the value of &lt;code&gt;100&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In short, the syntax for declaring a state variable can be broken down into this more readable psuedo code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [declare the variable name, declare setter function to change variable] = useState(initial value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, how does one set a new state value?&lt;/p&gt;

&lt;p&gt;In most cases it is as easy as &lt;code&gt;setHealth(80)&lt;/code&gt;. This will overwrite the initial value of &lt;code&gt;100&lt;/code&gt; and set it to &lt;code&gt;80&lt;/code&gt;. However is cases where you want to add onto the current value, you must use a callback in the setter. So, to add &lt;code&gt;30&lt;/code&gt; to now current value of health (&lt;code&gt;80&lt;/code&gt;), we use &lt;code&gt;setHealth(health =&amp;gt; health += 30)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Fundamentally this is because the setter function is &lt;em&gt;asynchronous&lt;/em&gt;. By implementing a callback, we refer the the current value of the state. If we don't use a callback then we are blindly setting the value and not waiting to ensure the value was set. This callback structure becomes useful when (or if) you want to chain series of setters together all of which rely on the previous one completing. For example&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="nf"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;health&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;health&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;health&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;health&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;health&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;health&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming &lt;code&gt;health&lt;/code&gt; starts at &lt;code&gt;100&lt;/code&gt; then health will be &lt;code&gt;190&lt;/code&gt; at the end of this operation. If we did not use callbacks, then the value comes out differently, and not as expected.&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="nf"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;setHealth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;em&gt;some&lt;/em&gt; cases this may return &lt;code&gt;130&lt;/code&gt; as the final value. This unexpected behavior is why you should always use callbacks in your setter function to ensure you are getting the most current value of the state you are trying to manipulate.&lt;/p&gt;

&lt;p&gt;If you are coming from Javascript this might seem like a lot of work to declare a variable. Why can't we just &lt;code&gt;const health = 100&lt;/code&gt; and later in the code, to update it, set &lt;code&gt;health = 80&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;React components have a render lifecycle. Each time a component re-renders it will redeclare all of the variables inside of it, essentially cleaning its cache. If we look at this diagram's "Updating" column we can see that &lt;code&gt;setState()&lt;/code&gt; causes a render to occur.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1400%2F1%2AcEWErpe-oY-_S1dOaT1NtA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1400%2F1%2AcEWErpe-oY-_S1dOaT1NtA.jpeg" alt="react render lifecycle"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;a href="https://code.likeagirl.io/understanding-react-component-life-cycle-49bf4b8674de" rel="noopener noreferrer"&gt;Source&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we set a new value to state, the entire component &lt;strong&gt;automatically&lt;/strong&gt; re-renders to update its UI to account for new values. &lt;strong&gt;This is where React gets its name. Setting state tells React to "react" to the changes you give it.&lt;/strong&gt; With React you don't need to impertively (see: manually) tell Javascript: "ok, if this variable changes, then update this part of the UI." React as a library handles that all by itself.&lt;/p&gt;

&lt;p&gt;So, to reiterate, calling a setting function on state variable causes a re-render of the component. There a few caveats here, like if the state doesn't change React is smart enough not to re-render.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is state below the hood?
&lt;/h1&gt;

&lt;p&gt;I'll leave you with this note, which I found interesting, about how state is actually handled behind the scenes by React:&lt;/p&gt;

&lt;p&gt;"State is a plain JavaScript object used by React to represent an information about the component’s current situation. It’s managed in the component (just like any variable declared in a function). The difference is while a “normal” variable “disappears” when their function exits, the state variables are preserved by React." &lt;a href="https://medium.com/edonec/state-in-react-an-overview-a182675cee2c#:~:text=State%20is%20a%20plain%20JavaScript%20object%20used%20by%20React%20to,variable%20declared%20in%20a%20function" rel="noopener noreferrer"&gt;Citation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How Do Hash Tables Work Under the Hood?</title>
      <dc:creator>tywenk</dc:creator>
      <pubDate>Tue, 12 Apr 2022 19:35:23 +0000</pubDate>
      <link>https://forem.com/tywenk/how-do-hash-tables-work-under-the-hood-4nff</link>
      <guid>https://forem.com/tywenk/how-do-hash-tables-work-under-the-hood-4nff</guid>
      <description>&lt;p&gt;Hash tables are a very time efficient way to store data in the form of key value pairs. Hash tables have a O(1) time complexity in accessing data, and almost nearly as fast in inserting (adding) data.&lt;/p&gt;

&lt;p&gt;Hash tables are also called objects in JavaScript, dictionaries in Python, and hashes in Ruby.&lt;/p&gt;




&lt;p&gt;To first understand how hash tables work, first lets understand how arrays work.&lt;/p&gt;

&lt;p&gt;Say you had an array of your extended familial relations. It might looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fam_array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uncle&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;Aunt&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;Cousin&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;Cousin&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;In this situation the keys of each item are automatically indexed as numbers. With the first item starting at 0 each item is assigned a sequential key. In most languages there is a syntax to access an item in the array via index notation. Calling &lt;code&gt;fam_array[0]&lt;/code&gt; would return &lt;code&gt;"Uncle"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This retrieval is very fast and inefficient because behind the scenes the &lt;code&gt;0&lt;/code&gt; is actually a pointer for a specific location in memory. Memory itself can be thought of as its own array. Memory is a linear line of cells each "indexed" with a its own addressing system. This addressing system uses hexadecimal, which is base-16 numerating.&lt;/p&gt;

&lt;p&gt;Let's take a look at this diagram of memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
===============     Highest Address (e.g. 0xFFFF)
|             |
|    STACK    |
|             |
|-------------|  &amp;lt;- Stack Pointer   (e.g. 0xEEEE)
|             |
.     ...     .
|             |
|-------------|  &amp;lt;- Heap Pointer    (e.g. 0x2222)
|             |
|    HEAP     |
|             |
===============     Lowest Address  (e.g. 0x0000)

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

&lt;/div&gt;



&lt;p&gt;As a refresher, the stack is where the program's functions and variables are loaded, looped, and run. The heap is where non-primitive objects and arrays are stored, and are referenced at by the stack.&lt;/p&gt;

&lt;p&gt;In our example, &lt;code&gt;fam_array&lt;/code&gt; is declared in the stack then assigned a pointer to a location in the heap which houses the actual array of &lt;code&gt;["Uncle", "Aunt", "Cousin", "Cousin"]&lt;/code&gt;. When &lt;code&gt;fam_array[0]&lt;/code&gt; is called, the &lt;code&gt;0&lt;/code&gt; acts as a pointer to the string &lt;code&gt;"Uncle"&lt;/code&gt; in the heap with its addressed as some hexadecimal, like &lt;code&gt;0x0f403...23c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This method of using a number &lt;code&gt;0&lt;/code&gt; as a pointer is extremely, extremely time efficient. It is a direct pointer to a spot in memory.&lt;/p&gt;

&lt;p&gt;The idea of hash tables is to bring that access efficiency of arrays to the more human-readable way of storing information in a key-value pair schema.&lt;/p&gt;

&lt;p&gt;Let's transform that array into a hash table, and add the following keys with names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uncle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;mary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aunt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;leo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cousin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;cher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cousin&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;Here each key is a string of a name. The value is that person's familial relation.&lt;/p&gt;

&lt;p&gt;Computers don't really like using strings such as "bob" to store data. "bob" isn't a pointer to a place in memory and can't be used in the same way a number index like &lt;code&gt;0&lt;/code&gt; in the previous example of arrays could be.&lt;/p&gt;

&lt;p&gt;The solution is to transform the key into an numbered index by using a &lt;em&gt;hash function&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A hash function turns keys into unique index&lt;/strong&gt;, as if it were an array. This hashing is of course what gives hash tables its name.&lt;/p&gt;

&lt;p&gt;The key in the key-value pair &lt;code&gt;"bob": "Uncle"&lt;/code&gt; would be run through a hash function, then given an index. What returns a number that is smaller than the length of the hash table. In this case, the hash table is four items long, so the maximum index &lt;code&gt;"bob"&lt;/code&gt; would hash into is 3.&lt;/p&gt;

&lt;p&gt;In psudeocode this would look like these two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; hashed_key = hash(key)&lt;/li&gt;
&lt;li&gt; index = hashed_key % array_size&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.tutorialspoint.com%2Fdata_structures_algorithms%2Fimages%2Fhash_function.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.tutorialspoint.com%2Fdata_structures_algorithms%2Fimages%2Fhash_function.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Colloquially these two steps are referred to the "hash function".&lt;/p&gt;

&lt;p&gt;This hashing process is run every time a hash table is created, accessed, modified. Hashing is a semi-random process that takes an input and outputs a ranom-looking string of a fixed bit length. Hashing algorithms vary, but their main purpose is to generate a unique looking number based on an input. &lt;code&gt;bob&lt;/code&gt; might hash into the hexidecimal &lt;code&gt;0x34acc53&lt;/code&gt;, but &lt;code&gt;bo&lt;/code&gt; which is only one letter different might hash into &lt;code&gt;0x667df3d&lt;/code&gt;. Either way hashing &lt;code&gt;bob&lt;/code&gt; or &lt;code&gt;bo&lt;/code&gt; will always yield the same result.&lt;/p&gt;

&lt;p&gt;So, using the example of the hash &lt;code&gt;fam&lt;/code&gt;, I could access the value of the &lt;code&gt;"bob"&lt;/code&gt; key by writing something like &lt;code&gt;fam[:bob]&lt;/code&gt; or &lt;code&gt;fam.bob&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This would require the language to hash the string or symbol &lt;code&gt;bob&lt;/code&gt; into a number (often represented as hexidecimal), then modulo it (&lt;code&gt;%&lt;/code&gt;) to find its index. When it has the index, then the retrieval process becomes like that of an array: the index is now a pointer to place in memory.&lt;/p&gt;

&lt;p&gt;The hashing algorithm used in hash functions varies depending on the language. Ruby, for instance, has some differences than the general hashing alogorithm described above. For one, Ruby does not &lt;code&gt;% array_size&lt;/code&gt;. Ruby sets a fixed array size of &lt;code&gt;11&lt;/code&gt; by default, and indexed each hash by &lt;code&gt;% 11&lt;/code&gt;. Ruby calls these 11 spots in array "bins".&lt;/p&gt;

&lt;p&gt;There will also be instances where hash functions produce the same index. &lt;code&gt;fam.leo&lt;/code&gt; might hash into &lt;code&gt;index 2&lt;/code&gt;, and &lt;code&gt;fam.bob&lt;/code&gt; might also hash into &lt;code&gt;index 2&lt;/code&gt;. This is called a collision. Every language has different collision handling. Ruby creates linked lists to handle collisions. Since both &lt;code&gt;leo&lt;/code&gt; and &lt;code&gt;bob&lt;/code&gt; are at index 2, both bob and leo will be placed there. This linked list is essentially an array that looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ ["bob", "next is leo"], ["leo", "next is ?"] ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when &lt;code&gt;fam.leo&lt;/code&gt; is called, &lt;code&gt;"leo"&lt;/code&gt; is first hashed into &lt;code&gt;0xff65cc&lt;/code&gt;, then % by 11, which returns index 2. At index two there is a linked list. Now Ruby iterates through this list (this is O(n) time complexity at this point) and looks for matches to the string "leo" using &lt;code&gt;.equal?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When linked lists get longer than 5 items, then it re-runs the hash through a hashing function and makes a larger bin size.&lt;/p&gt;

&lt;p&gt;Linked listing is just one way to handle collisions. There is also linear probing, quadratic probing, and also running &lt;code&gt;% array_size&lt;/code&gt; as a technique to reduce collisions. In practice most languages use a mix of techniques.&lt;/p&gt;

&lt;p&gt;So the conclusion is that if there are lots of collisions in a hash, then in fact the lookup time is not O(1), but closer to O(log n)!&lt;/p&gt;




&lt;p&gt;Sources:&lt;br&gt;
&lt;a href="https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/" rel="noopener noreferrer"&gt;https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/4560720/why-does-the-stack-address-grow-towards-decreasing-memory-addresses" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/4560720/why-does-the-stack-address-grow-towards-decreasing-memory-addresses&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.sitepoint.com/diving-into-how-hashes-work-in-ruby/" rel="noopener noreferrer"&gt;https://www.sitepoint.com/diving-into-how-hashes-work-in-ruby/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Optional Chaining in Javascript</title>
      <dc:creator>tywenk</dc:creator>
      <pubDate>Tue, 12 Apr 2022 19:34:07 +0000</pubDate>
      <link>https://forem.com/tywenk/optional-chaining-in-javascript-2oa2</link>
      <guid>https://forem.com/tywenk/optional-chaining-in-javascript-2oa2</guid>
      <description>&lt;p&gt;What is Optional Chaining in Javascript?&lt;/p&gt;

&lt;p&gt;Optional Chaining is a new feature of the Javascript language introduced in 2020 that allows for in-line checking of nested object properties. It's a very concise and easy-to-write method that also elegantly allows a program to fail. It avoids the dreaded &lt;code&gt;cannot read property of undefined&lt;/code&gt; error message when its tries to access a property of an object that does not exist.&lt;/p&gt;

&lt;p&gt;Optional chaining is also known as safe navigation.&lt;/p&gt;




&lt;p&gt;Given an object, &lt;code&gt;animals&lt;/code&gt;, how would you assign and access a nested value of &lt;code&gt;name&lt;/code&gt; in &lt;code&gt;dog&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;marge&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="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;Without optional chaining you would first check to see if &lt;code&gt;dog&lt;/code&gt; is a property of &lt;code&gt;animals&lt;/code&gt; with a ternary expression:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const name = animals.dog ? animals.dog.name : undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This use of a ternary is a valid but a little too lengthy of a way to check for properties as you drill into a object.&lt;/p&gt;

&lt;p&gt;With optional chaining, this can be done very efficiently:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const name = animals.dog?.name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Optional chaining syntax is using the &lt;code&gt;?&lt;/code&gt; to check if &lt;code&gt;dog&lt;/code&gt; exists as a property of &lt;code&gt;animals&lt;/code&gt;. If it isn't, it will return &lt;code&gt;undefined&lt;/code&gt; and the object chaining &lt;em&gt;short circuits&lt;/em&gt; so that &lt;code&gt;name&lt;/code&gt; is never called. If &lt;code&gt;dog&lt;/code&gt; does exist as a property of &lt;code&gt;animals&lt;/code&gt;, then the program will proceed and attempt to get &lt;code&gt;dog&lt;/code&gt;'s property of &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At this point optional chaining does not care if &lt;code&gt;name&lt;/code&gt; exists as a property -- and in fact there is not a way to optional chain the end of object drilling. This syntax is &lt;strong&gt;incorrect&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const name = animals.dog?.name?.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But this syntax is fine: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const name = animals?.dog&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the following example you can see how elegant it is to sneak in a &lt;code&gt;?&lt;/code&gt; into dot notation chaining, while also ensuring that the program will fail gracefully by returning &lt;code&gt;undefined&lt;/code&gt; instead of throwing an error.&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;dogName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;catName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;duckName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;duck&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dogWeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;weight&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;dogName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;dogName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;catName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;catName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;duckName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;duckName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;dogWeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;dogWeight&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 what will be logged:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;index&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;dogName&lt;/td&gt;
&lt;td&gt;'bob'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;catName&lt;/td&gt;
&lt;td&gt;'marge'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;duckName&lt;/td&gt;
&lt;td&gt;undefined&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dogWeight&lt;/td&gt;
&lt;td&gt;undefined&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Beyond searching properties of an object, optional chaining can also be used to check the truthy-ness of object's expressions, array indexes, and function arguemnts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;obj.val?.prop
obj.val?.[expr]
obj.arr?.[index]
obj.func?.(args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The quirky thing to note is that optional chaining requires both symbols &lt;code&gt;?&lt;/code&gt; and &lt;code&gt;.&lt;/code&gt; even in situations when dot notation is not used, such as in accessing an array with an index.&lt;/p&gt;

&lt;p&gt;Optional chaining (a.k.a. safe navigation) may have slightly different syntax in different languages. In Ruby, instead of &lt;code&gt;?.&lt;/code&gt; the language uses &lt;code&gt;&amp;amp;.&lt;/code&gt; for &lt;a href="https://stackoverflow.com/questions/33735228/why-does-ruby-use-its-own-syntax-for-safe-navigation-operator"&gt;interesting reasons&lt;/a&gt;. In many languages, though, something similar to &lt;code&gt;?&lt;/code&gt; is used.&lt;/p&gt;




&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/how-the-question-mark-works-in-javascript/"&gt;FreeCodeCamp&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Use Nested Routes in React Router 6</title>
      <dc:creator>tywenk</dc:creator>
      <pubDate>Tue, 12 Apr 2022 19:31:59 +0000</pubDate>
      <link>https://forem.com/tywenk/how-to-use-nested-routes-in-react-router-6-4jhd</link>
      <guid>https://forem.com/tywenk/how-to-use-nested-routes-in-react-router-6-4jhd</guid>
      <description>&lt;p&gt;React Router version 6 makes it easy to nest routes. Nested routes enables you to have multiple components render on the same page with route parity. This is useful for app experiences where you want the user to be able to "drill down" into content and not lose their way, such as in forums or blogs.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Installing React Router
&lt;/h2&gt;

&lt;p&gt;To get started install React Router 6 into your React app. In your app's directory open a terminal and input:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install react-router-dom@6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After it's installed, go to the top level component of your app. In my case, I like to leave &lt;code&gt;index.jsx&lt;/code&gt; clean and standard, so I put my routes in &lt;code&gt;App.jsx&lt;/code&gt;, which is the next highest component.&lt;/p&gt;

&lt;p&gt;At the top of &lt;code&gt;App.jsx&lt;/code&gt; import the required components:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { BrowserRouter as Router, Routes, Route } from "react-router-dom"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is standard practice to import &lt;code&gt;BrowserRouter as Router&lt;/code&gt; because &lt;code&gt;BrowserRouter&lt;/code&gt; is too long to type!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Routing
&lt;/h2&gt;

&lt;p&gt;From here you can begin to set up your routes. An empty shell of nested routes looks like this, with the outermost component &lt;code&gt;&amp;lt;Router&amp;gt;&lt;/code&gt; wrapping a &lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt; (plural!) which then wraps multiple &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; (singular!):&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BrowserRouter&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;Routes&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&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;App&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="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="nc"&gt;Routes&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="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="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Routes&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;The &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; component takes in a &lt;code&gt;path&lt;/code&gt; and &lt;code&gt;element&lt;/code&gt; prop. The &lt;code&gt;path&lt;/code&gt; value holds the path of the route. The &lt;code&gt;element&lt;/code&gt; value holds a pointer to a component or page.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Route path='/' element={&amp;lt;Home /&amp;gt;} /&amp;gt;&lt;/code&gt; is a route that points to a &lt;code&gt;Home&lt;/code&gt; component at the base route of &lt;code&gt;https://yourblog.com/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's try and make an Blog app which displays posts in a nested fashion. We want to see a list of post &lt;em&gt;previews&lt;/em&gt; in a &lt;code&gt;&amp;lt;Posts/&amp;gt;&lt;/code&gt; component. Eventually we want to be able to click a post preview to show a new route of the full post content. We also want to click a button in &lt;code&gt;&amp;lt;Posts/&amp;gt;&lt;/code&gt; that will bring up a new post form in the same place as &lt;code&gt;&amp;lt;Post/&amp;gt;&lt;/code&gt; Let's start by adding few routes to &lt;code&gt;App.jsx&lt;/code&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BrowserRouter&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;Routes&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&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;App&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="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="nc"&gt;Routes&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="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="si"&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;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;'about'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;About&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;'posts'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Posts&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;Routes&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;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7dv8v71h207jsgvx1ls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7dv8v71h207jsgvx1ls.png" alt="Image description" width="665" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You will have to import the Home, About, and Posts components at the top of your page for this to work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These separate routes now each point to a different component. These routes are &lt;em&gt;not&lt;/em&gt; yet nested. If we were to visit &lt;code&gt;https://yourblog.com/about&lt;/code&gt; the webpage would render only what is inside the &lt;code&gt;&amp;lt;About /&amp;gt;&lt;/code&gt; component. If we were to visit &lt;code&gt;https://yourblog.com/posts&lt;/code&gt; the webpage would render only what is inside the &lt;code&gt;&amp;lt;Posts /&amp;gt;&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;With the above App we have the following routes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"/"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/about"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that in your &lt;code&gt;path&lt;/code&gt; props we don't write &lt;code&gt;/about&lt;/code&gt; and instead write &lt;code&gt;about&lt;/code&gt;. The forward slash &lt;code&gt;/&lt;/code&gt; in path names is implicit in React Router 6.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Routing
&lt;/h2&gt;

&lt;p&gt;But we want nested routes! It's this easy:&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;App&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="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="nc"&gt;Routes&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="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="si"&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;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;'about'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;About&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;'posts'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Posts&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;'new'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NewPost&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/*A nested route!*/&lt;/span&gt;&lt;span class="si"&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;':postId'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/*A nested route!*/&lt;/span&gt;&lt;span class="si"&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;Routes&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;In between a &lt;code&gt;Route&lt;/code&gt; component you can embed another Route: &lt;code&gt;&amp;lt;Route&amp;gt;{/* Children routes go here*/}&amp;lt;/Route&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now our route structure looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"/"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/about"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts/new"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts/123"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we can learn how these nested routes can pass information to each other and ensure they display in a proper "nested" way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nesting routes with &amp;lt;Outlet/&amp;gt;
&lt;/h2&gt;

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

&lt;p&gt;&lt;code&gt;/posts/new&lt;/code&gt; points to a page to create new posts. This page will likely contain a controlled form to submit a new post.&lt;/p&gt;

&lt;p&gt;When we head to &lt;code&gt;https://yourblog.com/posts/new&lt;/code&gt; we will see both the &lt;code&gt;&amp;lt;Posts /&amp;gt;&lt;/code&gt; component AND the &lt;code&gt;&amp;lt;NewPost /&amp;gt;&lt;/code&gt; component. This is the magic of nesting routes! We are rendering two difference components with one descriptive path on the same page. Super cool!&lt;/p&gt;

&lt;p&gt;At this point, if you're following along, you might not see the &lt;code&gt;&amp;lt;NewPost /&amp;gt;&lt;/code&gt; component appear. This is because we need to tell the parent component, &lt;code&gt;&amp;lt;Posts /&amp;gt;&lt;/code&gt; where to render its children. React Router provides a component called &lt;code&gt;Outlet&lt;/code&gt; that renders a route's child component.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;&amp;lt;Posts /&amp;gt;&lt;/code&gt; component might look like:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Outlet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&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;Posts&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="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;List of posts go here!&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="nc"&gt;Outlet&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;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt; behaves a bit like &lt;code&gt;props.children&lt;/code&gt; in standard React. &lt;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt; is the placeholder location for where the nested children routes will be rendered.&lt;/p&gt;

&lt;p&gt;You might be wondering how to pass props from a parent route to a child route. React Router 6 has a native prop of &lt;code&gt;Outlet&lt;/code&gt; called &lt;code&gt;context&lt;/code&gt;, which behind the scenes is a &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;React context provider&lt;/a&gt;. &lt;code&gt;context&lt;/code&gt; accepts an array of states.&lt;/p&gt;

&lt;p&gt;In order to for a child route to accept the context, the child component must use React Router's provided hook &lt;code&gt;useOutletContext&lt;/code&gt;. This whole getup might look like:&lt;/p&gt;

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

&lt;span class="c1"&gt;// Posts.jsx, the parent&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;Outlet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&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;Posts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCurrentUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="cm"&gt;/*array of post content*/&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="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;List of posts go here!&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="nc"&gt;Outlet&lt;/span&gt; &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&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="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;span class="c1"&gt;//NewPost.jsx, the child&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;useOutletContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&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;NewPost&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useOutletContext&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="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;Welcome &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;, write a new post!&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;form&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;In order for outlet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The parent must import &lt;code&gt;Outlet&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The parent must embed &lt;code&gt;&amp;lt;Outlet/&amp;gt;&lt;/code&gt; with &lt;code&gt;context=[state]&lt;/code&gt; in the render&lt;/li&gt;
&lt;li&gt;The child must import &lt;code&gt;useOutletContext&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The child must destructure the array with &lt;code&gt;useOutletContext()&lt;/code&gt;. Order matters, unlike with props and object destructuring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, on to the next nested route, &lt;code&gt;:postId&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Routes and useParams
&lt;/h2&gt;

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

&lt;p&gt;&lt;code&gt;/posts/123&lt;/code&gt; will display a specific post. Our &lt;code&gt;path&lt;/code&gt; prop for this Route is a parameter, dentote by the prefix &lt;code&gt;":"&lt;/code&gt; in the path:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Route path=':postId' element={&amp;lt;Post /&amp;gt;} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;&amp;lt;Post /&amp;gt;&lt;/code&gt; component we can get the parameter &lt;code&gt;123&lt;/code&gt; by importing useParams.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { useParams } from "react-router-dom"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then in the body of your functional component, call useParams.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let params = useParams()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now within the scope of the component you can access &lt;code&gt;123&lt;/code&gt;, or whatever post id is passed in the route, by called &lt;code&gt;params.postId&lt;/code&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useParams&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useParams&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;&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="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="si"&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;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Placeholder Component with Index
&lt;/h2&gt;

&lt;p&gt;Nesting routes can also go deeper than one level. Let's nest another route in &lt;code&gt;&amp;lt;Post /&amp;gt;&lt;/code&gt; to display comments.&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;App&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="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="nc"&gt;Routes&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="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="si"&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;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;'about'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;About&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;'posts'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Posts&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;'new'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NewPost&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;':postId'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;Route&lt;/span&gt; &lt;span class="na"&gt;index&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PostIndex&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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;'comments'&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Comments&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&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;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="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Routes&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;Now our route structure looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"/"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/about"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts/new"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts/123"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"/posts/123/comments"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As expected, &lt;code&gt;&amp;lt;Comments /&amp;gt;&lt;/code&gt; now renders as a child route of &lt;code&gt;&amp;lt;Post /&amp;gt;&lt;/code&gt;. Of course, now in &lt;code&gt;&amp;lt;Post /&amp;gt;&lt;/code&gt; we have to add an &lt;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt; in order to render child routes. This will look something like:&lt;/p&gt;

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

&lt;p&gt;But wait, we added two routes, why is there no path for &lt;code&gt;&amp;lt;PostIndex /&amp;gt;&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Route index element={&amp;lt;CommentEmpty /&amp;gt;} /&amp;gt;&lt;/code&gt; has no path prop and instead is given an &lt;code&gt;index&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;index&lt;/code&gt; declares this route as the default child route to render in the parent's &lt;code&gt;Outlet&lt;/code&gt; when there is no other child to render. &lt;code&gt;index&lt;/code&gt; is the default placeholder content for an empty path.&lt;/p&gt;

&lt;p&gt;In this case, when there is no &lt;code&gt;/comments&lt;/code&gt; path, the render will look like:&lt;/p&gt;

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

&lt;p&gt;You could put a clean watermark in here for nicer UX, or put some interesting statistics about the Post, it's up to you! But you have the option to have something there and not some buggy-looking white space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Navigating Between Nested Routes
&lt;/h2&gt;

&lt;p&gt;Finally, React Router provides a handy way to navigate up and down nested routes.&lt;/p&gt;

&lt;p&gt;The library provides a hook called &lt;code&gt;useNavigate()&lt;/code&gt; that allows you to develop programatic path changes.&lt;/p&gt;

&lt;p&gt;To use it import it at the top of a component where you want a user to be able to navigate out of.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { useNavigate } from "react-router-dom"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then instantiate it in the body of the component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let navigate = useNavigate()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can use &lt;code&gt;navigate({/*options*/})&lt;/code&gt; to render a different route.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useNavigate&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;navigate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useNavigate&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="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;navigate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Go Back One&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&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;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;navigate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Go Back Two&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&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;Some useful useNavigate() options include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useNavigate("./") to go up a nested path&lt;/li&gt;
&lt;li&gt;useNavigate(-1) to go back in history, as if the user clicked the back button in a browser&lt;/li&gt;
&lt;li&gt;useNavigate("/pathname") to go to a specific path&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  End
&lt;/h2&gt;

&lt;p&gt;Hopefully this helps you develop some cool nested router apps!&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;Helpful blog that goes more in depth:&lt;br&gt;
(&lt;a href="https://ui.dev/react-router-nested-routes" rel="noopener noreferrer"&gt;https://ui.dev/react-router-nested-routes&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;React Router 6 docs:&lt;br&gt;
&lt;a href="https://reactrouter.com/docs/en/v6/api" rel="noopener noreferrer"&gt;https://reactrouter.com/docs/en/v6/api&lt;/a&gt;&lt;/p&gt;

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