<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ react router - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ react router - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 11:15:09 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/react-router/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use React Router to Build Single Page Applications ]]>
                </title>
                <description>
                    <![CDATA[ Single Page Applications (SPAs) have revolutionized web development. They offer a more dynamic and fluid user experience compared to traditional multi-page applications. Traditional web apps require full-page reloads for almost every click the user m... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-react-router-to-build-single-page-applications/</link>
                <guid isPermaLink="false">66ba29324d935175898a7067</guid>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[  Single Page Applications  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Timothy Olanrewaju ]]>
                </dc:creator>
                <pubDate>Thu, 18 Jul 2024 14:42:16 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/Using-React-Router-to-build-SPAs--Twitter-Post-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Single Page Applications (SPAs) have revolutionized web development. They offer a more dynamic and fluid user experience compared to traditional multi-page applications.</p>
<p>Traditional web apps require full-page reloads for almost every click the user makes. SPAs, on the other hand, load a single HTML page and update the page contents dynamically as users interact with the application. This dynamism mimics the feel of desktop applications and results in better responsive interactions.</p>
<h3 id="heading-before-moving-any-further">Before moving any further:</h3>
<p>To follow along with what I'll discuss in this article, you should have some basic knowledge of React and how to set up a React project. If you already do, let's crack on.</p>
<h2 id="heading-what-are-react-router-and-react-router-dom">What are React Router and React Router DOM?</h2>
<p>React router is a powerful library that manages navigation and routing in React applications. The React Router DOM is used specifically for web applications and has few DOM-specific APIs.</p>
<p>As we dive into the world of React Router DOM, we'll explore its core concepts while demonstrating their implementation in a React application. Our focus will be on building a simple navigation system with links to different components, illustrating how to configure routes, handle route matching, and implement navigation. </p>
<p>By the end of this article, you'll have a solid understanding of how to use React Router DOM to create dynamic and seamless navigation experiences in your single-page applications.</p>
<h2 id="heading-how-to-install-react-router">How to Install React Router</h2>
<p>As I explained above, React-router-DOM is used exclusively for integrating routing functionalities into web applications. So, in order to use it in your React app, you need to install the <code>react-router-dom</code> package by running this command in your React app terminal:</p>
<pre><code class="lang-react">npm install react-router-dom
</code></pre>
<p>After successfully installing it, you can now start routing in your React project. </p>
<h2 id="heading-core-concepts-in-react-router-dom">Core Concepts in React Router DOM</h2>
<h3 id="heading-browserrouter">BrowserRouter</h3>
<p>BrowserRouter a parent component that houses all the route components. All routes that you use in an application must be declared within the <code>BrowserRouter</code>. Most importantly, it stores the current location in the browser's address bar using URLs which comes in handy during navigation.</p>
<p>To use the BrowserRouter, you'll need to import it from the <code>react-router-dom</code> in your App.jsx file.</p>
<pre><code class="lang-react">import { BrowserRouter } from "react-router-dom";

function App() {

  return (
    &lt;BrowserRouter&gt;

    &lt;/BrowserRouter&gt;
  );
}

export default App;
</code></pre>
<p>The <code>BrowserRouter</code> has a <code>basename</code> attribute used to set base URL for all routes in an application. It's important if your app is hosted in a subdirectory on a domain. </p>
<pre><code>&lt;BrowserRouter basename=<span class="hljs-string">"/shop"</span>&gt;

&lt;/BrowserRouter&gt;
</code></pre><p>Adding <code>/shop</code> as a basename will make sure that all route paths are relative to <code>/shop</code>. </p>
<h3 id="heading-routes">Routes</h3>
<p>This component is a direct replacement for <code>switch</code> which was used in the former versions of React Router. It also acts like a parent and renders the first matching child route, which ensures that the correct component is displayed based on the current URL.</p>
<p>To declare routes, import <code>routes</code> from <code>react-router-dom</code> and position it within the <code>BrowserRouter</code> component.</p>
<pre><code class="lang-react">import { BrowserRouter, routes } from "react-router-dom";

function App() {

  return (
    &lt;BrowserRouter&gt;
        &lt;Routes&gt;

        &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
  );
}

export default App;
</code></pre>
<h3 id="heading-route">Route</h3>
<p><code>Route</code> a child component that consists of two attributes: <strong>path</strong> and <strong>element</strong>. A <strong>path</strong> can be any specified path name while the <strong>element</strong> attribute is the component that should be rendered. A route renders a specific component when the path specified matches a URL.</p>
<p>An application can have as many <code>route</code>s as it needs, and they must all be declared inside the <code>Routes</code> component. Assuming we have a <code>&lt;Home\&gt;</code> and <code>&lt;Pricing\&gt;</code> component, we will have to import the <code>Route</code> component and position it within the <code>Routes</code>.</p>
<pre><code class="lang-react">import { BrowserRouter, Routes, Route } from "react-router-dom";

//ALL COMPONENTS IMPORTS COMES HERE

function App() {

  return (
    &lt;BrowserRouter&gt;
        &lt;Routes&gt;
            &lt;Route path="/" element={&lt;Home/&gt;}/&gt;
                &lt;Route path="pricing" element={&lt;Pricing/&gt;}/&gt;
        &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
  );
}

export default App;
</code></pre>
<h3 id="heading-undeclared-routes">Undeclared Routes</h3>
<p>There is a way to handle routes that do not exist in your application, just like an Error 404 page. To do this, create another component bearing a Not Found message and the <code>route</code> added.</p>
<p>Set the path name to <code>*</code> and pass the component as the <strong>element.</strong></p>
<pre><code><span class="hljs-keyword">import</span> { BrowserRouter, Routes, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-comment">//ALL COMPONENTS IMPORTS COMES HERE</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"pricing"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Pricing</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"*"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">PageNotFound</span>/&gt;</span>}/&gt;
        <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><h3 id="heading-nested-routes">Nested Routes</h3>
<p>In some cases, routes can have children or sub-routes.</p>
<pre><code><span class="hljs-keyword">import</span> { BrowserRouter, Routes, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-comment">//ALL COMPONENTS IMPORTS COMES HERE</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"pricing"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Pricing</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"categories"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Categories</span>/&gt;</span>}&gt;
                    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"male"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Male</span>/&gt;</span>}/&gt;
                        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"female"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Female</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"*"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">PageNotFound</span>/&gt;</span>}/&gt;
        <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><p>On navigating to the nested elements, the URL on the browser will be reading something like <code>/categories/male</code> and <code>/categories/female</code>.</p>
<h3 id="heading-link">Link</h3>
<p>This acts like an anchor <code>href</code> attribute. It has a <strong>to</strong> attribute which specifies where the <code>Link</code> will take the user after a click. Usually, it is the path to a component's page that is passed to the <strong>to</strong> attribute. </p>
<p>Links are typically put in a Navbar component, so we will put two Links that point to the components path in our already declared Routes.</p>
<pre><code class="lang-react">import { Link } from "react-router-dom";
export default function PageNav() {
  return (
  &lt;&gt;
        &lt;Link to="/"&gt;Home&lt;/Link&gt;
        &lt;Link to="pricing"&gt;Pricing&lt;/Link&gt;
  &lt;/&gt;
  );
}
</code></pre>
<p><strong>NB:</strong> If you are practicing along while reading this article, it is important to note that the <code>PageNav</code> component created here should be situated in your <strong>App.jsx</strong> and specifically just after the opening <code>BrowserRouter</code> tag before the Routes. This is to ensure the <code>PageNav</code> always stays at the top like a navigation menu despite routing through different components.</p>
<pre><code><span class="hljs-keyword">import</span> { BrowserRouter, Routes, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-comment">//ALL COMPONENTS IMPORTS COMES HERE</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">PageNav</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"pricing"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Pricing</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"categories"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Categories</span>/&gt;</span>}&gt;
                    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"male"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Male</span>/&gt;</span>}/&gt;
                        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"female"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Female</span>/&gt;</span>}/&gt;
                <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"*"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">PageNotFound</span>/&gt;</span>}/&gt;
        <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><h3 id="heading-navlink">NavLink</h3>
<p>NavLink performs the same function as <code>Link</code> and has a <strong>to</strong> attribute as well. But it's different as it has a class attribute. The class attributes are <code>active</code>, <code>isPending</code>, and <code>isTransitioning</code>. This makes it more versatile than <code>Link</code> and you can use it to conditionally add styles during user interactions.</p>
<pre><code><span class="hljs-keyword">import</span> { NavLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">PageNav</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"pricing"</span>&gt;</span>Pricing<span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
  }
</code></pre><h3 id="heading-outlet">Outlet</h3>
<p>Having child elements inside a parent route element means there is a layer of abstraction in rendering the child routes' UI. This is where the <code>Outlet</code> component comes into play. You add it to the parent route – in our example, it would be the <code>Categories</code> component.</p>
<pre><code class="lang-react">import { NavLink, Outlet } from "react-router-dom";
export default function Categories() {
  return (
 &lt;&gt;
          &lt;NavLink to="men"&gt;
            Men
          &lt;/NavLink&gt;
          &lt;NavLink to="women"&gt;
            Women
          &lt;/NavLink&gt;
      &lt;Outlet /&gt;
&lt;/&gt;
  );
}
</code></pre>
<p>This allows for the rendering of the child routes UI in the nested route.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/nested-routes.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>Image showing children nested routes in Categories route</em></p>
<h3 id="heading-usenavigate-hook"><code>useNavigate</code> Hook</h3>
<p>This hook returns a function that enables programmatic navigation between routes. </p>
<p>There are several ways to use the navigate function in your application. First, we need to import the <code>useNavigate</code> hook and initialize it as <strong>navigate</strong>.</p>
<pre><code><span class="hljs-keyword">import</span> { useNavigate } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Homepage</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> navigate = useNavigate();

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is the Homepage<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>


    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre><p>We can use navigate in the following ways in our application:</p>
<ul>
<li>Attaching it to a button via the <code>onClick</code> prop with the intended path to be navigated to, passed to the navigate function.</li>
</ul>
<pre><code class="lang-react"> &lt;button onClick={() =&gt; navigate("/categories")}&gt;Go to Categories&lt;/button&gt;
</code></pre>
<ul>
<li>Using it with a <code>Link</code> component.</li>
</ul>
<pre><code class="lang-react">&lt;Link to={navigate("/categories")}&gt;Go to Categories&lt;/Link&gt;
</code></pre>
<ul>
<li>Using a number instead of the component path in the <strong>navigate</strong> function. The number should specify the number of navigations backward in the history stack where you would like to go.</li>
</ul>
<pre><code class="lang-react">&lt;Link to={navigate(-1)}&gt;Go one step backwards&lt;/Link&gt;
</code></pre>
<h3 id="heading-useparams-hook"><code>useParams</code> Hook</h3>
<p>Returns an object of the dynamic <code>params</code> gotten from the current URL matched by the Route's path. The parent routes pass all <code>params</code> to their child routes. </p>
<p>The example below shows an <code>OrderPage</code> component that will be rendered for every <code>customer</code> with their unique <code>id</code>. When the URL matches <code>/customer/123</code>, <code>:id</code> will be <code>123</code>.</p>
<pre><code class="lang-react">import { useParams } from "react-router-dom";

function App() {
  const {id} = useParams()
  return (
    &lt;BrowserRouter&gt;
        &lt;Routes&gt;
        &lt;Route path="customer"&gt;
            &lt;Route path=":id" element={&lt;OrderPage/&gt;}/&gt;
           &lt;/Route&gt;
        &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
  );
}

export default App;
</code></pre>
<h3 id="heading-final-result">Final Result</h3>
<p>At this point, we've fully implemented React Router in our small navigation project. This is what it looks like in full flow:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Untitled-design.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>For more detailed information and concepts about React router, you can visit the <a target="_blank" href="https://reactrouter.com/">Official React Router documentation</a> site.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, we've explored the concepts and implementation of Client Side Routing (CSR) in a React web application. React Router, through its web-centric library React-Router-DOM, enables CSR, allowing apps to update the URL with a click of a link without making a server request for a new document.</p>
<p>This functionality enhances the user experience by providing faster navigation and a more seamless interaction within the application. By leveraging CSR, developers can build more efficient and responsive single-page applications (SPAs), ultimately improving performance and user satisfaction.</p>
<p>If you enjoyed reading this article, you could <a target="_blank" href="https://buymeacoffee.com/timothyolanrewaju">Buy me a Coffee</a>.</p>
<p>Want to see more of these? Connect with me on <a target="_blank" href="https://www.linkedin.com/in/timothy-olanrewaju750/">LinkedIn</a>.</p>
<p>See you on the next one!</p>
<p>Happy Coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create Multi-Page Animations Using Framer Motion & React-Router-Dom ]]>
                </title>
                <description>
                    <![CDATA[ Animations are what make plain websites turn into exciting and unforgettable experiences. They give your website a bit of personality and uniqueness and leave the visitor admiring the overall aesthetic. It's a no-brainer that humans love beautiful th... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-multi-page-animations-using-framer-motion-and-react-router-dom/</link>
                <guid isPermaLink="false">66d4608c230dff016690584f</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Okosa Leonard ]]>
                </dc:creator>
                <pubDate>Mon, 17 Jun 2024 05:57:47 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/06/Green-Abstract-Wavy-Background-Motivational-Quote-Facebook-Post.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Animations are what make plain websites turn into exciting and unforgettable experiences. They give your website a bit of personality and uniqueness and leave the visitor admiring the overall aesthetic.</p>
<p>It's a no-brainer that humans love beautiful things. We all love products that look easy on the eyes.</p>
<p>In this article, we're going to learn how to create animations that wow users with the use of Framer motion and React-Router-Dom.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To be able to follow what we're doing in this article, you should have some knowledge of <a target="_blank" href="https://react.dev/">React</a>, <a target="_blank" href="https://www.framer.com/motion/">Framer motion</a> and <a target="_blank" href="https://reactrouter.com/">React-Router-DOM</a>.</p>
<p>To learn Framer motion better, you can study their documentation.</p>
<p><a target="_blank" href="https://nodejs.org/en/download/package-manager">Node.js</a> should also be installed on your system, and you should have a working code editor. I'll be using <a target="_blank" href="https://code.visualstudio.com/">VS Code</a>.</p>
<h2 id="heading-how-to-set-up-the-project">How to Set Up the Project</h2>
<p>To set up our project we're going to use Vite to set up our React development environment.</p>
<ol>
<li><p>Open the terminal in VScode. You can use Ctrl + backtick(`)</p>
</li>
<li><p>In your terminal, enter the following command:</p>
</li>
</ol>
<pre><code class="lang-plaintext">npm create vite@latest
</code></pre>
<ol start="3">
<li><p>Follow the prompts to name your project and choose your desired framework. In our case, we're using React. This will be a JavaScript project.</p>
</li>
<li><p>Go to your project directory and use <code>npm i</code> in the terminal.</p>
</li>
<li><p>To start your project use <code>npm run dev</code>.</p>
</li>
<li><p>Remember to clean your project by removing the code in App.js and your CSS files in the <code>src</code> folder.</p>
</li>
</ol>
<h2 id="heading-how-to-initialize-framer-motion-and-react-router-dom">How to Initialize Framer Motion and React-Router-Dom</h2>
<ol>
<li>To install Framer-motion in your project open the terminal and enter:</li>
</ol>
<pre><code class="lang-plaintext">npm i framer-motion
</code></pre>
<ol start="2">
<li>To install React-Router-DOM in your project open the terminal and enter:</li>
</ol>
<pre><code class="lang-plaintext">npm i react-router-dom
</code></pre>
<h2 id="heading-how-to-set-up-components-and-basic-routing-with-react-router-dom">How to Set Up Components and Basic Routing with React-Router-DOM</h2>
<p>Let's set up our components and the pages we'll be routing to for this project.</p>
<ol>
<li><p>In <code>src</code>, create a new folder named <code>components</code>.</p>
</li>
<li><p>We'll add four files in this folder named <code>Home.jsx</code>, <code>About.jsx</code>, <code>Projects.jsx</code> and <code>Navbar.jsx</code>.</p>
</li>
<li><p>Inside the first three, we're going to create a React functional component. Change the content of the <code>h1</code> tag in each component:</p>
</li>
</ol>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> {
 <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
 )
 }

 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Home
</code></pre>
<ol start="4">
<li>In the Navbar, we need to import <code>Link</code> from React-Router-DOM to create anchor elements. We then need to create a container housing our logo and nav links. The logo will link the points to our index page.</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>

<span class="hljs-keyword">const</span> Navbar () =&gt; {
 <span class="hljs-keyword">return</span> (
     <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"logo"</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Lennythedev<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-links"</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-item"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
           <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-item"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
           <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-item"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Projects<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
           <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
 }
</code></pre>
<ol start="5">
<li>Now let's go to our <code>index.js</code> or <code>main.js</code> file. The goal is to wrap our entire app with <code>BrowserRouter</code> which will enable routing within our application.</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./App.jsx'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>
<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Routes, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/*'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">App</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<ol start="6">
<li>Now in <code>App.js</code>, we're going to complete the final step of our configuration. We'll import our components, and some features from React-Router-DOM and render our components. By using <code>useLocation</code> feature from React-Router-DOM, we can set the current location of the routes by setting the key to the current path.</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>
<span class="hljs-keyword">import</span> { Routes, Route, useLocation } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>
<span class="hljs-keyword">import</span> NavBar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/NavBar'</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Home'</span>;
<span class="hljs-keyword">import</span> Projects <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Projects'</span>;
<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/About'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> location = useLocation();
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">NavBar</span> /&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">AnimatePresence</span> <span class="hljs-attr">mode</span>=<span class="hljs-string">'wait'</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{location}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{location.pathname}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">index</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>} /&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/projects'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Projects</span> /&gt;</span>}/&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/about'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">About</span> /&gt;</span>}/&gt;
       <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
       <span class="hljs-tag">&lt;/<span class="hljs-name">AnimatePresence</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<ol start="7">
<li>Now we can add our styling in <code>App.css</code>:</li>
</ol>
<pre><code class="lang-css">* {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Fira Sans Condensed"</span>, sans-serif;
}

<span class="hljs-selector-tag">html</span>,
<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Fira Sans Condensed"</span>, sans-serif;
  <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">0</span>, <span class="hljs-number">162</span>, <span class="hljs-number">255</span>);
}

<span class="hljs-selector-class">.nav</span> {
  <span class="hljs-attribute">position</span>: fixed;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: space-between;
}

<span class="hljs-selector-class">.nav-links</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">cursor</span>: pointer;
}

<span class="hljs-selector-class">.logo</span>, <span class="hljs-selector-class">.nav-item</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">2em</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">400</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.5vw</span>;
}

<span class="hljs-selector-tag">h1</span>{
  <span class="hljs-attribute">width</span>: <span class="hljs-number">80%</span>;
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translate</span>(-<span class="hljs-number">50%</span>, -<span class="hljs-number">50%</span>);
  <span class="hljs-attribute">text-align</span>: center;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">500</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">10vw</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1</span>;
  <span class="hljs-attribute">text-transform</span>: uppercase;
}

<span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">text-decoration</span>: none;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">500</span>;
}
</code></pre>
<ol start="8">
<li>After following all the steps your app should look like this:</li>
</ol>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/Annotation-2024-06-14-200041.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>styled webpage without animation</em></p>
<h2 id="heading-how-to-create-transitions-using-framer-motion">How to Create Transitions Using Framer Motion</h2>
<p>Finally let's create our animation for transitions between pages.</p>
<ol>
<li><p>Create a file in components named <code>Box.jsx</code> and <code>import motion from framer-motion</code>.</p>
</li>
<li><p>We can then return two divs, with <code>classNames</code> of <code>slide-in</code> and <code>slide-out</code> one for sliding in and another for sliding out.</p>
</li>
<li><p>We insert our animation in these divs with the help of framer-motion:</p>
</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { motion } <span class="hljs-keyword">from</span> <span class="hljs-string">"framer-motion"</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Box</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span>(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">motion.div</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">"slide-in"</span>
        <span class="hljs-attr">initial</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">scaleY:</span> <span class="hljs-attr">0</span> }}
        <span class="hljs-attr">animate</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">scaleY:</span> <span class="hljs-attr">0</span> }}
        <span class="hljs-attr">exit</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">scaleY:</span> <span class="hljs-attr">1</span> }}
        <span class="hljs-attr">transition</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">duration:</span> <span class="hljs-attr">1</span>, <span class="hljs-attr">ease:</span> [<span class="hljs-attr">0.22</span>, <span class="hljs-attr">1</span>, <span class="hljs-attr">0.36</span>, <span class="hljs-attr">1</span>] }}
     /&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">motion.div</span>
     <span class="hljs-attr">className</span>=<span class="hljs-string">"slide-out"</span>
        <span class="hljs-attr">initial</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">scaleY:</span> <span class="hljs-attr">1</span> }}
        <span class="hljs-attr">animate</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">scaleY:</span> <span class="hljs-attr">0</span> }}
        <span class="hljs-attr">exit</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">scaleY:</span> <span class="hljs-attr">0</span> }}
        <span class="hljs-attr">transition</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">duration:</span> <span class="hljs-attr">1</span>, <span class="hljs-attr">ease:</span> [<span class="hljs-attr">0.22</span>, <span class="hljs-attr">1</span>, <span class="hljs-attr">0.36</span>, <span class="hljs-attr">1</span>] }}
     /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<ol start="4">
<li>Next, we add our styling in our CSS file for <code>slide-in</code> and <code>slide-out</code> in our App.css</li>
</ol>
<pre><code class="lang-css"><span class="hljs-selector-class">.slide-in</span> {
  <span class="hljs-attribute">position</span>: fixed;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">background</span>: <span class="hljs-number">#0f0f0f</span>;
  <span class="hljs-attribute">transform-origin</span>: bottom;
}

<span class="hljs-selector-class">.slide-out</span> {
  <span class="hljs-attribute">position</span>: fixed;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">background</span>: <span class="hljs-number">#0f0f0f</span>;
  <span class="hljs-attribute">transform-origin</span>: top;
}
</code></pre>
<ol start="5">
<li>Finally, the last step is to use <code>AnimatePresence</code> from framer-motion in our <code>App.js</code> file and wrap the entire App in <code>AnimatePresence</code> and set the mode as wait. If you want to learn more about a <code>AnimatePresence</code> visit the framer-motion docs.</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>
<span class="hljs-keyword">import</span> { Routes, Route, useLocation } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>
<span class="hljs-keyword">import</span> NavBar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/NavBar'</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Home'</span>;
<span class="hljs-keyword">import</span> Projects <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Projects'</span>;
<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/About'</span>;
<span class="hljs-keyword">import</span> { AnimatePresence } <span class="hljs-keyword">from</span> <span class="hljs-string">'framer-motion'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> location = useLocation();
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">NavBar</span> /&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">AnimatePresence</span> <span class="hljs-attr">mode</span>=<span class="hljs-string">'wait'</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span> <span class="hljs-attr">location</span>=<span class="hljs-string">{location}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{location.pathname}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">index</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>} /&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/projects'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Projects</span> /&gt;</span>}/&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/about'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">About</span> /&gt;</span>}/&gt;
       <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
       <span class="hljs-tag">&lt;/<span class="hljs-name">AnimatePresence</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<ol start="6">
<li>Finally, our work should look the same as the video below:</li>
</ol>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/Sb1txpdycpA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Creating multi-page animations with Framer Motion and React-Router-Dom enhances user experience by providing smooth transitions.</p>
<p>This integration leverages the power of Framer Motion's animation capabilities with React-Router-Dom's routing features, resulting in dynamic, interactive web applications.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Implement Dynamic Segments with useParams in React Router ]]>
                </title>
                <description>
                    <![CDATA[ On a traditional website, when a user clicks on a URL, the browser makes a full-page request from the server and directs the user to a new page. This is referred to as static routing. This is very useful if you just need to navigate the user to a new... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-dynamic-segments-in-react-router/</link>
                <guid isPermaLink="false">66c5f6921bceda0ea5c39781</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Alex Anie ]]>
                </dc:creator>
                <pubDate>Wed, 31 Jan 2024 22:34:08 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/Dynamic-Segment-in-react-router-800x418.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>On a traditional website, when a user clicks on a URL, the browser makes a full-page request from the server and directs the user to a new page. This is referred to as static routing.</p>
<p>This is very useful if you just need to navigate the user to a new page. But with the development of web apps, there's more need for pages to render on the client or load dynamically. </p>
<p>This involves updating particular parts of the URL called segments, as well as rendering new content or updating the content on the same page without making a request from the server or reloading the entire page. </p>
<p>This is very common and useful in modern web apps. It enables client-side rendering, improves website navigation, and enables smooth transitions and animations (since the browser doesn't need to reload the page from external servers).</p>
<p>This can overall improve website performance and enable a good user experience.</p>
<p>In this tutorial, you'll learn about Dynamic Segments in React Router.</p>
<p>We'll go over what Dynamic Routing is and how it’s different from Static Routing. We'll also cover how to use <em>useParams</em> to enable Dynamic Segments, and how to set your path when getting data from an API.</p>
<p>Lastly, we’ll build a new project that dynamically renders new content to the same page when the user clicks on the sidebar.</p>
<p>By the end of this guide, you should be able to implement dynamic segments on your own in your React app.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></li>
<li><a class="post-section-overview" href="#heading-project-setup">Project Setup</a></li>
<li><a class="post-section-overview" href="#heading-installations">Installations</a><ul>
<li><a class="post-section-overview" href="#heading-install-react">Install React</a></li>
<li><a class="post-section-overview" href="#heading-install-react-router">Install React Router</a></li>
<li><a class="post-section-overview" href="#heading-install-feather-icon">Install Feather Icon</a></li>
<li><a class="post-section-overview" href="#heading-install-tailwind-css">Install Tailwind CSS</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-client-side-routing">Client-Side Routing</a></li>
<li><a class="post-section-overview" href="#heading-dynamic-segments">Dynamic Segments</a></li>
<li><a class="post-section-overview" href="#heading-nested-routing">Nested Routing</a></li>
<li><a class="post-section-overview" href="#heading-project-build-an-art-gallery">Project: Build an Art Gallery</a><ul>
<li><a class="post-section-overview" href="#heading-project-overview">Project Overview</a></li>
<li><a class="post-section-overview" href="#heading-folder-structure">Folder Structure</a></li>
<li><a class="post-section-overview" href="#heading-how-to-set-up-the-home-page">How to Set Up the Home Page</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-and-style-the-navbar">How to Create and Style the Navbar</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-the-asidebar">How to Create the AsideBar</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-the-asidebar">How to Create the Content Component</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-summary">Summary</a></li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To follow along, you'll need a basic knowledge of the following:</p>
<ul>
<li>React</li>
<li>React-Router</li>
<li>Tailwind CSS (optional)</li>
</ul>
<h2 id="heading-project-setup">Project Setup</h2>
<p>To get started, create a folder called <code>dynamic-segment</code> and open it in VS Code (or your code editor of choice):</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled.png" alt="VS code screeshot" width="600" height="400" loading="lazy"></p>
<p>Next, click on <code>Ctrl +</code> (backtick) to launch the terminal as indicated above. This will enable us to install the npm packages we’ll be using in this project. </p>
<h2 id="heading-installations">Installations</h2>
<p>Now that we have our project set up, let’s install the <code>npm</code> packages that we need to get our project up and running.</p>
<h3 id="heading-install-reacthttpsreactdev">Install <a target="_blank" href="https://react.dev/">React</a></h3>
<p>React is a JavaScript library for building reusable and interactive components. To install it, copy and paste the command provided by <a target="_blank" href="https://vitejs.dev/">vite.js</a> below. </p>
<pre><code class="lang-bash">npm create vite@latest
</code></pre>
<p>Then just follow the installing guide to finish the process. Once the installation is complete, the node_modules folder should be present in your project folder.</p>
<h3 id="heading-install-react-routerhttpsreactroutercomenmain">Install <a target="_blank" href="https://reactrouter.com/en/main">React Router</a></h3>
<p>This is a React routing library for creating client-side routing applications. To install it, copy and paste the command below and press enter.</p>
<pre><code class="lang-bash">npm i react-router-dom
</code></pre>
<h3 id="heading-install-feather-iconhttpsfeathericonsdev">Install <a target="_blank" href="https://feathericons.dev/">Feather Icon</a></h3>
<p>Feather icon is a small yet beautiful collection of 24 x 24 grid open-source icons. It's built for adding flat icons on web applications. </p>
<p>To install it, paste the command below and press enter. </p>
<pre><code class="lang-bash">npm i react-feather
</code></pre>
<h3 id="heading-install-tailwind-csshttpstailwindcsscom">Install <a target="_blank" href="https://tailwindcss.com/">Tailwind CSS</a></h3>
<p>Tailwind is a utility-first CSS framework for building beautiful and compact website designs. To install it, run the command on the terminal below. </p>
<pre><code class="lang-bash">npm install -D tailwindcss postcss autoprefixer
</code></pre>
<p>This will create a <code>tailwind.config.js</code> file. Next, generate your <code>postcss.config.js</code> files with the command below:</p>
<pre><code class="lang-bash">npx tailwindcss init -p
</code></pre>
<p>Next, configure your template paths and add the paths to all of your template files in your <code>tailwind.config.js</code> file. Then click <code>ctrl + s</code> to save.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">/** <span class="hljs-doctag">@type <span class="hljs-type">{import('tailwindcss').Config}</span> </span>*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  <span class="hljs-attr">content</span>: [
    <span class="hljs-string">"./index.html"</span>,
    <span class="hljs-string">"./src/**/*.{js,ts,jsx,tsx}"</span>,
  ],
  <span class="hljs-attr">theme</span>: {
    <span class="hljs-attr">extend</span>: {},
  },
  <span class="hljs-attr">plugins</span>: [],
}
</code></pre>
<p>Next, delete all CSS styles in the <code>./src/index.css</code> file and add the Tailwind directives <code>@tailwind</code> for each of Tailwind’s layers.</p>
<pre><code class="lang-css"><span class="hljs-keyword">@tailwind</span> base;
<span class="hljs-keyword">@tailwind</span> components;
<span class="hljs-keyword">@tailwind</span> utilities;
</code></pre>
<p>Next, delete the <code>assets</code> folder, the <code>App.css</code> and <code>App.jsx</code> files from the <code>/src</code> folder. Once you’ve done that, configure the <code>main.jsx</code> files as the route component as indicated below:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;

<span class="hljs-keyword">import</span> {
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'text-blue-700'</span>&gt;</span>Hello, world<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>Next, run the below command in the terminal to launch your app:</p>
<pre><code class="lang-jsx">npm run dev
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled-1.png" alt="Hello, World in React" width="600" height="400" loading="lazy"></p>
<h2 id="heading-client-side-routing">Client-Side Routing</h2>
<p>In React Router, navigation is relative between the <code>path</code> attribute and the <code>to</code> property. When a user clicks using the <code>&lt;Link&gt;</code> component (<code>&lt;a&gt;</code> tag), it navigates to the specified <code>path</code> within the route component and renders the component when it matches.</p>
<p>This type of navigation is called client-side routing because we are not rendering the pages from the server, but rather navigating from one component to another within the app.</p>
<p>The example below explains how client siding routing works:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//main.jsx</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;
nt
<span class="hljs-keyword">import</span> 
{
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
<span class="hljs-keyword">import</span> Book <span class="hljs-keyword">from</span> <span class="hljs-string">'./book'</span>;
<span class="hljs-keyword">import</span> Bookshop <span class="hljs-keyword">from</span> <span class="hljs-string">'./bookshop'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>
     👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
     👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>From the code example above, we imported two components, <code>Book</code> and <code>Bookshop</code>, and link them via the <code>&lt;a&gt;</code> tag and the <code>Route</code> component on the <code>./src/main.jsx</code> component.</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//book.jsx</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Book</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"px-4"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>77 Ways get to more customers By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Ubuy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Authenticity By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Emanuel Rose<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span> Change Your thinking change your life By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Brian Tracy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>

            👉 <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"bookshop"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 inline-block px-4 underline"</span>&gt;</span>see bookshop<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                {/* <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"publisher/itemId"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 underline"</span>&gt;</span>Publisher<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span> */}
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>The <code>href</code> attribute accepts the <code>bookshop</code> component as a relative path. So clicking the link should navigate you to the <code>bookshop</code> component.</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//bookshop.jsx</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Bookshop</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"px-4"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>list of book shops<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Book Shop &amp; Stationery<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Simon books<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Dynamic Book home<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>

  👉 <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 inline-block px-4 underline"</span>&gt;</span>Names of Books<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<p>The <code>href=”/”</code> in the bookshop component specifies the index route and should navigate you back to the home components.</p>
<p>Your app should look like this on your browser – click to navigate to the bookshop component.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Browser_output.gif" alt="Client-Side Routing in React" width="600" height="400" loading="lazy"></p>
<p>From the browser output above, you'll notice how the URL is updated from the address bar and a new component is rendered. </p>
<p>The type of routing is called client-side routing and only updates the URL path once with the new route. </p>
<p>In the next section, I'll explain how you can update a particular segment and render content dynamically. </p>
<h2 id="heading-dynamic-segments">Dynamic Segments</h2>
<p>A Dynamic Segment, as the name suggests, is a way of rendering a new component (UI) by updating a particular segment in the URL called params. You use the <a target="_blank" href="https://reactrouter.com/en/main/hooks/use-params">useParams</a> hook from <code>react-router-dom</code> to do this. </p>
<p>This is very useful in situations where content needs to render dynamically from a particular component or third-party API.</p>
<p>Continuing from where we stopped in the code, go to the <code>./src/main.jsx</code> component. Edit the Route and add <code>:itemId</code> to the path as indicated below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//main.jsx</span>

&lt;Route&gt;
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>} /&gt;</span>
 👉  &lt;Route path=<span class="hljs-string">'publisher/:itemId'</span> element={<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Publisher</span> /&gt;</span></span>} /&gt;
    &lt;/Route&gt;
</code></pre>
<p>Note that the <code>:</code> in the <code>:itemId</code> URL segment means Dynamic Segment. </p>
<p>Next, create a new component as <code>./src/publisher.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//publisher.jsx</span>

<span class="hljs-keyword">import</span> { useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Publisher</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> { itemId } = useParams();

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    {
        itemId ? (
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Book publishing companies<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Penguin Random House<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Scholastic<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>LPI Media<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        ) : (
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Page item is not present<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        )
    }
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Let's talk about what this code is doing: </p>
<ul>
<li><code>const { itemId } = useParams()</code>: here we apply <em>destructuring</em> to get the params from the URL in the address bar. With this, we can render the return content.</li>
<li><code>itemId?():</code>: here we conditionally render a list of bookshop companies when a clicked link matches the params.</li>
</ul>
<p>Next, in the <code>./src/book</code> component, include the <code>publisher/itemId</code> as indicated in the <code>&lt;a&gt;</code> tag below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//book.jsx</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Book</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"px-4"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>77 Ways get to more customers By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Ubuy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Authenticity By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Emanuel Rose<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span> Change Your thinking change your life By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Brian Tracy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>

                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"bookshop"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 inline-block px-4 underline"</span>&gt;</span>see bookshop<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            👉  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"publisher/itemId"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 underline"</span>&gt;</span>Publisher<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Dynamic_segment_one.gif" alt="Dynamic Segments in React" width="600" height="400" loading="lazy"></p>
<p>Notice the update in the URL at the browser address bar. </p>
<p>Let's look at another example.</p>
<p>In a real-world application, Dynamic Segments are mostly used to render content dynamically when the segment <code>:itemId</code> matches the <code>id</code> of the returned APIs.  </p>
<p>Let’s see how this works. First, we need to decide where we will be fetching our data from. In this case, create an external JavaScript object <code>./scr/books.js</code> and copy and paste the code below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//books.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {   <span class="hljs-attr">id</span>: <span class="hljs-string">"1"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"The Great Gatsby"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"F. Scott Fitzgerald"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1925"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"The Great Gatsby is a 1925 novel by American writer F. Scott Fitzgerald. Set in the Jazz Age on Long Island, near New York City, the novel depicts first-person narrator Nick Carraway's interactions with mysterious millionaire Jay Gatsby and Gatsby's obsession to reunite with his former lover, Daisy Buchanan."</span>
    },

    {   <span class="hljs-attr">id</span>: <span class="hljs-string">"2"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"Pride and Prejudice"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Jane Austen"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1813"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"Pride and Prejudice is the second novel by English author Jane Austen, published in 1813. A novel of manners, it follows the character development of Elizabeth Bennet, the protagonist of the book"</span>
    },

    {   <span class="hljs-attr">id</span>: <span class="hljs-string">"3"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"To Kill a Mockingbird"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Harper Lee"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1960"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"To Kill a Mockingbird is a novel by the American author Harper Lee. It was published in June 1960 and became instantly successful. In the United States"</span>
    },

    {   <span class="hljs-attr">id</span>:<span class="hljs-string">"4"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"Beloved"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Toni Morrison"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1987"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"Beloved is a 1987 novel by American novelist Toni Morrison. Set in the period after the American Civil War, the novel tells the story of a dysfunctional family of formerly enslaved people whose Cincinnati home is haunted by a malevolent spirit"</span>
    }
]
</code></pre>
<p>Next, create a new component called <code>./src/FavBooks.jsx</code> and write in the code below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//FavBooks.js</span>

<span class="hljs-keyword">import</span> { useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
<span class="hljs-keyword">import</span> book <span class="hljs-keyword">from</span> <span class="hljs-string">'./book'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FavBooks</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> {bookId} =  useParams() 

👉 <span class="hljs-keyword">const</span> newFavBook = book.find(<span class="hljs-function">(<span class="hljs-params">book</span>) =&gt;</span> book.id === bookId)

  <span class="hljs-keyword">if</span>(!newFavBook){
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`This page doesn't contain fav Books`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
  }
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
          {newFavBook &amp;&amp; (
            <span class="hljs-tag">&lt;&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`Title: ${newFavBook.title}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`By: ${newFavBook.author}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`Year: ${newFavBook.year}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`Description: ${newFavBook.description}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
            <span class="hljs-tag">&lt;/&gt;</span>
          )}
      <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>Next, go to the <code>./src/book.jsx</code> component and update the code as follows:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
<span class="hljs-keyword">import</span> books <span class="hljs-keyword">from</span> <span class="hljs-string">'./book.js'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Books</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-3xl"</span>&gt;</span>{`List of my favourite books`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span> 
        {
          books &amp;&amp; books.map((book)=&gt; (
            <span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">newbooks</span>/${<span class="hljs-attr">book.id</span>}`} <span class="hljs-attr">className</span>=<span class="hljs-string">'text-blue-600 underline'</span>&gt;</span>{book.title}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;/&gt;</span>
          )) 
        }
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>Next, configure the <code>path</code> to dynamic segment on the route component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Book <span class="hljs-keyword">from</span> <span class="hljs-string">'../src/books'</span>;
<span class="hljs-keyword">import</span> Bookshop <span class="hljs-keyword">from</span> <span class="hljs-string">'./bookshop'</span>;
<span class="hljs-keyword">import</span> Publisher <span class="hljs-keyword">from</span> <span class="hljs-string">'./publisher'</span>;
👉 <span class="hljs-keyword">import</span> FavBooks <span class="hljs-keyword">from</span> <span class="hljs-string">'./FavBooks'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>} /&gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'publisher/:itemId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Publisher</span> /&gt;</span>} /&gt;
     👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'newbooks/:bookId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">FavBooks</span> /&gt;</span>} /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Dynamic_segment_from_api.gif" alt="Dynamic segment from api in react" width="600" height="400" loading="lazy"></p>
<p>From the browser output, the URL params are being updated with the path segment and the id values from the books.js object. </p>
<p>Try clicking on each of the titles and notice how the id of the books.js object is present in the URL.</p>
<p>When a user clicks the link, it renders a new UI on a new page. But there are cases where you might want to render the content of the API on the same page as list items, so the content doesn’t have to open on a new page. To do this, we have to implement nested routing. </p>
<h2 id="heading-nested-routing">Nested Routing</h2>
<p>Nested routing makes it possible to nest routes to render new components on the same page for easy navigation and quick interactivity of the element. Nested routes make listed items function as a tab. As soon as any tab is clicked, the content that matches the corresponding tab gets displayed.</p>
<p>Now let’s see how to convert our little application to a nested route. </p>
<p>To go the <code>./src/main</code> component and create a nested route as follows:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>} &gt;
   👉  <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'newbooks/:bookId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">FavBooks</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'publisher/:itemId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Publisher</span> /&gt;</span>}&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)
</code></pre>
<p>Essentially, we are nesting the <code>FavBook</code> component to the <code>Book</code> component as its direct child, so the content will display below it.</p>
<p>Next, create a div tag and render an outlet component. This is us telling react-router where to render the newly nested route. </p>
<pre><code class="lang-jsx"><span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-3xl"</span>&gt;</span>{`List of my favourite books`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    👉 <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'flex'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span> 
          {
            books &amp;&amp; books.map((book)=&gt; (
              <span class="hljs-tag">&lt;&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">newbooks</span>/${<span class="hljs-attr">book.id</span>}`} <span class="hljs-attr">className</span>=<span class="hljs-string">'text-blue-600 underline'</span>&gt;</span>{book.title}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/&gt;</span>
            )) 
          }
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'w-[70%]'</span>&gt;</span>
       👉  <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
</code></pre>
<p>Note that to render the nested <code>FavBook</code> component side by side, both the outlet and the book list tag are nested in a section tag, and a style of display flex is applied.</p>
<p>Your code should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/rendered_outlet.gif" alt="Nested Routing in React" width="600" height="400" loading="lazy"></p>
<p>From the browser output, you can see that each listed item functions as a tab, and clicking them renders out the content of the API.</p>
<p>You've learned how to create a dynamic segment. In the next section, we'll build a project to help cement what we’ve learned even further.</p>
<h2 id="heading-project-build-an-art-gallery">Project: Build an Art Gallery</h2>
<p>In this project, we are going to build an art gallery app that contains a list of sculpture and art from different countries. This will help you solidify the concepts you've previously learned. </p>
<p>We are going to implement the following features:</p>
<ul>
<li>Client Side Routing</li>
<li>Active Links</li>
<li>Dynamic Segments</li>
<li>Nested Routes</li>
</ul>
<p>Below is a quick overview of what the project will look like.</p>
<h3 id="heading-project-overview">Project Overview</h3>
<p>Here is a complete preview of our project after completion. You can download the source code on 👉 <a target="_blank" href="https://github.com/alex-anie/Arts-Culture-dynamic-segment-example">GitHub</a> here.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/cotent_component-1.gif" alt="Project: Build an Art Gallery in React.js" width="600" height="400" loading="lazy"></p>
<h3 id="heading-folder-structure">Folder Structure</h3>
<p>This is what the project's folder structure should look like:</p>
<pre><code class="lang-html">📂src
        📂apis
            ├──data.js
        📂components
            ├──AsideBar.jsx
            ├──Content.jsx
            ├──Navbar.jsx
        📂pages
            ├──home.jsx
    ├──index.css
    ├──main.jsx
├──index.html
</code></pre>
<h3 id="heading-how-to-set-up-the-home-page">How to Set Up the Home Page</h3>
<p>To set up the home page, create a home component <code>./src/pages/home.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">├──home.jsx

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-orange-600"</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Next, go to the <code>main.jsx</code> component. If you don’t have one yet, create it as <code>./src/main.jsx.</code> then configure the route as follows:</p>
<pre><code class="lang-jsx">├──main.jsx

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;

<span class="hljs-keyword">import</span> {
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./pages/home'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>}&gt;

    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>Next, type <code>npm run dev</code> to launch your app. </p>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled-2.png" alt="Hello, World in React" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-create-and-style-the-navbar">How to Create and Style the Navbar</h3>
<p>Now that we have the home and route component set up, let’s create the navbar component, which is the top component in our app.</p>
<p>Create a component <code>./src/components/navbar.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">├──navbar.jsx

<span class="hljs-keyword">import</span> { Activity, Search } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-feather"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex justify-between bg-slate-200 rounded-3xl py-2"</span>&gt;</span>
                {/* logo */}
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Activity</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"inline-block ml-10 mr-2 text-orange-500"</span> /&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"inline-block text-xl"</span>&gt;</span>{`Arts &amp; Culture`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

                {/* Navlinks */}
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-white rounded-3xl py-1 px-2 mr-5"</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Search</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"inline-block mr-1 text-slate-500"</span>/&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"search"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"site-search"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"q"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Search anything"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-transparent outline-none text-slate-800"</span>/&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
   <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>From the code above, the navbar is divided between the logo and the search bar.</p>
<ul>
<li><strong>The logo:</strong> we import the <em>Activity Icons</em> as a component from the feather icons and apply some Tailwind CSS classes to style it. The icon is set to <code>inline-block</code> so that we can apply space to it. We apply <code>ml-10</code> and <code>mr-2</code>, which is margin-left of <code>2.5rem</code> and right <code>0.5rem</code> with a color of orange (<code>text-orange-500</code>).</li>
<li><strong>The search</strong>: for the search bar, we also imported it from feather icons as a component and applied the following styling: <code>inline-block mr-1 text-slate-500</code>. If you are having a hard time understanding Tailwind CSS classes you can read more about them from the docs <a target="_blank" href="https://tailwindcss.com/">here</a>.</li>
</ul>
<p>To position the logo and search bar side by side, we set the parent header to display flex and justify-content of space-between to apply space in between the logo and search bar.</p>
<p>Next, add the navbar to the route as follows:</p>
<pre><code class="lang-jsx">├──main.jsx

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>

<span class="hljs-keyword">import</span> {
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>

👉  <span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Navbar'</span>
        <span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./pages/home'</span>

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>}&gt;
    👉   <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Navbar</span> /&gt;</span>} /&gt; 
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>From the code above, the Navbar component is nested inside the Home components. This means we have to use an Outlet component to render the Navbar component.</p>
<p>Next, go to the <code>./src/page/home</code> component, import, and replace the <code>&lt;p&gt;</code> with the Outlet component as indicated below:</p>
<pre><code class="lang-jsx">├──home.jsx

👉 <span class="hljs-keyword">import</span> { Outlet } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
           👉 <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your app should look like this on your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled-3.png" alt="Navbar component in react" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-create-the-asidebar">How to Create the AsideBar</h3>
<p>The <code>AsideBar</code> is one of the most important components of our app. This is where the names of the cultures will be displayed. This component functions as a tab, and when a user clicks on it, it renders out more details about the culture that was clicked on. </p>
<p>Create a new component as <code>./src/components/AsideBar.jsx</code> and write in the code below:</p>
<pre><code class="lang-jsx">├──AsideBar.jsx

<span class="hljs-keyword">import</span> { NavLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>
<span class="hljs-keyword">import</span> data <span class="hljs-keyword">from</span> <span class="hljs-string">"../apis/data"</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AsideBar</span>(<span class="hljs-params"></span>) </span>{

    <span class="hljs-keyword">const</span> activeStyle = <span class="hljs-function">(<span class="hljs-params">{isActive}</span>) =&gt;</span>  {
            <span class="hljs-keyword">return</span> {
                <span class="hljs-attr">backgroundColor</span> : isActive ? <span class="hljs-string">"rgb(154 52 18)"</span> : <span class="hljs-string">""</span>,
                <span class="hljs-attr">color</span> : isActive ? <span class="hljs-string">"rgb(255 247 237)"</span> : <span class="hljs-string">""</span>,
            }
        }

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[100%] mt-[2em]"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[100%]"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[fit-content] bg-slate-200 rounded-xl"</span>&gt;</span>
                    {
                        data.map((data)=&gt;(
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{data.id}</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[100%] py-3 px-2 inline-block text-slate-800 hover:bg-orange-200 transition-all whitespace-nowrap border-y-4 "</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">content</span>/${<span class="hljs-attr">data.id</span>}`} <span class="hljs-attr">style</span>=<span class="hljs-string">{activeStyle}</span>&gt;</span>
                                        {data.type}
                                    <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        ))
                    }
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>From the code example above, we have our code split into two sections: the <code>data</code> and the <code>NavLink</code> component.</p>
<ul>
<li><strong>The data:</strong> we imported the data from <code>./src/apis/data.js</code> and we are mapping each array of objects and returning the <code>data.type</code> as the names of the <code>AsideBar</code>.</li>
<li><strong>The NavLink</strong>: the data returned from <code>data.js</code> is rendered directly on the <code>NavLink</code> component. The <code>NavLink</code> component has two props specified, the <code>style</code> and <code>to</code> props. The <code>style</code> prop received the <code>activeStyle</code> object that indicated what style should be applied to <code>NavLink</code> when its active. The <code>to</code> prop <code>to={</code>content/${data.id}<code>}</code>  we pass the <code>data.id</code> as a segment to match with the <code>path</code> of the content components (<em>more on this in the next section</em>). This enables the content to dynamically render when the <code>NavLink</code> is clicked on.</li>
</ul>
<p>Next, go to the home component and render the <code>AsideBar</code> as indicated below:</p>
<pre><code class="lang-jsx">├──home.jsx

      <span class="hljs-keyword">import</span> { Outlet } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
👉 <span class="hljs-keyword">import</span> AsideBar <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/AsideBar"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      👉  <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[80%] mt-[2em] mx-auto"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
                 👉 <span class="hljs-tag">&lt;<span class="hljs-name">AsideBar</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/aside_component.gif" alt="aside component in react" width="600" height="400" loading="lazy"></p>
<p>From interacting with the <code>Asidebar</code>, you may have noticed that the page breaks any time you click on the links. This is because the content component is not defined yet. So let’s create it.</p>
<h3 id="heading-how-to-create-the-content-component">How to Create the Content Component</h3>
<p>The content component renders the content that is related to a particular link that was clicked. </p>
<p>Create a new component called <code>./src/components/Content.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">├──Content.jsx

<span class="hljs-keyword">import</span> { Link, useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
<span class="hljs-keyword">import</span> data <span class="hljs-keyword">from</span> <span class="hljs-string">"../apis/data.js"</span>;
<span class="hljs-keyword">import</span> { WifiOff } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-feather"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Content</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> {contentId} = useParams()

    <span class="hljs-keyword">const</span> newData = data.find(<span class="hljs-function">(<span class="hljs-params">data</span>)=&gt;</span> data.id.toString() === contentId)

    <span class="hljs-keyword">if</span>(!contentId){
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"translate-x-44 translate-y-44"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">WifiOff</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-slate-400 text-center translate-x-48"</span>/&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-slate-400"</span>&gt;</span>{`Content can't be accessed! click the left nav to reload`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
        )
    }

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[80%] mx-auto mt-8"</span>&gt;</span> 
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span> &gt;</span>
                {
                    newData &amp;&amp; (
                        <span class="hljs-tag">&lt;&gt;</span>  
                            {/* Image Over */}
                            <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"h-[6em] w-[100%]"</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"h-[100%]  w-[100%]"</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{newData.imgHeaders}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"h-[100%]  w-[100%] object-cover rounded-xl"</span>/&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>

                            {/* Details */}
                           <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-6"</span>&gt;</span>
                           <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[50%]"</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-orange-500 w-[fit-content] rounded-xl mt-4 py-1 px-2 font-bold"</span>&gt;</span>{newData.catagories}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-light text-4xl my-7"</span>&gt;</span>{newData.type}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-bold mb-4 text-2xl"</span>&gt;</span>{newData.region}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-light"</span>&gt;</span>{newData.history}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"mt-4"</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>{`Learn more from`}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{newData.britannicaLink}</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-orange-500 py-2 px-2 rounded-md inline mt-4 hover:underline hover:text-black"</span>&gt;</span>britannica<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>

                            {/* Image Cover */}
                            <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[50%]"</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{newData.imgCover}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"rounded-3xl mt-10"</span>/&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>

                        <span class="hljs-tag">&lt;/&gt;</span>
                    )
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>The code above is doing the following:</p>
<ul>
<li><strong>useParams</strong>: we use the <a target="_blank" href="https://reactrouter.com/en/main/hooks/use-params">useParams()</a> hook to return the key and value pairs of the dynamic segment <code>content/:contentId</code> specified on the route.</li>
<li><strong>newData</strong>: using the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find">find()</a> array method, the first element of the object array is returned if the condition is true, otherwise it returns <code>undefined</code>.</li>
<li><strong>if(!contentId)</strong>: here, we are checking to see if the <code>contentId</code> doesn’t match or is not rendered yet – then the provided element within the function should run. This is very useful for checking errors and in situations where the content is not available.</li>
<li><strong>newData &amp;&amp;</strong>: here we are mapping through the return data object and rendering the content of the API as soon as the content is loaded. Each object property is parsed into an element to render as content.</li>
</ul>
<p>Next, go to the home component and render the content component as indicated below.</p>
<pre><code class="lang-jsx">├──home.jsx

        <span class="hljs-keyword">import</span> { Outlet } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
        <span class="hljs-keyword">import</span> AsideBar <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/AsideBar"</span>;
👉 <span class="hljs-keyword">import</span> Content <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/Content"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[80%] mt-[2em] mx-auto"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">AsideBar</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>

                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
              👉 <span class="hljs-tag">&lt;<span class="hljs-name">Content</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Next, configure the route to a dynamic segment:</p>
<pre><code class="lang-jsx">├──home.jsx

     <span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./pages/home'</span>;
     <span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/navbar'</span>;
👉 <span class="hljs-keyword">import</span> Content <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Content'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>}&gt;
       <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Navbar</span> /&gt;</span>}&gt; 
       👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'content/:contentId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Content</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>Your app should look just like this on your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/cotent_component-1-1.gif" alt="cotent component in react.js" width="600" height="400" loading="lazy"></p>
<p>Clicking on the <code>Asidebar</code> will load the content from the API and render it on the same page as the <code>Asidebar</code>.</p>
<h2 id="heading-summary">Summary</h2>
<p>In this tutorial, we learned about Dynamic Segments in React Router. We talked about what Dynamic Router is and how it’s different from static routing. You also learned how to use the <code>useParams</code> hook to enable Dynamic Segments, as well as how to set your path when getting data from an API</p>
<p>Then we built a new project that dynamically rendered new content to the same page when the user clicks on the sidebar.</p>
<p>You can take this project further and make it your own by implementing more features. </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn React Router 6 in Ten Hours ]]>
                </title>
                <description>
                    <![CDATA[ React Router is a powerful library that helps you build single-page applications with ease. It allows you to create multiple routes, nested routes, and handle transitions between them, all while maintaining a clean and organized codebase. We just pub... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-react-router-6-full-course/</link>
                <guid isPermaLink="false">66b204f7eea9870582e16c9c</guid>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Mon, 24 Apr 2023 20:34:28 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/reactrouter.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>React Router is a powerful library that helps you build single-page applications with ease. It allows you to create multiple routes, nested routes, and handle transitions between them, all while maintaining a clean and organized codebase.</p>
<p>We just published a course on the freeCodeCamp.org YouTube channel that will teach you all about React Router 6.</p>
<p>In this course, you will build an Airbnb-style web app called "VanLife," dedicated to renting out travel vans for your next big road trip. Along the way, you will learn all the important parts of React Router 6, such as layout and index routes, nested routes, filtering results with search parameters, protecting routes for authenticated users, and more.</p>
<p>Bob Ziroll, Scrimba's Head of Education, created this course. It is intended to have heavy amounts of participation from you, the student.</p>
<p>Before taking this course, you should already be well versed in HTML, CSS, JavaScript, and React. But if you're ready to take your React Router skills to the next level, this course is for you.</p>
<p>The course is broken down into multiple sections, including an introduction to React Router 6, multi-page vs. single-page apps, setting up React Router with BrowserRouter, creating routes and paths, and building out the VanLife app. You'll also learn about the new Remix-inspired data router APIs, including Loaders and Actions.</p>
<p>The course is structured so that you can follow along with each lesson and build out the VanLife app in real-time. You'll also have the opportunity to participate in challenges that test your understanding of the material.</p>
<p>By the end of this course, you will have built a real-world app using React Router 6, and you'll be ready to tackle even more complex projects in the future.</p>
<p>So what are you waiting for? Watch the <a target="_blank" href="https://youtu.be/nDGA3km5He4">full course on the freeCodeCamp.org YouTube channel</a> (10-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/nDGA3km5He4" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Deploy a React Router App to Netlify and Fix the "Page Not Found" Error ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever experienced a “PAGE NOT FOUND” error when trying to access or refresh an application that uses React Router? Don't worry, you're not alone. In this article, you will learn how to deploy a React application that uses react-router without... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-deploy-a-routed-react-app-to-netlify/</link>
                <guid isPermaLink="false">66c8c860c4cede4e0083f72e</guid>
                
                    <category>
                        <![CDATA[ Netlify ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Chidera ]]>
                </dc:creator>
                <pubDate>Mon, 03 Oct 2022 17:16:24 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/09/netlify1-1-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever experienced a “PAGE NOT FOUND” error when trying to access or refresh an application that uses React Router? Don't worry, you're not alone.</p>
<p>In this article, you will learn how to deploy a React application that uses react-router without any problems.</p>
<h2 id="heading-the-problem-with-deploying-react-router-apps">The Problem with Deploying react-router Apps</h2>
<p>To make sure your users are happy, you'll typically prioritize user experience when building an application. Good user experience ensures that your website or application is easy to understand, easy to use, and easy to navigate. </p>
<p>If you have published a React app to Netlify that uses React router before now, you will likely notice that while navigating through your routes, you get an error (<strong>page not found</strong>) when you refresh your browser. This creates a bad user experience. </p>
<p>Through this article, you will learn how to deploy a React app through the Netlify CLI and how to resolve the problem with deploying a react-router app.</p>
<h2 id="heading-what-is-netlify">What is Netlify?</h2>
<p>According to <a target="_blank" href="https://docs.netlify.com/">their docs</a>, "Netlify is an all-in-one platform for automating modern web projects." </p>
<p>Netlify helps web developers build and deploy their applications instantly. This handy tool replaces your hosting infrastructure and helps facilitate your continuous integration and deployment pipeline with one workflow. It can really help increase your productivity.</p>
<p>The Netlify CLI (Command Line Interface) helps you easily build, test, and deploy applications straight from your command line. </p>
<p>It lets you:</p>
<ol>
<li>start a project in seconds</li>
<li>configure continuous deployment</li>
<li>run a local development server that you can share with other developers</li>
<li>deploy your projects globally</li>
</ol>
<p>You can sign up for a <a target="_blank" href="http://netlify.app">Netlify account</a> with your email or your Git account.<br>If you don't have a Git account, you can create a GitHub, GitLab, or bitbucket account in no time.</p>
<p>So, let’s see how you can deploy your react-router app using the Netlify CLI.</p>
<h2 id="heading-how-to-deploy-your-app-through-the-netlify-cli">How to Deploy Your App Through the Netlify CLI</h2>
<p>To start using Netlify’s CLI, you must have Node.js installed on your computer. You can visit <a target="_blank" href="https://nodejs.org/en/download/">here</a> to install Node.js. </p>
<p>Then you can get started by installing the Netlify CLI using this command:</p>
<pre><code class="lang-js">npm install netlify-cli -g
</code></pre>
<p>Now that you have installed Netlify’s CLI, you can deploy your application to Netlify. Before that, you might want to get your build folder (I'll explain why below). </p>
<p>To get your build folder, type the following command:</p>
<pre><code class="lang-js">yarn run build
<span class="hljs-comment">//or</span>
npm run build
</code></pre>
<p>If you have not logged in to your Netlify account before now, you might see a pop-up window asking for permission to access Netlify.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/netlify3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After getting access to the Netlify CLI, you will get a prompt asking what you would like to do. Select the option to <strong>create and configure a new site</strong>. You can use your arrow keys to navigate between options.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/netlify4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You will get prompts to select a team and a site name for your app. You can either input your preferred name for your app or use the default name which you can change later.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/netlify5.1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You will get a prompt asking which directory to publish. Input <strong>build</strong> as your deploy folder and press enter to get your React app deployed. The build folder which was generated at the beginning 0f this tutorial will serve as the deploy path.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/netlify7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you get a final response that states <strong>“If everything looks good on your draft URL, deploy it to your main site URL with the --prod flag”</strong>, you are on the right track. You will be provided with the website draft URL to preview your app.</p>
<p>You can deploy to the main site by running the following command:</p>
<pre><code class="lang-js">netlify deploy --prod
</code></pre>
<p>Awesome. You now have your website URL.</p>
<h2 id="heading-how-to-fix-the-page-not-found-error">How to Fix the "Page Not Found" Error</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/netlify1.png" alt="Image" width="600" height="400" loading="lazy">
<em>page not found, netlify error</em></p>
<p>To avoid the page-not-found error whenever you are either trying to access your app or you're on other routes, you have to set up a redirect and rewrite rules for your react-router application. </p>
<p>The redirect rule will help your application avoid a 404 error. All requests will get redirected to the /index.html instead of giving a 404 error.</p>
<p>To configure your redirect rules, you have to create a file that does not have an extension named (_redirects) inside your public folder.</p>
<p>Include the following command in the _redirects file:</p>
<pre><code class="lang-_redirects">/*    /index.html  200
</code></pre>
<p>To view the changes in your app, you'll have to deploy it again with the following command:</p>
<pre><code class="lang-js">netlify deploy
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This article has explained how to deploy a react-router app using Netlify’s CLI and fix the page-not-found error while accessing your routes.</p>
<p>I hope you have found this article useful.</p>
<p><strong>Keep building, and keep deploying!</strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Implement Vertical Scrolling in React Using react-router-hash-link ]]>
                </title>
                <description>
                    <![CDATA[ Smooth scrolling is a feature that makes webpages more usable and allows for a better user scrolling experience in most browsers. Implementing a smooth page scroll while using react-router-dom has been a problem in React.js. So this article, you are ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-implement-vertical-scrolling-in-react-using-react-router-hash-link/</link>
                <guid isPermaLink="false">66c8c8620f021017a28a49cb</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Israel Chidera ]]>
                </dc:creator>
                <pubDate>Tue, 27 Sep 2022 17:42:42 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/09/ferenc-almasi-L8KQIPCODV8-unsplash.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Smooth scrolling is a feature that makes webpages more usable and allows for a better user scrolling experience in most browsers.</p>
<p>Implementing a smooth page scroll while using react-router-dom has been a problem in React.js. So this article, you are going to learn how to implement smooth vertical scrolling using the react-router-hash-link package.</p>
<h3 id="heading-what-is-react-router-hash-link">What is react-router-hash-link?</h3>
<p>According to its <a target="_blank" href="https://github.com/rafgraph/react-router-hash-link">docs</a>, react-router-hash-link is a solution to React Router's issue of not scrolling to #hash-fragments when using link components to navigate.</p>
<h3 id="heading-prerequisites">Prerequisites.</h3>
<p>You must use <em>BrowserRouter</em> from react-router-dom for react-router-hash-link smooth scrolling to work. To install and use react-router, enter the following command:</p>
<pre><code class="lang-js">npm i react-router-dom
</code></pre>
<p>Or for yarn:</p>
<pre><code class="lang-js">yarn add react-router-dom
</code></pre>
<p>To understand how to implement vertical scrolling, you will be building a simple landing page with a navbar and three sections.</p>
<h2 id="heading-how-to-use-react-router-hash-link">How to Use react-router-hash-link</h2>
<h3 id="heading-step-1-install-react-router-hash-link">Step 1: Install react-router-hash-link</h3>
<p>To install the react-router-hash-link package, run the following command:</p>
<pre><code class="lang-js">npm install --save react-router-hash-link
</code></pre>
<p>Or with yarn:</p>
<pre><code class="lang-js">yarn add react-router-hash-link
</code></pre>
<h3 id="heading-step-2-import-the-react-router-hash-link-package-into-your-react-app">Step 2: Import the react-router-hash-link package into your React app.</h3>
<p>Open your LandingPage.js file and import <em>hashlink</em> from your react-router-hash-link package.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { HashLink } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-hash-link'</span>;

<span class="hljs-keyword">const</span> LandingPage = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
            . . .
            <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
            . . .
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/&gt;</span></span>
    )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> LandingPage
</code></pre>
<h3 id="heading-step-3-add-the-hashlink-component-to-point-to-different-areas-of-your-app">Step 3: Add the Hashlink component to point to different areas of your app</h3>
<p>Add the Hashlink component to your LandingPage.js file like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { HashLink } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-hash-link'</span>;

<span class="hljs-keyword">const</span> LandingPage = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">HashLink</span> <span class="hljs-attr">smooth</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/#home"</span>&gt;</span>
                About
            <span class="hljs-tag">&lt;/<span class="hljs-name">HashLink</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">HashLink</span> <span class="hljs-attr">smooth</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/#services"</span>&gt;</span>
                Profile
            <span class="hljs-tag">&lt;/<span class="hljs-name">HashLink</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">HashLink</span> <span class="hljs-attr">smooth</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/#testimonial"</span>&gt;</span>
                Services
            <span class="hljs-tag">&lt;/<span class="hljs-name">HashLink</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
        . . .
        <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
    )
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> LandingPage
</code></pre>
<h3 id="heading-step-4-add-the-id-of-your-hashlink-to-any-element">Step 4: Add the ID of your Hashlink to any element.</h3>
<p>When you click on the link with your id, a scroll effect will be implemented. You will see a scroll to the element or the page that has an id that matches the #hashfragment in the link.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { HashLink } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-hash-link'</span>;

<span class="hljs-keyword">const</span> LandingPage = <span class="hljs-function">() =&gt;</span> {

    <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">HashLink</span> <span class="hljs-attr">smooth</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/#home"</span>&gt;</span>
                About
            <span class="hljs-tag">&lt;/<span class="hljs-name">HashLink</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">HashLink</span> <span class="hljs-attr">smooth</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/#services"</span>&gt;</span>
                Profile
            <span class="hljs-tag">&lt;/<span class="hljs-name">HashLink</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">HashLink</span> <span class="hljs-attr">smooth</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/#testimonial"</span>&gt;</span>
                Services
            <span class="hljs-tag">&lt;/<span class="hljs-name">HashLink</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">id</span>=<span class="hljs-string">”about”</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> About<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                Lorem ipsum dolor sit, amet consectetur adipisicing elit.
                Vero, nam! Iure officia aut esse tempore accusantium
                explicabo? Corporis deleniti ipsa fuga quas aut neque
                dicta nostrum laboriosam, iusto ullam minima est porro,
                totam saepe. Facilis aliquid praesentium, voluptates rem
                quibusdam sequi numquam illo eius adipisci eaque,
                necessitatibus consectetur, labore vero et ipsum.
                Officiis, ea vero. Praesentium, et. Enim, nostrum illo.
            <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>        
        <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">id</span>=<span class="hljs-string">”profile”</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Profile <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                Lorem ipsum dolor sit, amet consectetur adipisicing elit.
                Vero, nam! Iure officia aut esse tempore accusantium
                explicabo? Corporis deleniti ipsa fuga quas aut neque
                dicta nostrum laboriosam, iusto ullam minima est porro,
                totam saepe. Facilis aliquid praesentium, voluptates rem
                quibusdam sequi numquam illo eius adipisci eaque,
                necessitatibus consectetur, labore vero et ipsum.
                Officiis, ea vero. Praesentium, et. Enim, nostrum illo.
            <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">id</span>=<span class="hljs-string">”services”</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Services <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                Lorem ipsum dolor sit, amet consectetur adipisicing elit.
                Vero, nam! Iure officia aut esse tempore accusantium
                explicabo? Corporis deleniti ipsa fuga quas aut neque
                dicta nostrum laboriosam, iusto ullam minima est porro,
                totam saepe. Facilis aliquid praesentium, voluptates rem
                quibusdam sequi numquam illo eius adipisci eaque,
                necessitatibus consectetur, labore vero et ipsum.
                Officiis, ea vero. Praesentium, et. Enim, nostrum illo.
            <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
    )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> LandingPage
</code></pre>
<p>And there you have it! You've now implemented smooth scrolling on a simple webpage.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>React-router-hash-link allows you to leverage smooth scrolling seamlessly while using react-router-dom for routing. </p>
<p>It has a lot of functions including Scroll to top and Scroll with offset functions, so you can explore what else it can do.  </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn React Router 6 ]]>
                </title>
                <description>
                    <![CDATA[ React Router is a JavaScript library that helps developers create single-page applications that are responsive and have a dynamic user interface. It is a popular tool among React developers and is used to create routing and navigation for web applica... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-react-router-6/</link>
                <guid isPermaLink="false">66b204f920f547d355775790</guid>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Wed, 04 May 2022 14:42:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/05/reactrouter6.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>React Router is a JavaScript library that helps developers create single-page applications that are responsive and have a dynamic user interface. It is a popular tool among React developers and is used to create routing and navigation for web applications.</p>
<p>We just published a course on the freeCodeCamp.org YouTube channel that will teach you how to use React Router version 6. </p>
<p>John Smilga has created many extremely popular courses and now he will teach you React Router.</p>
<p>React Router v6 is a complete rewrite of the previous versions. The biggest difference is that v6 is a static router and does not keep track of any state internally. This means that v6 is much simpler and easier to use than previous versions.</p>
<p>Another difference is that v6 uses a new URL parsing algorithm that is more compliant with the RFC standard. This means that v6 is more likely to work with other software that also uses RFC compliant URL parsing.</p>
<p>Here are the sections in this course:</p>
<ul>
<li>First Pages</li>
<li>Page Components</li>
<li>Link Component</li>
<li>Error Page</li>
<li>Navbar</li>
<li>Nested Routes</li>
<li>Shared Layout</li>
<li>Index Pages</li>
<li>NavLink Component</li>
<li>URL Params</li>
<li>Single Product</li>
<li>useNavigate()</li>
<li>Protected Route</li>
<li>Refactor</li>
</ul>
<p>Watch the full course below or on <a target="_blank" href="https://youtu.be/59IXY5IDrBA">the freeCodeCamp.org YouTube channel</a> (1.5 hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/59IXY5IDrBA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Router Version 6 Tutorial – How to Set Up Router and Route to Other Components ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, we'll talk about what React Router is and how to use it. Then we'll discuss its features and how to use them in your React app to navigate to and render multiple components.  Prerequisites A React app A good understanding of what c... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-react-router-version-6/</link>
                <guid isPermaLink="false">66b0a2f2b30dd4d00547bb9d</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ihechikara Abba ]]>
                </dc:creator>
                <pubDate>Tue, 14 Dec 2021 21:41:14 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/12/react-router-cover.svg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, we'll talk about what React Router is and how to use it. Then we'll discuss its features and how to use them in your React app to navigate to and render multiple components. </p>
<h3 id="heading-prerequisites"><strong>Prerequisites</strong></h3>
<ul>
<li>A React app</li>
<li>A good understanding of what components are in React. </li>
<li>Node.js installed.</li>
</ul>
<h3 id="heading-heres-an-interactive-scrim-about-how-to-set-up-react-router-and-route-to-other-components">Here's an interactive scrim about how to set up React Router and route to other components:</h3>
<div class="embed-wrapper"><iframe src="https://scrimba.com/scrim/crd9bBC6?embed=freecodecamp,mini-header" width="100%" height="480" title="Embedded content" loading="lazy"></iframe></div>

<h2 id="heading-react-as-a-single-page-application-spa">React as a Single Page Application (SPA)</h2>
<p>You need to understand how pages are rendered in a React app before diving into routing. This section is aimed at beginners – you can to skip it if you already understand what a SPA is and how it relates to React. </p>
<p>In non-single page applications, when you click on a link in the browser, a request is sent to the server before the HTML page gets rendered. </p>
<p>In React, the page contents are created from our components. So what React Router does is intercept the request being sent to the server and then injects the contents dynamically from the components we have created. </p>
<p>This is the general idea behind SPAs which allows content to be rendered faster without the page being refreshed.</p>
<p>When you create a new project, you'll always see an <code>index.html</code> file in the public folder. All the code you write in your <code>App</code> component which acts as the root component gets rendered to this HTML file. This means that there is only one HTML file where your code will be rendered to.</p>
<p>What happens when you have a different component you would prefer to render as a different page? Do you create a new HTML file? The answer is no. React Router – like the name implies – helps you route to/navigate to and render your new component in the <code>index.html</code> file.</p>
<p>So as a single page application, when you navigate to a new component using React Router, the <code>index.html</code> will be rewritten with the component's logic.</p>
<h2 id="heading-how-to-install-react-router">How to Install React Router</h2>
<p>To install React Router, all you have to do is run <code>npm install react-router-dom@6</code> in your project terminal and then wait for the installation to complete. </p>
<p>If you are using yarn then use this command: <code>yarn add react-router-dom@6</code>.</p>
<h2 id="heading-how-to-set-up-react-router">How to Set Up React Router</h2>
<p>The first thing to do after installation is complete is to make React Router available anywhere in your app. </p>
<p>To do this, open the <code>index.js</code> file in the <code>src</code> folder and import <code>BrowserRouter</code> from <code>react-router-dom</code> and then wrap the root component (the <code>App</code> component) in it. </p>
<p>This is what the <code>index.js</code> looked like initially:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./App'</span>;

ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)
);
</code></pre>
<p>After making changes with React Router, this is what you should have:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./App'</span>;
<span class="hljs-keyword">import</span> { BrowserRouter } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>,
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>)
);
</code></pre>
<p>All we did was replace <code>React.StrictMode</code> with <code>BrowserRouter</code> which was imported from <code>react-router-dom</code>.  Now the router features are accessible from any part of your app.</p>
<h2 id="heading-how-to-route-to-other-components">How to Route to Other Components</h2>
<p>We are finally done setting things up, so now we'll look at routing to and rendering different components.</p>
<h3 id="heading-step-1-create-multiple-components">Step 1 - Create multiple components</h3>
<p>We'll create the following <code>Home</code>, <code>About</code>, and <code>Contact</code> components like this:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is the home page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Home;
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is the about page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> About
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Contact</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is the contact page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Contact
</code></pre>
<h3 id="heading-step-2-define-routes">Step 2 - Define routes</h3>
<p>Since the <code>App</code> component acts as the root component where our React code gets rendered from initially, we will be creating all our routes in it. </p>
<p>Don't worry if this does not make much sense – you'll understand better after looking at the example below.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { Routes, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">"./Home"</span>
<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">"./About"</span>
<span class="hljs-keyword">import</span> Contact <span class="hljs-keyword">from</span> <span class="hljs-string">"./Contact"</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span> &lt;<span class="hljs-attr">Home</span>/&gt;</span> } /&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"about"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span> &lt;<span class="hljs-attr">About</span>/&gt;</span> } /&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"contact"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span> &lt;<span class="hljs-attr">Contact</span>/&gt;</span> } /&gt;
      <span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<p>We first imported the features we'll be using – <code>Routes</code> and <code>Route</code>. After that, we imported all the components we needed to attach a route to. Now let's break down the process.</p>
<p><code>Routes</code> acts as a container/parent for all the individual routes that will be created in our app.</p>
<p><code>Route</code> is used to create a single route. It takes in two attributes: </p>
<ul>
<li><code>path</code>, which specifies the URL path of the desired component. You can call this pathname whatever you want. Above, you'll notice that the first pathname is a backslash (/). Any component whose pathname is a backslash will get rendered first whenever the app loads for the first time. This implies that the <code>Home</code> component will be the first component to get rendered.</li>
<li><code>element</code>, which specifies the component the route should render. </li>
</ul>
<p>All we have done now is define our routes and their paths, and attach them to their respective components. </p>
<p>If you are coming from version 5 then you'll notice that we're not using <code>exact</code> and <code>switch</code>, which is awesome. </p>
<h3 id="heading-step-3-use-link-to-navigate-to-routes">Step 3 - Use <code>Link</code> to navigate to routes</h3>
<p>If you have been coding along up to this point without any errors, your browser should be rendering the <code>Home</code> component. </p>
<p>We will now use a different React Router feature to navigate to other pages based on those routes and pathnames we created in the <code>App</code> component. That is: </p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is the home page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"about"</span>&gt;</span>Click to view our about page<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"contact"</span>&gt;</span>Click to view our contact page<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Home;
</code></pre>
<p>The <code>Link</code> component is similar to the anchor element (<a>) in HTML. Its <code>to</code> attribute specifies which path the link takes you to. </a></p>
<p>Recall that we created the pathnames listed in the <code>App</code> component so when you click on the link, it will look through your routes and render the component with the corresponding pathname.</p>
<p>Always remember to import <code>Link</code> from <code>react-router-dom</code> before using it.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>At this point, we have seen how to install, set up and use the basic features of React Router to navigate to different pages in your app. This pretty much covers the basics for getting started, but there are a lot more cooler features.</p>
<p>For example, you can use <code>useNavigate</code> to push users to various pages, and you can use <code>useLocation</code> to get the current URL. Alright, we won't start another tutorial at the end of the article. </p>
<p>You can check out more features in the <a target="_blank" href="https://reactrouter.com/docs/en/v6">React Router documentation</a>.</p>
<p>You can find me on Twitter <a target="_blank" href="https://twitter.com/Ihechikara2">@ihechikara2</a>. Subscribe to my <a target="_blank" href="https://www.getrevue.co/profile/The-Congregation-of-Programmers">newsletter</a> for free learning resources.</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Deploy a React Router-Based Application to Netlify ]]>
                </title>
                <description>
                    <![CDATA[ In this article, we'll learn the most popular ways of deploying a React app to Netlify. We'll also learn the configuration changes you'll need to make to deploy a Routing-based React app. The thing I love about Netlify is that it provides a lot of us... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-deploy-react-router-based-app-to-netlify/</link>
                <guid isPermaLink="false">66bc5519d94fa6cb67b844ff</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Netlify ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Yogesh Chavan ]]>
                </dc:creator>
                <pubDate>Mon, 19 Apr 2021 21:38:40 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/04/routing.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, we'll learn the most popular ways of deploying a React app to Netlify. We'll also learn the configuration changes you'll need to make to deploy a Routing-based React app.</p>
<p>The thing I love about <a target="_blank" href="https://www.netlify.com/">Netlify</a> is that it provides a lot of useful features<br>for free such as:</p>
<ul>
<li>A way to deploy a static website within seconds</li>
<li>Continuous deployment, which means when you connect your Github/Gitlab/Bitbucket repository, it automatically triggers deployment when new commits are pushed to the repository</li>
<li>Assurance that your website never goes down, even during new deployments</li>
<li>Allows you to easily rollback to any previous working version of your site with a single click</li>
<li>Lets you quickly preview any of the previously deployed versions of the app</li>
<li>Change the domain or subdomain of your site instantly </li>
</ul>
<p>and much more.</p>
<p>So let's see how to deploy a React app to Netlify.</p>
<blockquote>
<p>Want to learn Redux from the absolute beginning and build a food ordering app from scratch? Check out the <a target="_blank" href="https://master-redux.yogeshchavan.dev/">Mastering Redux</a> course.</p>
</blockquote>
<h2 id="heading-drag-and-drop-the-build-folder-in-netlify">Drag and Drop the Build Folder in Netlify</h2>
<p>The fastest and easy way to deploy a React application is just to drag and drop the build folder in Netlify.</p>
<p>To create a build folder, just execute the <code>npm run build</code> or <code>yarn build</code> command from the command line from your project folder.</p>
<p>Once the build folder is created, you just need to drop the folder in the drop area under the <code>sites</code> menu, as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/drag_drop.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-deploy-an-app-to-netlify-from-a-github-repository">How to Deploy an App to Netlify from a GitHub Repository</h2>
<p>This is my favorite way of deploying applications on Netlify.</p>
<p>Because whenever you push any changes to the GitHub repository, it will automatically be deployed to Netlify. You can also see all deployed versions and easily roll back to any previously working version of code with just a single click.</p>
<p>If you already have a repository pushed to GitHub, then you just need to connect it.</p>
<p>Login to your Netlify account. In the dashboard, click on the <code>New site from Git</code> button.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/netlify_home.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Click on the <code>GitHub</code> button to connect your GitHub repository.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/git_provider.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>It will open a new tab. Make sure the popup is enabled in your browser.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/select_repository.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Search for the GitHub repository in the <code>Search repos</code> search box. If your repository is not getting displayed then click on the <code>Configure the Netlify app on GitHub</code> button at the bottom of the page.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/configure_netlify.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Once clicked, scroll down on the page and click on the <code>Select repositories</code> dropdown and search for your repository and click on the <code>Save</code> button.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/select_repo.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You will be redirected to the previous page showing all the available repositories.</p>
<p>Search for the repository you want to deploy. For this article, I have selected the <a target="_blank" href="https://github.com/myogeshchavan97/react-book-management-app">react-book-management-app</a> repository which we created in my <a target="_blank" href="https://www.freecodecamp.org/news/react-crud-app-how-to-create-a-book-management-app-from-scratch/">previous article</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/find_repository-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Once you select the repository, you will see the following screen:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/deploy_repository.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>For this application, we don't need to change anything.</p>
<p>Your <code>Build command</code> and <code>Publish directory</code> will be automatically populated. Make sure to enter these fields if you have a different command in <code>package.json</code> to build your app or those fields are not auto-populated. </p>
<p>Now, click on the <code>Deploy site</code> button. Once clicked, you will see the <code>Site deploy in progress</code> message.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/deploying.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You'll have to wait a little bit while it's deploying. Once deployment is completed, you will see the following screen:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/deployed.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Open the link in the new tab and you will see your application deployed live.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/deployed_app.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Awesome! Now, if you make any changes in the source code and push that change to GitHub, Netlify will detect that change and re-deploy your application with your latest changes.</p>
<p>If you check the application, you will see that the application works just fine with the navigation and you're able to add/edit/delete a book.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/working_app.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>But there is one issue.</strong> If you directly access the <code>/add</code> route or refresh the <code>/add</code> route page, you will get a page not found error as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/page_not_found.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You will get the same error if you try to refresh the edit page route.</p>
<p>This is because when we access any route on our local machine, React Router handles the routing. But when we deploy the application on any server, directly accessing the route will send the request to the server itself (Netlify in our case).</p>
<p>But as there is no <code>/add</code> route handler on the server-side, you will see a page not found error. But Netlify provides a way to fix this.</p>
<p>Create a new file with the name <code>_redirects</code> inside the <code>public</code> folder of our project and add the following contents inside it:</p>
<pre><code class="lang-js"><span class="hljs-comment">/* /index.html 200</span>
</code></pre>
<p>Here, we're telling Netlify to redirect all the routes to the <code>index.html</code> file. </p>
<p>The <code>index.html</code> file contains our entire React app code. It gets generated inside the <code>build</code> folder when the <code>yarn build</code> command is executed by Netlify while deploying the app.</p>
<p>And as routing is handled by our React app which is contained in the <code>index.html</code> file, our application will work without a page not found issue.</p>
<p>Now, push the changes to the GitHub repository so Netlify will deploy the app again with these changes.</p>
<p>And once deployed, if you check the deployed application, you will see that the application works fine and we don't get a page not found error.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/no_page_not_found.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>That's it! We're all done with deploying our application to Netlify.</p>
<h2 id="heading-how-to-easily-change-a-site-name-in-netlify">How to Easily Change a Site Name in Netlify</h2>
<p>If you check the name of the deployed site you will see that it's not easy to remember, especially if you have lot of applications deployed. But Netlify provides a way to easily change that.</p>
<p>Click on the <code>Site settings</code> button displayed on the <code>Site overview</code> section.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/site_settings.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Then click on the <code>Change site name</code> button and enter a new name. Click on the <code>Save</code> button, and now you can access your application with the changed name.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/changed_site_name.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<blockquote>
<p>I usually like to give the same name as the repository name so it's easy to find a particular application if you have a lot of deployed applications on Netlify.</p>
</blockquote>
<p>If you want to know how to deploy React + Node.js application to production, check out <a target="_blank" href="https://dev.to/myogeshchavan97/how-to-deploy-react-node-js-application-to-heroku-4jb4">this article</a>.</p>
<h3 id="heading-thanks-for-reading">Thanks for reading!</h3>
<p>You can find the complete GitHub source code along with this redirection change in <a target="_blank" href="https://github.com/myogeshchavan97/netlify-react-book-management-app">this repository</a>.</p>
<p><strong>You can see the live demo of the deployed application <a target="_blank" href="https://react-book-management-app.netlify.app/">here</a>.</strong></p>
<p>Want to learn all ES6+ features in detail including let and const, promises, various promise methods, array and object destructuring, arrow functions, async/await, import and export and a whole lot more from scratch?</p>
<p>Check out my <a target="_blank" href="https://modernjavascript.yogeshchavan.dev/">Mastering Modern JavaScript</a> book. This book covers all the pre-requisites for learning React and helps you to become better at JavaScript and React.</p>
<blockquote>
<p>Check out free preview contents of the book <a target="_blank" href="https://www.freecodecamp.org/news/learn-modern-javascript/">here</a>.</p>
</blockquote>
<p>Also, you can check out my <strong>free</strong> <a target="_blank" href="https://yogeshchavan1.podia.com/react-router-introduction">Introduction to React Router</a> course to learn React Router from scratch.</p>
<p>Want to stay up to date with regular content regarding JavaScript, React, Node.js? <a target="_blank" href="https://www.linkedin.com/in/yogesh-chavan97/">Follow me on LinkedIn</a>.</p>
<p><a href="https://bit.ly/3w0DGum" target="_blank"><img src="https://gist.github.com/myogeshchavan97/98ae4f4ead57fde8d47fcf7641220b72/raw/c3e4265df4396d639a7938a83bffd570130483b1/banner.jpg" width="600" height="400" alt="banner" loading="lazy"></a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The React Router Cheatsheet – Everything You Need to Know ]]>
                </title>
                <description>
                    <![CDATA[ If you're building React applications for the web, you're going to need to use a dedicated router to display pages and navigate your user around them.  That's why today we're going to go over the most popular and most powerful router for React applic... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-router-cheatsheet/</link>
                <guid isPermaLink="false">66d037e12b211a17e00e3708</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Reed ]]>
                </dc:creator>
                <pubDate>Mon, 19 Apr 2021 14:52:33 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/04/the-react-router-cheatsheet.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're building React applications for the web, you're going to need to use a dedicated router to display pages and navigate your user around them. </p>
<p>That's why today we're going to go over the most popular and most powerful router for React applications – React Router. </p>
<p>We're going to go over 11 of the essential features you need to know if you're using React Router in your projects today, specifically for the web using the package <code>react-router-dom</code>.</p>
<h2 id="heading-want-your-own-copy">Want Your Own Copy?‬ 📄</h2>
<p><strong><a target="_blank" href="https://reedbarger.com/resources/react-router-cheatsheet-2021">Click here to download the cheatsheet in PDF format</a></strong> (it takes 5 seconds).</p>
<p>It includes all of the essential information here as a convenient PDF guide.</p>
<h2 id="heading-install-react-router">Install React Router</h2>
<p>The very first step to using React Router is to install the appropriate package. </p>
<p>They are technically three different packages: React Router, React Router DOM, and React Router Native. </p>
<p>The primary difference between them lies in their usage. React Router DOM is for web applications and React Router Native is for mobile applications made with React Native.</p>
<p>The first thing that you'll need to do is install React Router DOM using npm (or yarn): </p>
<pre><code class="lang-bash">npm install react-router-dom
</code></pre>
<h2 id="heading-basic-react-router-setup">Basic React Router Setup</h2>
<p>Once it's installed, we can bring in our first component which is required to use React router which is called BrowserRouter. </p>
<p>Note that there are multiple types of routers that <code>react-router-dom</code> provides aside from BrowserRouter which we won't go into. It's a common practice to alias (rename) BrowserRoute as simply 'Router' when it is imported.</p>
<p>If we want to provide routes within our entire application it needs to be wrapped around our entire component tree. That's why you will usually see it wrapped around or within the main app component:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      {/* routes go here, as children */}
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>This is the primary function of the BrowserRouter: to be able to declare individual routes within our application. </p>
<p>Note that any router-specific data cannot be accessed outside of the Router component. For example, we cannot access history data outside of the router (that is, with the <code>useHistory</code> hook) and we cannot create a Route outside of a Router component.</p>
<h2 id="heading-route-component">Route Component</h2>
<p>The next component is the Route component. </p>
<p>We declare routes within the Router component as children. We can declare as many routes as we like and we need to provide at least two props to each route, <code>path</code> and <code>component</code> (or <code>render</code>):</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>about<span class="hljs-tag">&lt;/&gt;</span></span>   
}
</code></pre>
<p>The <code>path</code> prop specifies on what path of our app a given route is located. </p>
<p>For an about page, for example, we might want that route to be accessible on the path '/about'. </p>
<p>The <code>render</code> or <code>component</code> props are used to display a specific component for our path.</p>
<p>The <code>component</code> props can only receive a reference to a given component, whereas <code>render</code> is more typically used for applying some conditional logic to render one component or another. For render you can either use a reference to a component, or use a function:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>home<span class="hljs-tag">&lt;/&gt;</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>about<span class="hljs-tag">&lt;/&gt;</span></span>;
}
</code></pre>
<p>It's worth noting that you can potentially drop the <code>render</code> or <code>component</code> prop entirely and use the component that you want to associate with a given route as a child of Route:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">About</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>Finally, if want a component (such as a navbar) to be visible on every page, put it still within the browser router, but above (or below) the declared routes: </p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// visible on every page</span>
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>navbar<span class="hljs-tag">&lt;/&gt;</span></span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>home<span class="hljs-tag">&lt;/&gt;</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>about<span class="hljs-tag">&lt;/&gt;</span></span>;
}
</code></pre>
<h2 id="heading-switch-component">Switch Component</h2>
<p>When we begin to add multiple routes, we'll notice something strange.</p>
<p>Let's say we have a route for the home page and about page. Even though we specify two different paths, '/' and '/about', when I visit the about page we'll see both the home and the about components.</p>
<p>We can fix this with the exact prop, on the home route to make sure that our router matches exactly the path '/' instead of '/about':</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Switch, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>When it comes to switching between different routes that our router should show, there is in fact a dedicated component that you should be using if you have multiple routes within your router and that is the Switch component. </p>
<p>The switch component should be included within the router and we can place all of our routes within it: </p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Switch, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>The switch component looks through all of its child routes and it displays the first one whose path matches the current URL. </p>
<p>This component is what we want to use in most cases for most applications, because we have multiple routes and multiple plate pages in our app but we only want to show one page at a time. </p>
<p>If for some reason you do want multiple pages to be displayed at the same time, you might consider not using the switch component.</p>
<h2 id="heading-404-route">404 Route</h2>
<p>If we attempt to go to a path that doesn't exist in our application, what are we going to see?</p>
<p>We're not going to see anything if we don't have a route corresponding to that. How do we make a catch-all route? </p>
<p>If a user attempts to go to a page for which we don't have a defined route, we can create a route and then set the path to an asterisk *:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Switch, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"*"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{NotFound}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">NotFound</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>You have landed on a page that doesn't exist<span class="hljs-tag">&lt;/&gt;</span></span>;
}
</code></pre>
<p>This will match any attempt to visit a page that doesn't exist and we can connect it to a not found component to tell our users that they have "landed on a page that doesn't exist."</p>
<h2 id="heading-link-component">Link Component</h2>
<p>Let's say that within our NavBar, we actually want to create some links so we can move around our application more easily instead of having to change the URL manually in the browser. </p>
<p>We can do so with another special component from React Router DOM called the Link component. It accepts the <code>to</code> prop, which specifies where we want the link to navigate our user to. In our case, we might have a home and about link:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Switch, Route, Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
  )
}
</code></pre>
<p>The link component allows us to provide some inline styles just like any standard React component. It also gives us a helpful <code>component</code> prop, so we can set our link as our own custom component for even easier styling.</p>
<h2 id="heading-navlink-component">NavLink Component</h2>
<p>Additionally, React Router DOM gives us a NavLink component which is helpful in the event that we want to apply some special styles. </p>
<p>If we are on the current path that the link points to, this allows us to create some active link styles to tell our users, by looking at our link, what page they're on. </p>
<p>For example, if our users are on the homepage, we could tell them as much by using the <code>activeStyle</code> prop to make our link bold and red when they're on the homepage:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  BrowserRouter <span class="hljs-keyword">as</span> Router,
  Switch,
  Route,
  NavLink
} <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span>
        <span class="hljs-attr">activeStyle</span>=<span class="hljs-string">{{</span>
          <span class="hljs-attr">fontWeight:</span> "<span class="hljs-attr">bold</span>",
          <span class="hljs-attr">color:</span> "<span class="hljs-attr">red</span>"
        }}
        <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>
      &gt;</span>
        Home
      <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">activeClassName</span>=<span class="hljs-string">"active"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>
        About
      <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
  );
}
</code></pre>
<p>There is also an <code>activeClassName</code> prop which can be set as well if you do not want to include inline styles or want more reusable styles to perform the same function as <code>activeStyle</code>.</p>
<h2 id="heading-redirect-component">Redirect Component</h2>
<p>Another very helpful component that React Router DOM gives us is the redirect component.</p>
<p>This may seem strange to have a component that performs a function of redirecting our user when it's displayed, but this is very functional. </p>
<p>Whenever we're using something like a private route and we have a condition in which the user is not authenticated, we want to redirect them back to the login page. </p>
<p>Here is an example of an implementation of a private route component that ensures that a user is authenticated before it shows them a particular route that has been declared with this component. </p>
<p>Otherwise, if they're not authenticated, they will be redirected to a public route (presumably a route to login) once the redirect component is displayed:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  BrowserRouter <span class="hljs-keyword">as</span> Router,
  Switch,
  Route,
  Redirect
} <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">PrivateRoute</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/hidden"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Hidden}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">PrivateRoute</span>(<span class="hljs-params">{ component: Component, ...rest }</span>) </span>{
  <span class="hljs-comment">// useAuth is some custom hook to get the current user's auth state</span>
  <span class="hljs-keyword">const</span> isAuth = useAuth();

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>
      {<span class="hljs-attr">...rest</span>}
      <span class="hljs-attr">render</span>=<span class="hljs-string">{(props)</span> =&gt;</span>
        isAuth ? <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> {<span class="hljs-attr">...props</span>} /&gt;</span> : <span class="hljs-tag">&lt;<span class="hljs-name">Redirect</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> /&gt;</span>
      }
    /&gt;</span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>home<span class="hljs-tag">&lt;/&gt;</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Hidden</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>hidden<span class="hljs-tag">&lt;/&gt;</span></span>;
}
</code></pre>
<p>The redirect component is very simple to use, very declarative, and allows us to see the great benefit of React Router DOM being component-based, just like everything in React.</p>
<h2 id="heading-usehistory-hook">useHistory Hook</h2>
<p>On top of all of these powerful components, there are some very useful hooks that React Router DOM gives us. </p>
<p>They are mainly helpful by supplying additional information that we can use within our components. They can be called as normal React hooks for which we can use their values exactly as we like. </p>
<p>Perhaps the most powerful hook is the <code>useHistory</code> hook. We can call it up at the top of any component that is declared within our router component and get back <code>history</code> data, which includes information such as the location associated with our component. </p>
<p>This tells us all about where the user currently is, such as the pathname that they're on, as well as any query parameters that might be appended to our URL. All of the location data is accessible from <code>history.location</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useHistory } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> history = useHistory();

  <span class="hljs-built_in">console</span>.log(history.location.pathname); <span class="hljs-comment">// '/about'</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>The about page is on: {history.location.pathname}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>Additionally, the history object directly includes helpful methods that allows us to programmatically direct our user to different pages in our app. </p>
<p>This is very helpful, for example, for redirecting our user after logging in, or in any situation where we need to take a user from one page to another. </p>
<p>We can push users from one page to another using <code>history.push</code>. When we use the push method, we just need to supply the path that we want to take our users to using this method. It adds this new page on to the stack (so to speak) of our history:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useHistory } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> history = useHistory();

  <span class="hljs-built_in">console</span>.log(history.location.pathname); <span class="hljs-comment">// '/about'</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>The about page is on: {history.location.pathname}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> history.push('/')}&gt;Go to home page<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>We can also redirect our users with <code>history.replace</code>, which also accepts a path value, but clears out everything in history, after the navigation is performed. This is helpful for situations where going back in history is no longer needed, such as after users have been logged out.</p>
<h2 id="heading-uselocation-hook">useLocation Hook</h2>
<p>The <code>useLocation</code> hook includes all of the same information that the <code>useHistory</code> hook does. </p>
<p>It is important to note that if you need both location data and to use history to programmatically navigate your user, make sure to useHistory. However, if you only want location data, all you need to do is call useLocation or get back all of the location data on an object that is identical to the data provided on <code>history. location</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useLocation } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> location = useLocation();

  <span class="hljs-built_in">console</span>.log(location.pathname); <span class="hljs-comment">// '/about'</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>The about page is on: {location.pathname}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-useparams-hook-dynamic-routes">useParams Hook + Dynamic Routes</h2>
<p>One thing that we didn't cover when it comes to routes is that we can naturally create dynamic routes. This means routes that are not fixed and determined, but can be any number of characters. </p>
<p>Dynamic routes are helpful in situations where we have, let's say, a blog post with a unique slug. How do we make sure that we display the appropriate data and appropriate components, given that our blog post slug can be completely different?</p>
<p>To declare a route parameter on a given route, it must be prefixed with a colon <code>:</code>. If I wanted to create a dynamic route, "/blog/:postSlug", for a blog post component, it might look like the following:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Switch, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/blog/:postSlug"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{BlogPost}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>home<span class="hljs-tag">&lt;/&gt;</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">BlogPost</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>blog post<span class="hljs-tag">&lt;/&gt;</span></span>;
}
</code></pre>
<p>We're now matching the appropriate component or whatever the slug is. But within our BlogPost component, how do we receive that post slug data? </p>
<p>We can access any route params of a declared route with its associated component using the <code>useParams</code> hook. </p>
<p>useParams will return an object which will contain properties that match our route params (in this case, <code>postSlug</code>). We can use object destructuring to immediately access and declare as a variable with the name <code>postSlug</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Switch, Route, useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/blog/:postSlug"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{BlogPost}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>home<span class="hljs-tag">&lt;/&gt;</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">BlogPost</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [post, setPost] = React.useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> { postSlug } = useParams();

  React.useEffect(<span class="hljs-function">() =&gt;</span> {
    fetch(<span class="hljs-string">`https://jsonplaceholder.typicode.com/posts/<span class="hljs-subst">${postSlug}</span>`</span>)
      .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json())
      .then(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> setPost(data));
  }, [postSlug]);

  <span class="hljs-keyword">if</span> (!post) <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{post.description}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>If we go to the route '/blog/my-blog-post', I can access the string 'my-blog-post' on the <code>postSlug</code> variable and fetch that post's associated data within useEffect.</p>
<h2 id="heading-useroutematch-hook">useRouteMatch Hook</h2>
<p>If we want to know whether the given component is on a certain page, we can use the <code>useRouteMatch</code> hook. </p>
<p>For example, within our blog post, to see if the page that we're on matches the route "/blog/:postSlug", we can get back a boolean value that will tell us if the route that we're on matches the pattern that we specified:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useRouteMatch } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">BlogPost</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> isBlogPostRoute = useRouteMatch(<span class="hljs-string">"/blog/:postSlug"</span>);

  <span class="hljs-comment">// display, hide content, or do something else</span>
}
</code></pre>
<p>This is helpful in conditions in which we want to show something specific, based off of whether we're on a certain route or not.</p>
<h2 id="heading-want-to-keep-this-guide-for-future-reference">Want to keep this guide for future reference?‬</h2>
<p><strong><a target="_blank" href="https://reedbarger.com/resources/react-router-cheatsheet-2021">Click here to download the cheatsheet as a helpful PDF</a>.</strong></p>
<p>Here are 3 quick wins you get when you grab the downloadable version:</p>
<ul>
<li>You’ll get tons of copyable code snippets for easy reuse in your own projects.</li>
<li>It is a great reference guide to strengthen your skills as a React developer and for job interviews.</li>
<li>You can take, use, print, read, and re-read this guide literally anywhere that you like.</li>
</ul>
<h2 id="heading-become-a-professional-react-developer">Become a Professional React Developer</h2>
<p>React is hard. You shouldn't have to figure it out yourself.</p>
<p>I've put everything I know about React into a single course, to help you reach your goals in record time:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><strong>Introducing: The React Bootcamp</strong></a></p>
<p><strong>It’s the one course I wish I had when I started learning React.</strong></p>
<p>Click below to try the React Bootcamp for yourself:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><img src="https://reedbarger.nyc3.digitaloceanspaces.com/reactbootcamp/react-bootcamp-cta-alt.png" alt="Click to join the React Bootcamp" width="600" height="400" loading="lazy"></a>
<em>Click to get started</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Deploy a Routed React App to GitHub Pages ]]>
                </title>
                <description>
                    <![CDATA[ When we build projects, we want to showcase them online. Instead of buying a domain and taking the time to configure it, it's easier just to host it using GitHub Pages.  A project that just uses JavaScript, HTML and CSS is simple to host on GitHub Pa... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/deploy-a-react-app-to-github-pages/</link>
                <guid isPermaLink="false">66ba4fe98e44e0cdf128124a</guid>
                
                    <category>
                        <![CDATA[ github pages ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tomer ]]>
                </dc:creator>
                <pubDate>Mon, 22 Feb 2021 22:53:34 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/602aaa1e0a2838549dcc5c67.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When we build projects, we want to showcase them online. Instead of buying a domain and taking the time to configure it, it's easier just to host it using <a target="_blank" href="https://pages.github.com/">GitHub Pages</a>. </p>
<p>A project that just uses JavaScript, HTML and CSS is simple to host on GitHub Pages. Projects that are built in React, Vue or Angular require some configurations, though. This gives anyone who visits your application online the same experience you have when you build the application locally.</p>
<p>In this article, I'll show you how to create a simple React application that uses routing and then we'll learn how to upload it to GitHub Pages. We will give special attention to the routing part since it is important to understand and implement.</p>
<blockquote>
<p>⚠️ This article assumes you have some knowledge of React and Git.</p>
</blockquote>
<h3 id="heading-prerequisites">Prerequisites</h3>
<p>You need to have Node, yarn and npm installed on your machine. To check if they are installed, open up a terminal window and type the following:</p>
<pre><code class="lang-bash">npm -v
yarn -v
node -v
</code></pre>
<p>If these commands print out a version number in the terminal, you are good to go. If not, you need to go ahead and install what is missing.</p>
<ul>
<li><a target="_blank" href="https://nodejs.org/en/download/">Node</a> (contains npm)</li>
<li><a target="_blank" href="https://classic.yarnpkg.com/en/docs/install/#windows-stable">Yarn</a></li>
</ul>
<p>We will also need to create a repository on GitHub. Head over to your account and create a new repository. Choose whichever name you deem fit for this project, but I will go with <strong>starter-project</strong> for the rest of this article.</p>
<p>To create our project, we will be using <strong>create-react-app</strong>. It is a package that lets you create a single page application with ease. To create a project, you need to type the following in the terminal:</p>
<pre><code class="lang-bash">npx create-react-app starter-project
</code></pre>
<p>Once the operation finishes, you will have a boilerplate React project, ready to go. To see if it works properly, head into the directory of the project (in our example it would be starter-project) and run the command:</p>
<pre><code class="lang-bash">yarn start
</code></pre>
<p>If everything runs properly, you will see a message in the terminal that says that your application is running on a local server at this address: <strong>http://localhost:3000</strong></p>
<p>If you head over there in your browser, you will see this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/IB8uRE3cjN.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-deploy-your-project-to-github">How to Deploy Your Project to GitHub</h2>
<p>You may have noticed that we have not created any repository in GitHub. So before we move on, we must have our project uploaded there. Head over to your GitHub account and create a repository with the same name as the React project. </p>
<blockquote>
<p>☝️ Make sure to mark your repository as public. If you mark it as private, you won't be able to use GitHub Pages.</p>
</blockquote>
<p>We are going to add this repository as a remote to our project. To do that, in the terminal, type:</p>
<pre><code class="lang-bash">git remote add &lt;name-of-remote&gt; &lt;url-of-repository&gt;
</code></pre>
<p>So, in our case, the command looks like this:</p>
<pre><code class="lang-bash">git remote add origin https://github.com/TomerPacific/starter-project
</code></pre>
<blockquote>
<p>It's important to call the remote <strong><em>origin</em></strong> as it will be used in our deploy process.</p>
</blockquote>
<p>After executing the command above, we can't push our code yet. First, we need to configure an upstream branch and set the remote as origin.</p>
<pre><code class="lang-bash"> git push --set-upstream origin master
</code></pre>
<p>Now, we can push all our project's files to our repository.</p>
<p>In order for us to be able to upload our built application to GitHub Pages, we first need to install the <a target="_blank" href="https://www.npmjs.com/package/gh-pages">gh-pages package</a>.</p>
<pre><code class="lang-bash">yarn add gh-pages
</code></pre>
<p>This package will help us to deploy our code to the gh-pages branch which will be used to host our application on GitHub Pages. </p>
<p>To allow us to use the gh-pages package properly, we need to add two keys to our scripts value in the package.json file:</p>
<pre><code class="lang-package.json">"scripts": {
    "start": "react-scripts start",
    "predeploy": "npm run build", &lt;----------- #1
    "deploy": "gh-pages -d build", &lt;---------- #2
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
</code></pre>
<p>Next, we need to modify our package.json file by adding the <strong>homepage</strong> field. This field is used by React to figure out the root URL in the built HTML file. In it, we will put the URL of our GitHub repository.</p>
<pre><code class="lang-package.json">{
  "name": "starter-project",
  "homepage": "https://tomerpacific.github.io/starter-project/", &lt;----
  "version": "0.1.0",
  /....
}
</code></pre>
<p>To deploy our application, type the following in the terminal:</p>
<pre><code class="lang-bash">npm run deploy
</code></pre>
<p>Running the command above takes care of building your application and pushing it to a branch called gh-pages, which GitHub uses to link with GitHub Pages.</p>
<blockquote>
<p>🚧 If you did not name your remote <strong><em>origin</em></strong>, you will get an error during this phase stating that: <strong>Failed to get remote.origin.url (task must either be run in a git repository with a configured origin remote or must be configured with the "repo" option)</strong>. </p>
</blockquote>
<p>You will know that the process was successful if at the end of it you see the word <strong>Published</strong>. We can now head to our GitHub repository under Settings and scroll down to the GitHub Pages section.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/chrome_egdTtIso1X.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you see a message similar to the one above, it means your application is now hosted successfully on GitHub Pages.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/image-308.png" alt="Image" width="600" height="400" loading="lazy">
_Photo by [Unsplash](https://unsplash.com/@noahglynn?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit"&gt;Noah Glynn / &lt;a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm<em>campaign=api-credit)</em></p>
<h2 id="heading-routing-in-react">Routing in React</h2>
<p>So far - so good:</p>
<ol>
<li>We have a basic React application that is hosted on GitHub Pages</li>
<li>We also have a streamlined process to deploy it when we want to make changes</li>
</ol>
<p>But since the purpose of this article is to show a more complex application than the one we initially created, we will be discussing routing. </p>
<p>One component that is missing from out application is navigation. Our application won't just be one page, it will probably have many pages. So, how will users be able to navigate between them?</p>
<p>Routing is the practice of selecting a path for traffic in a network. Or in more basic terms, what happens when you click on a link inside of a webpage and where you get redirected. </p>
<p>React is a library, and it does not contain everything you need for your application out of the box (in our case, routing). Therefore, we will need to install <a target="_blank" href="https://reactrouter.com/web/guides/quick-start">react router</a>. </p>
<p>React router has different components for web applications and for native ones. Since we are building a web application, we will be using <strong>react-router-dom</strong>.</p>
<pre><code class="lang-bash">yarn add react-router-dom
</code></pre>
<p>To make use of routing in our application, let's create a navigation element which will be visible at the top of the application. We will be adding this inside our App.js file and replacing the current HTML markup that is there.</p>
<pre><code class="lang-html"> <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"navigation"</span>&gt;</span>
             <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                 <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
             <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
             <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                 <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
             <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
             <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                 <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
             <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Usually, in a non React project, we would put a relative path to our HTML pages for each section. That way, the browser knows where to load the data from. </p>
<p>But in our project, we won't have different HTML pages for each section. We will just load a different component. The markup that used to be inside of App.js will now be found inside of a component called Home.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-header"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-logo"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                    Edit <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>src/App.js<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span> and save to reload.
                <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span>
                    <span class="hljs-attr">className</span>=<span class="hljs-string">"App-link"</span>
                    <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span>
                    <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>
                    <span class="hljs-attr">rel</span>=<span class="hljs-string">"noopener noreferrer"</span>
                &gt;</span>
                    Learn React
                <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>

        );
    }

}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Home;
</code></pre>
<p>As we have created three sections in our navgiation and taken care of the home section, let's give another example with the <strong>About</strong> section.</p>
<p>We'll create a new file called <strong>About.jsx</strong> that will hold our template and code for the about section.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">const</span> divStyle = {
    <span class="hljs-attr">color</span>:<span class="hljs-string">'white'</span>
};

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">About</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{

    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{divStyle}</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>About Page<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This section contains information about...<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}



<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> About;
</code></pre>
<p>You may be asking yourself, how will the application know to redirect the user once they click on the about link? For that we will use a component called <strong>Route</strong>. </p>
<p>The Route is one of the most important components in react-router because it lets you render different component based on the path of the URL. For our project, we will use the code below inside of App.js just below the navigation markup.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">About</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
</code></pre>
<p>You can see that we created two routes for home and about. The Switch component lets us group route components together and it will only match one of them.</p>
<p>Our combined App.js file looks like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Route, Switch, Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">'./About'</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./Home'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
      <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"navigation"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">About</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
            );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>One last thing we should do is wrap our entire project in a Router component. We need to do this because it enables us to use routing in our application. We will be using the BrowserRouter component as it uses HTML5's history API.</p>
<pre><code class="lang-html">ReactDOM.render(
  <span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span>,
  document.getElementById('root')
);
</code></pre>
<p>If we run things locally, everything seems to work. Let's deploy our augmented project to GitHub Pages and see what the result is.</p>
<h2 id="heading-how-to-handle-routing-using-hashrouter">How to Handle Routing Using HashRouter</h2>
<p>At first glance, everything seems to be working fine. But when you try refreshing the page or navigating through the browser itself, you'll keep getting 404 errors. </p>
<p>Why does this happen? Because GitHub Pages does not support browser history like your browser does. In our case, the route <strong>https://tomerpacific.github.io/starter-project/about</strong> doesn't help GitHub Pages understand where to point the user (since it is a frontend route). </p>
<p>To overcome this problem, we need to use a Hash Router instead of a Browser Router in our application. This type of router uses the hash portion of the URL to keep the UI in sync with the URL.</p>
<pre><code class="lang-html">ReactDOM.render(
  <span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">HashRouter</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">HashRouter</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span>,
  document.getElementById('root')
);
</code></pre>
<p>You can read more about this <a target="_blank" href="https://create-react-app.dev/docs/deployment/#github-pages-https-pagesgithubcom">here</a>.</p>
<p>Deploy your application again and you'll be satisfied with the result. No more 404 errors.</p>
<p>This article was inspired by working on a project of mine. You can view it below:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://tomerpacific.github.io/julOnSaleReact/">https://tomerpacific.github.io/julOnSaleReact/</a></div>
<p>And you can see the source code here:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/TomerPacific/julOnSaleReact">https://github.com/TomerPacific/julOnSaleReact</a></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Learn React in 2021: The 7 Skills You Need To Know ]]>
                </title>
                <description>
                    <![CDATA[ You want to learn React_—_the most in-demand JavaScript library in the world. But what steps do you need to take to get there? Let's walk through the seven skills you should learn to become a professional React developer. As you're piecing together y... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-learn-react-skills-you-need-to-know/</link>
                <guid isPermaLink="false">66d0378415ea3036a9539941</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React context ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react hooks ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Reed ]]>
                </dc:creator>
                <pubDate>Wed, 06 Jan 2021 16:01:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/01/how-to-learn-react-2021-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>You want to learn React_—_the most in-demand JavaScript library in the world. But what steps do you need to take to get there? Let's walk through the seven skills you should learn to become a professional React developer.</p>
<p>As you're piecing together your React learning path, it's easy to feel overwhelmed and say, <em>"It's impossible to learn it all. There are too many things I need to learn!"</em></p>
<p><strong>To be successful with React, don't attempt to learn everything. Focus on learning the right things.</strong> </p>
<p>Let's break down the seven key skills that you need to focus on to build impressive applications and become an in-demand React developer.</p>
<h2 id="heading-step-1-become-confident-with-the-core-fundamentals-of-html-css-and-javascript">Step 1: Become confident with the core fundamentals of HTML, CSS, and JavaScript</h2>
<p>The first step in learning React is actually somewhat of a step backwards.</p>
<p>The building blocks of the web and every single webpage are HTML, CSS, and JavaScript. Any good React developer should know how to use them. React builds upon them to help you create apps that run on the web.</p>
<p>If you’ve built something with HTML, CSS and JavaScript already, you’re in a good place. For example, if you’ve built a complete landing page or a small website.</p>
<p>Out of the three technologies, <strong>React is most connected with JavaScript</strong>. React <em>is</em> JavaScript, with some added features. For that reason, you’ll need to have solid JavaScript skills.</p>
<h3 id="heading-what-javascript-concepts-should-you-know-for-react">What JavaScript concepts should you know for React?</h3>
<ul>
<li>Basic data structures: variables, objects, and arrays</li>
<li>Array methods and working with array data: <code>.map()</code>, <code>.filter()</code>, and <code>.reduce()</code></li>
<li>Be very familiar with functions and a little bit with classes</li>
<li>Asynchronous JavaScript: promises, making HTTP requests with Fetch API, async/await syntax can help</li>
<li>The DOM: learn to create, select and modify HTML elements as well as their attributes</li>
<li>Object and array destructuring are helpful for working with data</li>
</ul>
<p>Many developers claim that you should know "ES6/ES7/ES8/ESNext JavaScript" (in other words, the latest JavaScript features) to better learn React. Knowing more JavaScript can help, but new features can also serve as a distraction for new learners. </p>
<p>First, become confident with the JavaScript concepts I've listed above by building some small projects that require JavaScript. After that, you can take a look at some newer features of the language.</p>
<h2 id="heading-step-2-learn-react-fundamentals-and-react-hooks">Step 2: Learn React fundamentals and React hooks</h2>
<p>Once you’re confident with JavaScript, you’re ready to learn React and its core concepts. </p>
<p>These fundamentals are all React-specific and they exist to help us build applications with React, using patterns that JavaScript itself does not have.</p>
<h3 id="heading-what-react-fundamentals-do-you-need-to-know">What React fundamentals do you need to know?</h3>
<ul>
<li>How to structure JSX elements (and how it differs from plain HTML)</li>
<li>How to render (show) JSX elements and how to show or hide elements based on certain conditions</li>
<li>How to split JSX into components and how to reuse and organize those components to make our app interface.</li>
<li>How to pass data (+ JSX elements and components) to components using props and when to do so</li>
<li>How to store and update data within components using state and how to "lift state up" to other components</li>
<li>How to use event data in React and handle events from <code>onClick</code>, <code>onChange</code>, and <code>onSubmit</code> events (i.e. events from buttons, inputs, and forms)</li>
</ul>
<p>As a 7-year old library, React’s features have changed over time. A question I’m often asked is <strong>what should you learn first: the old or the new syntax?</strong> At the end of 2018, React received a large update that included features called React Hooks.</p>
<p>Hooks were a great improvement. They made React apps simpler, more powerful, and allow us to write less code. What’s important for you to know is the five core React hooks.</p>
<h3 id="heading-what-five-react-hooks-do-you-need-to-know-most-of-all">What five React Hooks do you need to know most of all?</h3>
<ul>
<li><code>useState</code>: to store and manage data within individual components</li>
<li><code>useEffect</code>: to perform actions like HTTP requests or working with a browser API</li>
<li><code>useRef</code>: to reference JSX elements</li>
<li><code>useContext</code>: to access data from React Context to share data among components more easily [rather than passing props]</li>
<li><code>useReducer</code>: to store and manage data across multiple components</li>
</ul>
<p>There are more hooks than these 5, but the others are not needed as often. So yes, you should learn the latest React features. Jump into using hooks right away. They will be the basis of every React app you make.</p>
<h2 id="heading-step-3-learn-to-fetch-data-from-both-rest-and-graphql-apis">Step 3: Learn to fetch data from both REST and GraphQL APIs</h2>
<p>A React app is the frontend of a complete application. In every application, you will likely have both a React frontend, which the user interacts with as well as a backend that our React code interacts with. The backend is where we get our data from and do other things like authenticate our users.</p>
<p>There are two ways of working with data from a backend. The standard way of getting data is from what’s called a REST API. <strong>REST APIs</strong> are the most common form of API. And the newer way is from what’s called a <strong>GraphQL API</strong>. </p>
<p>You’ll encounter both types of API in your work, so become familiar with using React to interact with both.</p>
<h2 id="heading-step-4-learn-to-style-your-react-apps-with-a-component-library-or-utility-class-library">Step 4: Learn to style your React apps with a component library or utility class library</h2>
<p>Every React app needs styling for its appearance. One choice is to use plain CSS. You can write your own styles and put them in a separate style sheet.</p>
<p>But besides CSS, many React developers use what’s known as a component library for easier styling. <strong>A component library</strong> gives us reusable components that have their own pre-made styles. The most popular component library for React is <strong>Material UI</strong>. But there are many others you can choose from.</p>
<p>Developers also use tools that provide pre-made class names called utility class libraries. Unlike a component library, <strong>a utility class library</strong> comes with pre-made classes to style your elements. </p>
<p>You can style your app by applying classnames to each element. This removes the need to write any styles yourself. The most popular utility class library is <strong>Tailwind CSS</strong>.</p>
<p>You’ll encounter both of these as a developer, so become familiar with both a component library and a utility class library.</p>
<h2 id="heading-step-5-manage-state-in-react-with-react-context">Step 5: Manage state in React with React context</h2>
<p><strong>What is state management?</strong> It’s the process of deciding where to locate data and how to work with it across our app. To manage state across your app’s components, use React Context. </p>
<p><strong>React Context</strong> is a tool that’s built into the core React library and allows us to pass data across our app's components without using props. It helps us solve the problem of prop-drilling which involves passing props (data) down to components that are deeply nested into one another. </p>
<p>With React Context, we place a value on the context we create and then access it using the <code>useContext()</code> React hook.</p>
<p><strong>What about Redux?</strong> Redux is a popular library for managing state in React apps. It is a far more complex tool than you need for most apps you’ll build. While Redux is still relied upon for very large applications, React Context will be enough for any app you make.</p>
<p>Plus you can get many of the benefits of Redux by combining React Hooks and React Context. This is a great advantage compared to learning an extra library.</p>
<h2 id="heading-step-6-learn-a-react-router-in-particular-react-router-dom">Step 6: Learn a React router. In particular, react-router-dom</h2>
<p><strong>What is a router?</strong> Any website we visit has many pages we can browse by going forward or backward in our browser’s history. To create these different pages or routes in React, we need to use what’s called a router.</p>
<p>React itself does not come with its own router, so we’re going to need to use a third party library, one called react-router-dom. </p>
<p><strong>react-router-dom</strong> is necessary for creating pages in our app, as well as navigating the user around each page. It lets us create links to different pages of our app and navigate to them or redirect them to other pages if we need.</p>
<h3 id="heading-what-features-of-react-router-dom-do-you-need-to-know">What features of react-router-dom do you need to know?</h3>
<ul>
<li>How to create app routes using the <code>&lt;Route /&gt;</code>, <code>&lt;Switch /&gt;</code>, and <code>&lt;BrowserRouter /&gt;</code> components</li>
<li>How to navigate users to different pages using the <code>&lt;Link /&gt;</code> component and programmatically using the <code>useHistory()</code> hook</li>
<li>How to create dynamic routes using the path prop (i.e. <code>&lt;Route path="/posts/:post-slug" component={Post} /&gt;</code>) and get the path's value using the <code>useParams()</code> hook</li>
<li>How to redirect users from protected content using the <code>&lt;Redirect /&gt;</code> component (see number 7)</li>
</ul>
<h2 id="heading-step-7-learn-patterns-for-authentication-in-react">Step 7: Learn patterns for authentication in React</h2>
<p>Authenticated users are people who have signed in to use our app. And we want to give those users access to different things. For that reason, authentication goes hand in hand with routing. This is because authenticated users should be able to see certain pages that unauthenticated users cannot.</p>
<h3 id="heading-what-do-you-need-to-know-about-authentication-as-a-react-developer">What do you need to know about authentication as a React developer?</h3>
<p>There is one main thing. You should learn how to show certain content to authenticated users and hide that content from unauthenticated ones.</p>
<p>In review, these are all the core skills you need to have as a React developer, capable of building complete applications and working as a hired frontend developer.</p>
<p>Beyond these 7 skills, note that there are many that can deepen your understanding as a developer. For example, learning more about browsers, HTTP, and APIs, to name a few. But if you follow all these steps, you’ll have a solid understanding of React and be able to build applications of any scale.</p>
<h2 id="heading-become-a-professional-react-developer">Become a Professional React Developer</h2>
<p>React is hard. You shouldn't have to figure it out yourself.</p>
<p>I've put everything I know about React into a single course, to help you reach your goals in record time:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><strong>Introducing: The React Bootcamp</strong></a></p>
<p><strong>It’s the one course I wish I had when I started learning React.</strong></p>
<p>Click below to try the React Bootcamp for yourself:</p>
<p><a target="_blank" href="https://www.thereactbootcamp.com"><img src="https://reedbarger.nyc3.digitaloceanspaces.com/reactbootcamp/react-bootcamp-cta-alt.png" alt="Click to join the React Bootcamp" width="600" height="400" loading="lazy"></a>
<em>Click to get started</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Router Tutorial – How to Render, Redirect, Switch, Link, and More, With Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ By Vijit Ail If you have just started with React, you are probably still wrapping your head around the whole Single Page Application concept.  Traditionally routing works like this: let's say you type in /contact in the URL. The browser will make a G... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-router-tutorial/</link>
                <guid isPermaLink="false">66d461773dce891ac3a9683c</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[  Single Page Applications  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 26 May 2020 17:55:23 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9ad1740569d1a4ca27f2.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vijit Ail</p>
<p>If you have just started with React, you are probably still wrapping your head around the whole Single Page Application concept. </p>
<p>Traditionally routing works like this: let's say you type in <code>/contact</code> in the URL. The browser will make a GET request to the server, and the server will return an HTML page as the response. </p>
<p>But, with the new Single Page Application paradigm, all the URL requests are served using the client-side code.</p>
<p>Applying this in the context of React, each page will be a React component. React-Router matches the URL and loads up the component for that particular page. </p>
<p>Everything happens so fast, and seamlessly, that the user gets a native app-like experience on the browser. There is no flashy blank page in between route transitions.</p>
<p>In this article, you'll learn how to use React-Router and its components to create a Single Page Application. So open up your favorite text editor, and let's get started.</p>
<h2 id="heading-setup-the-project">Setup the project</h2>
<p>Create a new React project by running the following command. </p>
<pre><code class="lang-sh">yarn create react-app react-router-demo
</code></pre>
<p>I'll be using yarn to install the dependencies, but you can use npm as well.</p>
<p>Next, let's install <code>react-router-dom</code>.</p>
<pre><code>yarn add react-router-dom
</code></pre><p>For styling the components, I'm going to use the Bulma CSS framework. So let's add that as well.</p>
<pre><code>yarn add bulma
</code></pre><p>Next, import <code>bulma.min.css</code> in the <code>index.js</code> file and clean up all the boilerplate code from the <code>App.js</code> file.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> <span class="hljs-string">"bulma/css/bulma.min.css"</span>;
</code></pre>
<p>Now that you have the project set up let's start by creating a few page components.</p>
<h2 id="heading-creating-the-page-components">Creating the Page Components</h2>
<p>Create a pages directory inside the src folder where we will park all the page components.</p>
<p>For this demo, create three pages - Home, About, and Profile.</p>
<p>Paste the following inside the Home and About components.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// pages/Home.js</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"title is-1"</span>&gt;</span>This is the Home Page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras gravida,
      risus at dapibus aliquet, elit quam scelerisque tortor, nec accumsan eros
      nulla interdum justo. Pellentesque dignissim, sapien et congue rutrum,
      lorem tortor dapibus turpis, sit amet vestibulum eros mi et odio.
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Home;
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-comment">// pages/About.js</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> About = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"title is-1"</span>&gt;</span>This is the About Page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
      inceptos himenaeos. Vestibulum ante ipsum primis in faucibus orci luctus
      et ultrices posuere cubilia curae; Duis consequat nulla ac ex consequat,
      in efficitur arcu congue. Nam fermentum commodo egestas.
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> About;
</code></pre>
<p>We will create the Profile page later on in the article.</p>
<h2 id="heading-create-the-navbar-component">Create the Navbar Component</h2>
<p>Let's start by creating the navigation bar for our app. This component will make use of the <code>&lt;NavLink /&gt;</code> component from <code>react-router-dom</code>.  </p>
<p>Create a directory called "components" inside the src folder.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// components/Navbar.js</span>

<span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { NavLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [isOpen, setOpen] = useState(<span class="hljs-literal">false</span>);
  <span class="hljs-keyword">return</span> ( 
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar is-primary"</span>
      <span class="hljs-attr">role</span>=<span class="hljs-string">"navigation"</span>
      <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"main navigation"</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
          {/* ... */}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
  );
 };

 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Navbar;
</code></pre>
<p>The <code>isOpen</code> state variable will be used to trigger the menu on mobile or tablet devices.</p>
<p>So let's add the hamburger menu.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [isOpen, setOpen] = useState(<span class="hljs-literal">false</span>);
  <span class="hljs-keyword">return</span> ( 
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar is-primary"</span>
      <span class="hljs-attr">role</span>=<span class="hljs-string">"navigation"</span>
      <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"main navigation"</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-brand"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span>
            <span class="hljs-attr">role</span>=<span class="hljs-string">"button"</span>
            <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">navbar-burger</span> <span class="hljs-attr">burger</span> ${<span class="hljs-attr">isOpen</span> &amp;&amp; "<span class="hljs-attr">is-active</span>"}`}
            <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"menu"</span>
            <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span>
            <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setOpen(!isOpen)}
          &gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          {/* ... */}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
  );
 };
</code></pre>
<p>To add the link in the menu, use the <code>&lt;NavLink /&gt;</code> component by <code>react-router-dom</code>.</p>
<p>The <code>NavLink</code> component provides a declarative way to navigate around the application. It is similar to the <code>Link</code> component, except it can apply an active style to the link if it is active. </p>
<p>To specify which route to navigate to, use the <code>to</code> prop and pass the path name.<br>The <code>activeClassName</code> prop will add an active class to the link if it's currently active.</p>
<pre><code class="lang-jsx">&lt;NavLink
    className=<span class="hljs-string">"navbar-item"</span>
    activeClassName=<span class="hljs-string">"is-active"</span>
    to=<span class="hljs-string">"/"</span>
    exact
&gt;
    Home
&lt;/NavLink&gt;
</code></pre>
<p>On the browser, the <code>NavLink</code> component is rendered as an <code>&lt;a&gt;</code> tag with an <code>href</code> attribute value that was passed in the <code>to</code> prop.</p>
<p>Also, here you need to specify the <code>exact</code> prop so that it is matched precisely with the URL.</p>
<p>Add all the links and finish up the <code>Navbar</code> component.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { NavLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [isOpen, setOpen] = useState(<span class="hljs-literal">false</span>);
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar is-primary"</span>
      <span class="hljs-attr">role</span>=<span class="hljs-string">"navigation"</span>
      <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"main navigation"</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-brand"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span>
            <span class="hljs-attr">role</span>=<span class="hljs-string">"button"</span>
            <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">navbar-burger</span> <span class="hljs-attr">burger</span> ${<span class="hljs-attr">isOpen</span> &amp;&amp; "<span class="hljs-attr">is-active</span>"}`}
            <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"menu"</span>
            <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span>
            <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setOpen(!isOpen)}
          &gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">navbar-menu</span> ${<span class="hljs-attr">isOpen</span> &amp;&amp; "<span class="hljs-attr">is-active</span>"}`}&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-start"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-item"</span> <span class="hljs-attr">activeClassName</span>=<span class="hljs-string">"is-active"</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>
              Home
            <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span>
              <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-item"</span>
              <span class="hljs-attr">activeClassName</span>=<span class="hljs-string">"is-active"</span>
              <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>
            &gt;</span>
              About
            <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span>
              <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-item"</span>
              <span class="hljs-attr">activeClassName</span>=<span class="hljs-string">"is-active"</span>
              <span class="hljs-attr">to</span>=<span class="hljs-string">"/profile"</span>
            &gt;</span>
              Profile
            <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-end"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-item"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"buttons"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"button is-white"</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Navbar;
</code></pre>
<p>If you notice here, I've added a Login button. We will come back to the <code>Navbar</code> component again when we discuss protected routes later on in the guide.</p>
<h2 id="heading-rendering-the-pages">Rendering the pages</h2>
<p>Now that the <code>Navbar</code> component is set up let's add that to the page and start with rendering the pages.</p>
<p>Since the navigation bar is a common component across all the pages, instead of calling the component in each page component, it will be a better approach to render the <code>Navbar</code> in a common layout.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// App.js</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container mt-2"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">marginTop:</span> <span class="hljs-attr">40</span> }}&gt;</span>
        {/* Render the page here */}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>Now, add the page components inside of the container.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// App.js</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container mt-2"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">marginTop:</span> <span class="hljs-attr">40</span> }}&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">About</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>If you check out the results now, you'll notice that both the Home and the About page component gets rendered onto the page. That's because we haven't added any routing logic yet.</p>
<p>You need to import the <code>BrowserRouter</code> component from React Router to add the ability to route the components. All you need to do is wrap all the page components inside of the <code>BrowserRouter</code> component. This will enable all the page components to have the routing logic. Perfect!</p>
<p>But again, nothing's going to change with the results – you are still going to see both the pages rendered. You need to render the page component only if the URL matches a particular path. That's where the <code>Route</code> component from React Router comes into play.</p>
<p>The <code>Router</code> component has a <code>path</code> prop that accepts the page's path, and the page component should be wrapped with the <code>Router</code>, as shown below.</p>
<pre><code class="lang-jsx">&lt;Route path=<span class="hljs-string">"/about"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">About</span> /&gt;</span></span>
&lt;/Route&gt;
</code></pre>
<p>So let's do the same for the <code>Home</code> component.</p>
<pre><code class="lang-jsx">&lt;Route exact path=<span class="hljs-string">"/"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span></span>
&lt;/Route&gt;
</code></pre>
<p>The <code>exact</code> prop above tells the <code>Router</code> component to match the path exactly. If you don't add the <code>exact</code> prop on the <code>/</code> path, it will match with all the routes starting with a <code>/</code> including <code>/about</code>.</p>
<p>If you go check out the results now, you'll still see both the components rendered. But, if you go to <code>/about</code>, you'll notice that only the <code>About</code> component gets rendered. You see this behavior because the router keeps matching the URL with the routes even after it had matched a route already. </p>
<p>We need to tell the router to stop matching further once it matches a route. This is done using the <code>Switch</code> component from React Router.</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container mt-2"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">marginTop:</span> <span class="hljs-attr">40</span> }}&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Home</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">About</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
  );
}
</code></pre>
<p>There you go! You have successfully configured routing in your React app. </p>
<h2 id="heading-protected-routes-and-redirect">Protected Routes and Redirect</h2>
<p>When working on Real-world applications, you will have some routes behind an authentication system. You are going to have routes or pages that can only be accessed by a logged-in user. In this section, you'll learn how to go about implementing such routes.</p>
<p><strong><em>Please note</em></strong> <em>that I'm not going to create any login form or have any back-end service authenticate the user. In a real application, you wouldn't implement authentication the way demonstrated here.</em></p>
<p>Let's create the Profile page component that should only be accessed by the authenticated user.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// pages/Profile.js</span>

<span class="hljs-keyword">import</span> { useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Profile = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> { name } = useParams();
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"title is-1"</span>&gt;</span>This is the Profile Page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">article</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"message is-dark"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">marginTop:</span> <span class="hljs-attr">40</span> }}&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"message-header"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{name}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"message-body"</span>&gt;</span>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.{" "}
          <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Pellentesque risus mi<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>, tempus quis placerat ut, porta
          nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida
          purus diam, et dictum <span class="hljs-tag">&lt;<span class="hljs-name">a</span>&gt;</span>felis venenatis<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span> efficitur. Aenean ac{" "}
          <span class="hljs-tag">&lt;<span class="hljs-name">em</span>&gt;</span>eleifend lacus<span class="hljs-tag">&lt;/<span class="hljs-name">em</span>&gt;</span>, in mollis lectus. Donec sodales, arcu et
          sollicitudin porttitor, tortor urna tempor ligula, id porttitor mi
          magna a neque. Donec dui urna, vehicula et sem eget, facilisis sodales
          sem.
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">article</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
<p>We will grab the user's name from the URL using route parameters.</p>
<p>Add the Profile route into the router.</p>
<pre><code class="lang-jsx">&lt;Route path=<span class="hljs-string">"/profile/:name"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Profile</span> /&gt;</span></span>
&lt;/Route&gt;
</code></pre>
<p>Currently the profile page can be accessed directly. So to make it an authenticated route, create a Higher-Order component (HOC) to wrap the authentication logic.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> withAuth = <span class="hljs-function">(<span class="hljs-params">Component</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> AuthRoute = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> isAuth = !!<span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"token"</span>);
    <span class="hljs-comment">// ...</span>
  };

  <span class="hljs-keyword">return</span> AuthRoute;
};
</code></pre>
<p>To determine if a user is authenticated or not, grab the authentication token that is stored in the browser when the user logs in. If the user is not authenticated, redirect the user to the Home page. The <code>Redirect</code> component from React Router can be used to redirect the user to another path.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> withAuth = <span class="hljs-function">(<span class="hljs-params">Component</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> AuthRoute = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> isAuth = !!<span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"token"</span>);
    <span class="hljs-keyword">if</span> (isAuth) {
      <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Component</span> /&gt;</span></span>;
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Redirect</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> /&gt;</span></span>;
    }
  };

  <span class="hljs-keyword">return</span> AuthRoute;
};
</code></pre>
<p>You can also pass in other user information like name and user ID using props to the wrapped component.</p>
<p>Next, use the <code>withAuth</code> HOC in the Profile component.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> withAuth <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/withAuth"</span>;

<span class="hljs-keyword">const</span> Profile = <span class="hljs-function">() =&gt;</span> {
 <span class="hljs-comment">// ...</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> withAuth(Profile);
</code></pre>
<p>Now, if you try to visit <code>/profile/JohnDoe</code>, you will get redirected to the Home page. That's because the authentication token is not yet set in your browser storage.</p>
<p>Alright, so, let's return to the <code>Navbar</code> component and add the login and logout functionalities. When the user is authenticated, show the Logout button and when the user is not logged in show the Login button. </p>
<pre><code class="lang-jsx"><span class="hljs-comment">// components/Navbar.js</span>

<span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// ...</span>
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>
          <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar is-primary"</span>
          <span class="hljs-attr">role</span>=<span class="hljs-string">"navigation"</span>
          <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"main navigation"</span>
        &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
            {/* ... */}
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-end"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-item"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"buttons"</span>&gt;</span>
                {!isAuth ? (
                  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"button is-white"</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{loginUser}</span>&gt;</span>
                    Log in
                  <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                ) : (
                  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"button is-black"</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{logoutUser}</span>&gt;</span>
                    Log out
                  <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                )}
              <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
    );
}
</code></pre>
<p>When the user clicks on the login button, set a dummy token in the local storage, and redirect the user to the profile page. </p>
<p>But we cannot use the Redirect component in this case – we need to redirect the user programmatically. Sensitive tokens used for authentication are usually stored in cookies for security reasons.</p>
<p>React Router has a <code>withRouter</code> HOC that injects the <code>history</code> object in the props of the component to leverage the History API. It also passes the updated <code>match</code> and <code>location</code> props to the wrapped component.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// components/Navbar.js</span>

<span class="hljs-keyword">import</span> { NavLink, withRouter } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">(<span class="hljs-params">{ history }</span>) =&gt;</span> { 
  <span class="hljs-keyword">const</span> isAuth = !!<span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"token"</span>);

  <span class="hljs-keyword">const</span> loginUser = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"token"</span>, <span class="hljs-string">"some-login-token"</span>);
    history.push(<span class="hljs-string">"/profile/Vijit"</span>);
  };

  <span class="hljs-keyword">const</span> logoutUser = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">localStorage</span>.removeItem(<span class="hljs-string">"token"</span>);
    history.push(<span class="hljs-string">"/"</span>);
  };

  <span class="hljs-keyword">return</span> ( 
   {<span class="hljs-comment">/* ... */</span>}
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> withRouter(Navbar);
</code></pre>
<p>And <em>voilà</em>! That's it. You have conquered the land of authenticated routes as well.</p>
<p>Check out the live demo <a target="_blank" href="https://react-router-demo.vijitail.dev/">here</a> and the complete code in this <a target="_blank" href="https://github.com/vijitail/react-router-demo">repo</a> for your reference.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>I hope by now you have a fair idea of how client-side routing works in general and how to implement routing in React using the React Router library. </p>
<p>In this guide, you learned about the vital components in React Router like <code>Route</code>, <code>withRouter</code>, <code>Link</code>, and so on, along with some advanced concepts like authenticated routes, that are required to build an application. </p>
<p>Do check out the React Router <a target="_blank" href="https://reacttraining.com/react-router/web/guides/quick-start">docs</a> to get a more detailed overview of each of the components.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A Complete Beginner's Guide to React Router (Including Router Hooks) ]]>
                </title>
                <description>
                    <![CDATA[ By Ibrahima Ndaw React is a JavaScript library for building user interfaces. We can also extend it to build multi-page applications with the help of React Router. This is a third-party library that enables routing in our React apps. In this tutorial,... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-complete-beginners-guide-to-react-router-include-router-hooks/</link>
                <guid isPermaLink="false">66d85016e9c1a2c18adec0a4</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 18 Feb 2020 11:05:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/06/cover.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ibrahima Ndaw</p>
<p>React is a JavaScript library for building user interfaces. We can also extend it to build multi-page applications with the help of React Router. This is a third-party library that enables routing in our React apps.</p>
<p>In this tutorial, we are going to cover everything you need to know to get started with React Router.</p>
<ul>
<li><a class="post-section-overview" href="#heading-setting-up-the-project">Setting up the project</a></li>
<li><a class="post-section-overview" href="#heading-what-is-routing">What is routing?</a></li>
<li><a class="post-section-overview" href="#heading-setting-up-the-router">Setting up the router</a></li>
<li><a class="post-section-overview" href="#heading-rendering-routes">Rendering routes</a></li>
<li><a class="post-section-overview" href="#heading-using-links-to-switch-pages">Using Links to switch pages</a></li>
<li><a class="post-section-overview" href="#heading-passing-route-parameters">Passing Route parameters</a></li>
<li><a class="post-section-overview" href="#heading-navigating-programmatically">Navigating programmatically</a></li>
<li><a class="post-section-overview" href="#heading-redirecting-to-another-page">Redirecting to another page</a></li>
<li><a class="post-section-overview" href="#redirecting-to-404-page">Redirecting to a 404 page</a></li>
<li><a class="post-section-overview" href="#heading-guarding-routes">Guarding routes</a></li>
<li><a class="post-section-overview" href="#heading-router-hooks">Router Hooks</a></li>
<li><a class="post-section-overview" href="#heading-usehistory">useHistory</a></li>
<li><a class="post-section-overview" href="#heading-useparams">useParams</a></li>
<li><a class="post-section-overview" href="#heading-uselocation">useLocation</a></li>
<li><a class="post-section-overview" href="#heading-final-thoughts">Final Thoughts</a></li>
<li><a class="post-section-overview" href="#heading-next-steps">Next Steps</a></li>
</ul>
<h2 id="heading-setting-up-the-project">Setting up the project</h2>
<p>To be able to follow along, you will need to create a new React app by running the following command in your terminal:</p>
<pre><code class="lang-shell">npx create-react-app react-router-guide
</code></pre>
<p>Then, add these lines of code to the <code>App.js</code> file:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
  );
}
<span class="hljs-comment">// Home Page</span>
<span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );
<span class="hljs-comment">// About Page</span>
<span class="hljs-keyword">const</span> About = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );
<span class="hljs-comment">// Contact Page</span>
<span class="hljs-keyword">const</span> Contact = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );

<span class="hljs-keyword">const</span> FakeText = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
  )
</code></pre>
<p>Then, if you're ready to go, let's start by answering an important question: what is routing?</p>
<h2 id="heading-what-is-routing">What is routing?</h2>
<p>Routing is the capacity to show different pages to the user. That means the user can move between different parts of an application by entering a URL or clicking on an element.</p>
<p>As you may already know, by default, React comes without routing. And to enable it in our project, we need to add a library named <a target="_blank" href="https://reacttraining.com/react-router/web/guides/quick-start">react-router</a>.</p>
<p>To install it, you will have to run the following command in your terminal:</p>
<pre><code class="lang-shell">yarn add react-router-dom
</code></pre>
<p>Or</p>
<pre><code class="lang-shell">npm install react-router-dom
</code></pre>
<p>Now, we've successfully installed our router, let's start using it in the next section.</p>
<h2 id="heading-setting-up-the-router">Setting up the router</h2>
<p>To enable routing in our React app, we first need to import <code>BrowserRouter</code> from <code>react-router-dom</code>.</p>
<p>In the <code>App.js</code> file, enter the following:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>This should hold everything in our app where routing is needed. That means, if we need routing in our entire app, we must wrap our higher component with <code>BrowserRouter</code>.</p>
<p>By the way, you don't have to rename <code>BrowserRouter as Router</code> as I do here, I just want to keep things readable.</p>
<p>A router alone doesn't do much. So let's add a route in the next section.</p>
<h2 id="heading-rendering-routes">Rendering routes</h2>
<p>To render routes, we have to import the <code>Route</code> component from the router package.</p>
<p>In your <code>App.js</code> file, add the following code:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>} /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>Then, add it where we want to render the content. The <code>Route</code> component has several properties. But here, we just need <code>path</code> and <code>render</code>.</p>
<p><code>path</code>: the path of the route. Here, we use <code>/</code> to define the path of the home page.</p>
<p><code>render</code>: will display the content whenever the route is reached. Here, we'll render a welcome message to the user.</p>
<p>In some cases serving routes like that is perfectly fine. But imagine a case when we have to deal with a real component – using <code>render</code> may not be the right solution.</p>
<p>So, how can we display a real component? Well, the <code>Route</code> component has another property named <code>component</code>.</p>
<p>Let's update our example a bit to see it in action.</p>
<p>In your <code>App.js</code> file, add the following code:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );
</code></pre>
<p>Now, instead of rendering a message, our route will load the <code>Home</code> component.</p>
<p>To get the full power of React Router, we need to have multiple pages and links to play with. We already have pages (components if you want, too), so now let's add some links so we can switch between pages.</p>
<h2 id="heading-using-links-to-switch-pages">Using links to switch pages</h2>
<p>To add links to our project, we will use the React Router again.</p>
<p>In your <code>App.js</code> file, add the following code:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );

<span class="hljs-keyword">const</span> About = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );

<span class="hljs-keyword">const</span> Contact = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );
</code></pre>
<p>After importing <code>Link</code>, we have to update our navigation bar a bit. Now, instead of using <code>a</code> tag and <code>href</code>, React Router uses <code>Link</code> and <code>to</code> to, well, be able to switch between pages without reloading it.</p>
<p>Then, we need to add two new routes, <code>About</code> and <code>Contact</code>, to be able to switch between pages or components.</p>
<p>Now, we can go to different parts of our app through links. But there is an issue with our router: the <code>Home</code> component is always displayed even if we switch to other pages.</p>
<p>This is because React Router will check if the <code>path</code> defined starts with <code>/</code>. If that's the case, it will render the component. And here, our first route starts with <code>/</code>, so the <code>Home</code> component will be rendered each time.</p>
<p>However, we can still change the default behavior by adding the <code>exact</code> property to <code>Route</code>.</p>
<p>In <code>App.js</code>, add:</p>
<pre><code class="lang-jsx">    &lt;Route path=<span class="hljs-string">"/"</span> exact component={Home} /&gt;
</code></pre>
<p>By updating the <code>Home</code> route with <code>exact</code>, now it will be rendered only if it matches the full path.</p>
<p>We can still enhance it by wrapping our routes with <code>Switch</code> to tell to React Router to load only one route at a time.</p>
<p>In <code>App.js</code>, add:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link, Switch } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span></span>
</code></pre>
<p>Now that we have new links, let's use them to pass parameters.</p>
<h2 id="heading-passing-route-parameters">Passing route parameters</h2>
<p>To pass data between pages, we have to update our example.</p>
<p>In your <code>App.js</code> file, add the following code:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link, Switch } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">'John Doe'</span>
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`/<span class="hljs-attr">about</span>/${<span class="hljs-attr">name</span>}`}&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about/:name"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );

<span class="hljs-keyword">const</span> About = <span class="hljs-function">(<span class="hljs-params">{match:{params:{name}}}</span>) =&gt;</span> (
  <span class="hljs-comment">// props.match.params.name</span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About {name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
);

<span class="hljs-keyword">const</span> Contact = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );
</code></pre>
<p>As you can see here, we start by declaring a new constant <code>name</code> which will be passed as a parameter to the <code>About</code> page. And we append <code>name</code> to the corresponding link.</p>
<p>With that, we now have to update the <code>About</code> route by adjusting its path to receive <code>name</code> as a parameter <code>path="/about/:name"</code>.</p>
<p>Now, the parameter will be received as props from the <code>About</code> component. The only thing we have to do now is destructure the props and get back the <code>name</code> property. By the way, <code>{match:{params:{name}}}</code> is the same as <code>props.match.params.name</code>.</p>
<p>We've done a lot up to this point. But in some cases we don't want to use links to navigate between pages.</p>
<p>Sometimes, we have to wait for an operation to finish before navigating to the next page.</p>
<p>So, let's handle that case in the next section.</p>
<h2 id="heading-navigating-programmatically">Navigating programmatically</h2>
<p>The props we receive have some convenient methods we can use to navigate between pages.</p>
<p>In <code>App.js</code>, add:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> Contact = <span class="hljs-function">(<span class="hljs-params">{history}</span>) =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> history.push('/') } &gt;Go to home<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  );
</code></pre>
<p>Here, we pull the <code>history</code> object from the props we receive. It has some handy methods like <code>goBack</code>, <code>goForward</code>, and so on. But here, we will use the <code>push</code> method to be able to go to the Home page.</p>
<p>Now, let's handle the case when we want to redirect our user after an action.</p>
<h2 id="heading-redirecting-to-another-page">Redirecting to another page</h2>
<p>The React Router has another component named <code>Redirect</code>. As you guessed, it helps us redirect the user to another page</p>
<p>In <code>App.js</code>, add:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link, Switch, Redirect } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> About = <span class="hljs-function">(<span class="hljs-params">{match:{params:{name}}}</span>) =&gt;</span> (
  <span class="hljs-comment">// props.match.params.name</span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    { name !== 'John Doe' ? <span class="hljs-tag">&lt;<span class="hljs-name">Redirect</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> /&gt;</span> : null }
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About {name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">FakeText</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
);
</code></pre>
<p>Now, if the <code>name</code> passed as a parameter is not equal to <code>John Doe</code>, the user will be redirected to the home page.</p>
<p>You could argue that you should redirect the user with <code>props.history.push('/)</code>. Well, the <code>Redirect</code> component replaces the page and therefore the user can't go back to the previous page. But, with the push method they can. However, you can use <code>props.history.replace('/)</code> to mimic the <code>Redirect</code> behavior.</p>
<p>Now let's move on and handle the case when the user hits a route that doesn't exist.</p>
<h2 id="heading-redirecting-to-a-404-page">Redirecting to a 404 page</h2>
<p>To redirect the user to a 404 page, you can create a component to show it. But here, to keep things simple, I will just display a message with <code>render</code>.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link, Switch } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">'John Doe'</span>

  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`/<span class="hljs-attr">about</span>/${<span class="hljs-attr">name</span>}`}&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about/:name"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>404: page not found<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>} /&gt;

    <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>The new route we've added will catch every path that doesn't exist and redirect the user to the 404 page.</p>
<p>Now, let's move on and learn how to protect our routes in the next section.</p>
<h2 id="heading-guarding-routes">Guarding routes</h2>
<p>There are many ways to protect routes to React. But here I will just check if the user is authenticated and redirect them to the appropriate page.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>

<span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link, Switch } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">'John Doe'</span>
  <span class="hljs-keyword">const</span> isAuthenticated = <span class="hljs-literal">false</span>
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`/<span class="hljs-attr">about</span>/${<span class="hljs-attr">name</span>}`}&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/contact"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
      {
      isAuthenticated ? 
      <span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about/:name"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/&gt;</span> : <span class="hljs-tag">&lt;<span class="hljs-name">Redirect</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> /&gt;</span>
      }

    <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
&lt;/Router&gt;
  );
}
</code></pre>
<p>As you can see here, I declared a variable to mimic authentication. Then, check if the user is authenticated or not. If they are, render protected pages. Otherwise redirect them to the home page.</p>
<p>We've covered a lot up to this point, but an interesting part remains: router hooks.</p>
<p>Let's move to the final section and introduce Hooks.</p>
<h2 id="heading-router-hooks">Router Hooks</h2>
<p>Router hooks make things much easier. Now you can access the history, location, or parameters in an easy and elegant way.</p>
<h3 id="heading-usehistory">useHistory</h3>
<p>The <code>useHistory</code> hook gives us access to the history instance without pulling it from props.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { useHistory } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Contact = <span class="hljs-function">() =&gt;</span> {
<span class="hljs-keyword">const</span> history = useHistory();
<span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> history.push('/') } &gt;Go to home<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  )
  };
</code></pre>
<h3 id="heading-useparams">useParams</h3>
<p>This hook helps us get the parameter passed on the URL without using the props object.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Link, Switch, useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">'John Doe'</span>
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`/<span class="hljs-attr">about</span>/${<span class="hljs-attr">name</span>}`}&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about/:name"</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">const</span> About = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> { name } = useParams()
  <span class="hljs-keyword">return</span> (
  <span class="hljs-comment">// props.match.params.name</span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    { name !== 'John Doe' ? <span class="hljs-tag">&lt;<span class="hljs-name">Redirect</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> /&gt;</span> : null }
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About {name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
)
};
</code></pre>
<h3 id="heading-uselocation">useLocation</h3>
<p>This hook returns the location object that represents the current URL.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { useLocation } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Contact = <span class="hljs-function">() =&gt;</span> {
<span class="hljs-keyword">const</span> { pathname } = useLocation();

<span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Current URL: {pathname}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
  )
  };
</code></pre>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>React Router is an amazing library that helps us go from a single page to a multi-page application feeling with great usability. (Just keep in mind – at the end of the day, it's still a single page app). </p>
<p>And now with router hooks, you can see how easy and elegant they are. They're definitely something to consider in your next project.</p>
<p>You can read more of my articles on <a target="_blank" href="https://www.ibrahima-ndaw.com/blog/">my blog</a>.</p>
<h2 id="heading-next-steps">Next Steps</h2>
<p><a target="_blank" href="https://reacttraining.com/react-router/web">React Router Documentation</a></p>
<p>Photo by <a target="_blank" href="https://unsplash.com/@sortino?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Joshua Sortino</a> on <a target="_blank" href="https://unsplash.com/s/photos/route?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn React Router in 5 Minutes - A Beginner's Tutorial ]]>
                </title>
                <description>
                    <![CDATA[ By Bob Ziroll Sometimes you've only got 5 minutes to spare. Instead of wasting it on social media, let's get a 5-minute introduction to React-Router! In this tutorial, we're going to learn the basics of routing in React by building navigation for a S... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-router-in-5-minutes/</link>
                <guid isPermaLink="false">66d45ddfbc9760a197a1035b</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 11 Nov 2019 19:30:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9f69740569d1a4ca4284.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Bob Ziroll</p>
<p>Sometimes you've only got 5 minutes to spare. Instead of wasting it on social media, let's get a 5-minute introduction to React-Router! In this tutorial, we're going to learn the basics of routing in React by building navigation for a Scrimba knitting shop website. It's not real, but maybe one day... ;)</p>
<p>If you want a proper introduction to this subject, you can join the waitlist for my <a target="_blank" href="https://scrimba.com/g/greact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=router_article">upcoming advanced React course</a>, or if you're still a beginner, check out my <a target="_blank" href="https://scrimba.com/g/glearnreact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=router_article">introductory course on React.</a></p>
<h3 id="heading-what-is-react-router-anyway">What is React-Router, anyway?</h3>
<p>Many modern websites are actually made up of a single page, they just look like multiple pages because they contain components which render like separate pages. These are usually referred to as SPAs - <strong>s</strong>ingle-<strong>p</strong>age <strong>a</strong>pplications. At its core, what React Router does is conditionally render certain components to display depending on the <em>route</em> being used in the URL (<code>/</code> for the home page, <code>/about</code> for the about page, etc.).</p>
<p>For example, we can use React Router to connect <em>www.knit-with-scrimba.com/</em> to <em>www.knit-with-scrimba.com/about</em> or <em>www.knit-with-scrimba.com/shop</em></p>
<h3 id="heading-sounds-great-how-do-i-use-it">Sounds great - how do I use it?</h3>
<p>To use React Router, you first have to install it using NPM:</p>
<pre><code class="lang-bash">npm install react-router-dom
</code></pre>
<p>Alternatively, you can just use <a target="_blank" href="https://scrimba.com/c/cNq8MzCr">this playground in Scrimba</a>, which has the completed code already written.</p>
<p>You'll need to import BrowserRouter, Route, and Switch from <code>react-router-dom</code> package:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { BrowserRouter, Route, Switch } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
</code></pre>
<p>In my example, I link the landing page with two other "pages" (which are actually just components) called <code>Shop</code> and <code>About</code>.</p>
<p>First, you'll need to set up your app to work with React Router. Everything that gets rendered will need to go inside the <code>&lt;BrowserRouter&gt;</code> element, so wrap your App in those first. It's the component that does all the logic of displaying various components that you provide it with.</p>
<pre><code class="lang-js"><span class="hljs-comment">// index.js</span>
ReactDOM.render(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>, 
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)
)
</code></pre>
<p>Next, in your App component, add the <code>Switch</code> element (open and closing tags). These ensure that only one component is rendered at a time. If we don't use this, we can default to the <code>Error</code> component, which we're going to write later.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>

            <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
    )
}
</code></pre>
<p>It's now time to add your <code>&lt;Route&gt;</code> tags. These are the links between the components and should be placed inside the <code>&lt;Switch&gt;</code> tags.</p>
<p>To tell the <code>&lt;Route&gt;</code> tags which component to load, simply add a <code>path</code> attribute and the name of the component you want to load with <code>component</code> attribute.</p>
<pre><code class="lang-js">&lt;Route path=<span class="hljs-string">'/'</span> component={Home} /&gt;
</code></pre>
<p>Many homepage URLs are the site name followed by <code>"/"</code>, for example, <em>www.knit-with-scrimba.com/</em>. In this case, we add <code>exact</code> to the Route tag. This is because the other URLs also contain "/", so if we don't tell the app that it needs to look for just <code>/</code>, it loads the first one to match the route, and we get a pretty tricky bug to deal with.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> <span class="hljs-attr">exact</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/shop"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Shop}</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
    )
}
</code></pre>
<p>Now import the components into the app. You may wish to have them in a separate "components" folder to keep code clean and readable.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Home'</span>;
<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/About'</span>;
<span class="hljs-keyword">import</span> Shop <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Shop'</span>;
</code></pre>
<p>Onto that error message I mentioned earlier, which loads if a user types an incorrect URL. This is just like a normal <code>&lt;Route&gt;</code> tag, but with no path. The Error component contains <code>&lt;h1&gt;Oops! Page not found!&lt;/h1&gt;</code>. Don't forget to import it into the app.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Switch</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Home}</span> <span class="hljs-attr">exact</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/shop"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Shop}</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Error}</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Switch</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
    )
}
</code></pre>
<p>So far, our site is only navigable by typing the URLs. To add clickable links to the site, we use the <code>Link</code> element from React Router and set up a new <code>Navbar</code> component. Once again, don't forget to import the new component into the app.</p>
<p>Now add a <code>Link</code> for each component in the app and use <code>to="URL"</code> to link them.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>Home <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/about"</span>&gt;</span>About Us <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/shop"</span>&gt;</span>Shop Now <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
<p>Your site now has clickable links that can navigate you around your single-page app!</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>So there we have it. If you want to easily navigate around a React app, forget the anchor tags and add React Router. It's clean, it's organized, and it makes adding and deleting pages a whole lot easier.</p>
<p>To learn more about React Hooks and other great features of React, you can join the waitlist for my <a target="_blank" href="https://scrimba.com/g/greact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=router_article">upcoming advanced React course.</a> </p>
<p>Or if you're looking for something more beginner friendly, you can check out my <a target="_blank" href="https://scrimba.com/g/greact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=router_article">introductory course on React.</a></p>
<p>Happy coding ;)</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
