<?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-4 - 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-4 - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 31 May 2026 05:06:23 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/react-router-4/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to upgrade to React Router 4 ]]>
                </title>
                <description>
                    <![CDATA[ By Lekha Surasani Not long after I started working at my current position, the team realized that it would be necessary for us to upgrade to React 16 so we could use a new UI library we were keen on adopting.  To figure out how much time this upgrad... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-guide-to-upgrading-to-react-router-4/</link>
                <guid isPermaLink="false">66d46013264384a65d5a958c</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react-router-4 ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 01 Aug 2019 22:00:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/07/react-router-4-2.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Lekha Surasani</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/07/image-82.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Not long after I started working at my current position, the team realized that it would be necessary for us to upgrade to React 16 so we could use a new UI library we were keen on adopting. </p>
<p>To figure out how much time this upgrade would require, we looked at all of our current packages to see if they were compatible with React 16, and to see if we were still using unsupported or deprecated packages. </p>
<p>The beginnings of our code base had been built by developers who used whatever open source or third party library they wanted, without actually vetting them. Thus, we found that a lot of the packages were deprecated and needed to be replaced ASAP. </p>
<p>One of the biggest surprises for us was the deprecation of <code>react-router-redux</code>. We were using <code>react-router-redux</code> in conjunction with react-router v3. This led us to think critically about why we were using <code>redux</code> in our router in the first place. </p>
<p>Once we started to look into react router v4, we realized that the new features would pretty much eliminate any reason for us to use an additional library to connect our router and <code>redux</code>. So, that left us in the position to just upgrade from react router 3 to 4, and remove  <code>react-router-redux</code> from our application.</p>
<p>So, I was tasked with upgrading our router to v4 after only being in the position and working with React for about 2 months. This was because upgrading from React Router 3 to React Router 4 sounded like it should be a trivial undertaking. But, as I quickly found out, it was a little bit more involved than I anticipated. </p>
<p>Looking through the <a target="_blank" href="https://reacttraining.com/react-router/web/guides/quick-start">documentation</a>, the <a target="_blank" href="https://github.com/ReactTraining/react-router/">GitHub repo</a>, and many, many Stack Overflow answers, I finally pieced together the steps for the upgrade and wanted to share my findings — especially to explain how and why certain changes are made.</p>
<p>The biggest change to note, from the creators of React Router, is that the upgrade from React Router 3 to React Router 4 is more than just updating a few libraries and features — it allows you to fundamentally change how your Router works. The creators of React Router wanted to go back to a simple Router, allowing the developer to customize it however they would like.</p>
<p>I’ve split up this guide into 5 different parts:</p>
<ol>
<li>Package</li>
<li>History</li>
<li>Route</li>
<li>Props</li>
<li>Redux Integration</li>
</ol>
<hr>
<h1 id="heading-package">Package</h1>
<p>React  Router v4 package structure changed such that it’s no longer necessary  to install react-router — you have to install <code>react-router-dom</code> (and  uninstall <code>react-router</code>), but you don’t lose anything since it re-exports  all of <code>react-router</code>’s exports. This means you also have to update any  <code>react-router</code> import statements to <code>react-router-dom</code>.</p>
<hr>
<h1 id="heading-history">History</h1>
<p>History is an essential part of routing, allowing us to remember where we came from and where we are currently. History comes in many forms for react-router, and could take a while to explain. So, to keep this article on topic, I’ll simply recommend that you read through <a target="_blank" href="https://medium.com/@pshrmn/a-little-bit-of-history-f245306f48dd">this article</a> that explains history in relation to react router 4. This article should cover most cases of your usage of history.</p>
<hr>
<h1 id="heading-route">Route</h1>
<p>React Router v3 allowed us to place all of our application routes in one file, which we’ll call router.js. However, React Router v4 allows you to  place Routes in the components that they’re rendering. The idea here is to <em>dynamically route</em> the application — in other words, the routing takes place as the app is rendering.</p>
<p>However,  if you have a decent-size legacy code base you’re working with, you probably won’t be making a change that big. Luckily, React Router v4  still allows you to place all the routes in a central file, which is how I’ll create all of our examples. There are, however, a few older components and features that will need replacing.</p>
<h2 id="heading-indexroute">IndexRoute</h2>
<p>Previously,  <code>IndexRoute</code> was used as a route for some default UI of a parent Route. But, in v4, <code>IndexRoute</code> is no longer used, since this functionality is  now available in Route.</p>
<p>For providing default UI, multiple Routes that have the same path will let all of the associated components render:</p>
<pre><code><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="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    // example of our route components
    <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">"/"</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">{Contact}</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
</code></pre><p>So, all of the Components — <code>Home</code>, <code>About</code>, and <code>Contact</code> — will render. Because of this, you can no longer nest Routes, either.</p>
<p>Additionally, to allow for better matching without the use of <code>IndexRoute</code>, you can use the exact keyword.</p>
<pre><code class="lang-javascript"><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="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
    // example of our route components
    <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">Router</span>&gt;</span></span>
</code></pre>
<h2 id="heading-exclusive-routing">Exclusive Routing</h2>
<p>After adding in the exact keyword, <code>“something.com/about”</code> will be routed to  when the router sees a path <code>“/about”</code>. But now what if you have another  path, <code>“/about/team”</code>? As I stated before, the router will render anything  that matches. So, the components associated with both <code>“/about”</code> and  <code>“/about/team”</code> will render. If that’s what you intended, then that’s great! However, if this isn’t what you want, you may have to put a  Switch around this group of Routes. This will allow the first path that  matches the URL to render.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, 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">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">"/about/team"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Team}</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>Note  that the keyword exact still has to appear for the Home component —  otherwise it would match for the subsequent routes. Also note that we  have to list <code>“/about/team”</code> before <code>“/about”</code> so the route goes to the <code>Team</code> component instead of the <code>About</code> component when it sees  <code>“something.com/about/team”</code>. If it saw <code>“/about”</code> first, it would stop  there and render the <code>About</code> component because Switch only renders the  first component that matches.</p>
<h2 id="heading-default-route">Default Route</h2>
<p>A default route, or a “catch all” route, commonly used for 404 pages, is the route you use when none of the routes match.</p>
<p>In React Router v3, a default <code>Route</code> was:</p>
<p><code>&lt;Route path=”*” component={NotFound} /&gt;</code></p>
<p>In React Router v4, the default <code>Route</code> was changed to:</p>
<pre><code><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, 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">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">"/about/team"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Team}</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">component</span>=<span class="hljs-string">{NotFound}</span> /&gt;</span> // this is our default route
    <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  you don’t include a path in a <code>Route</code>, the component will always render.  So, as we discussed above, we can use <code>Switch</code> to get only one component  to render, and then place the “catch all” route very last (so it doesn’t  use that one before the <code>Router</code> gets a chance to check the rest of the  paths), so something will always render even if the other paths don’t  match.</p>
<h2 id="heading-onenter">onEnter</h2>
<p>Previously,  you could use <code>onEnter</code> to make sure the component of the <code>Route</code> has all  of the information it needs or as a check (such as to make sure the user  is authenticated) before the component rendered.</p>
<p>This  feature has been deprecated because the new structure of Routes is that  they should act like components, so you should take advantage of component lifecycle methods instead.</p>
<p>In React Router v3:</p>
<p><code>&lt;Route path=”/about” onEnter={fetchedInfo} component={Team}/&gt;</code></p>
<p>Becomes:</p>
<pre><code><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, 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">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">"/about/team"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Team}</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">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>
</code></pre><pre><code>...

componentDidMount() {
    <span class="hljs-built_in">this</span>.props.fetchInfo();
}

...
</code></pre><hr>
<h1 id="heading-props">Props</h1>
<p>In  React Router v4, the props passed through the router have changed, as  did the way they are accessed. The Route now passes three props:</p>
<ul>
<li><code>history</code></li>
<li><code>location</code></li>
<li><code>match</code></li>
</ul>
<h2 id="heading-history-1">history</h2>
<p><code>history</code> contains a lot of other properties and methods, so I won’t list all of  them, but here is a selection that might be most commonly used:</p>
<ul>
<li><code>length</code>: number of entries in the history stack</li>
<li><code>location</code>: contains the same information as below</li>
<li><code>push(path, [state])</code>: pushes new entry on history stack</li>
<li><code>goBack()</code>: allows you to move the pointer on the history stack back 1 entry</li>
</ul>
<p>It’s  important to note that <code>history</code> is mutable, and while it contains a <code>location</code> property, this instance of <code>location</code> shouldn’t be used since it  could have been changed. Instead, you want to use the actual <code>location</code> prop discussed below.</p>
<h2 id="heading-location">location</h2>
<p>The location has properties:</p>
<ul>
<li><code>pathname</code></li>
<li><code>search</code></li>
<li><code>hash</code></li>
<li><code>state</code></li>
</ul>
<p><code>location.search</code> is used to replace <code>location.query</code> and it must be parsed. I used  <code>URLSearchParams</code> to parse it. So a URL such as  <code>“https://something.com/about?string=’hello’”</code> would be parsed as such:</p>
<pre><code>...

const query = <span class="hljs-keyword">new</span> URLSearchParams(<span class="hljs-built_in">this</span>.props.location.search)
<span class="hljs-keyword">const</span> string = query.get(<span class="hljs-string">'string'</span>) <span class="hljs-comment">// string = 'hello'</span>

...
</code></pre><p>Additionally,  the <code>state</code> property can be used to pass the <code>location</code>-specific <code>state</code> of components through props. So, if you wanted to pass some information  from one component to another, you could use it like this:</p>
<pre><code>...
<span class="hljs-comment">// To link to another component, we could do this:</span>
&lt;Link to=<span class="hljs-string">'/path/'</span> /&gt;

<span class="hljs-comment">// However, if we wanted to add state to the location, we could do this:</span>
<span class="hljs-keyword">const</span> location = {
    <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/path/'</span>,
    <span class="hljs-attr">state</span>: { <span class="hljs-attr">fromDashboard</span>: <span class="hljs-literal">true</span> },
}
&lt;Link to={location} /&gt;
...
</code></pre><p>So, once we get to the component rendered by that path, we’ll have access to <code>fromDashboard</code> from <code>location.state.fromDashboard</code>.</p>
<h2 id="heading-match">match</h2>
<p><code>match</code> has the following properties:</p>
<ul>
<li><code>params</code>:  gets the dynamic segments of the path from the URL — for example if the  path is <code>“/about/:id”</code>, in the component, accessing  <code>this.props.match.params</code> will give you the id in the URL</li>
<li><code>isExact</code>: true if the entire URL was matched</li>
<li><code>path</code>: the path in the routes that was matched</li>
<li><code>url</code>: the matched portion of the URL</li>
</ul>
<h1 id="heading-redux-integration">Redux Integration</h1>
<p>As  I addressed earlier, we found that in our case, we didn’t need to have an additional library to connect <code>redux</code> with our router, especially since our main use case for this — Blocked Updates — was covered by react  router.</p>
<h2 id="heading-blocked-updates">Blocked Updates</h2>
<p>In  some cases, the app doesn’t update when the location changes. This is called a “Blocked Update”. This can happen if both of these conditions  are met:</p>
<ol>
<li>The component is connected to Redux via <code>connect()(Component)</code>.</li>
<li>The component isn’t rendered by a <code>&lt;Route&gt;</code></li>
</ol>
<p>In these cases, I wrapped the component’s connect with <code>withRouter</code>.</p>
<p>This  allowed the router information to follow the component when it gets linked to, so the app still updates when the Redux state changes.</p>
<hr>
<p>And that’s it!</p>
<p>This upgrade took me over a week — a few days of trying to figure out how to do it at all, and then another few days to start actually making changes. Upgrading to React Router 4 is a huge change not to be taken lightly, but it’ll make your router a lot more lightweight and easy to use.</p>
<p>Please don’t hesitate to comment/ask questions!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Beginner’s Guide to React Router 4 ]]>
                </title>
                <description>
                    <![CDATA[ By Emmanuel Yusufu React is a JavaScript library for building user interfaces. With the React paradigm, every piece of the UI is a component that manages its own self-contained state (data) and functions. React, like other front-end JavaScript framew... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/beginners-guide-to-react-router-4-8959ceb3ad58/</link>
                <guid isPermaLink="false">66c34570790a62b5fbf7b88d</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react-router-4 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 23 Dec 2017 18:15:30 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*pLzme1OXO4RQbO3eZROkgw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Emmanuel Yusufu</p>
<p>React is a JavaScript library for building user interfaces. With the React paradigm, every piece of the UI is a component that manages its own self-contained state (data) and functions.</p>
<p>React, like other front-end JavaScript frameworks, is useful for creating Single Page Applications (SPAs). These are web apps that do not need a full page reload on change of view. Instead they swap views into or out from a section of the page as the user navigates through the app.</p>
<p>Though SPAs provide a fluid navigation experience for users, routing features of traditional websites are expected.</p>
<p>For instance:</p>
<ul>
<li>every view on the screen should have its own specific URL so I can bookmark the page.</li>
<li>The forward and back button should move me forward or backward in my browsing history.</li>
<li>Nested views and those with parameters should be supported, such as <code>**example.com/products/shoes/101**</code>.</li>
</ul>
<p>In the React community, React Router is the favorite library for handling routing. The most compelling aspect of this version of the library is that it’s “just React”. The routes are just components that get rendered to the screen when the app is running. They are not defined in external files as done in other frameworks.</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<p>You will need the following: <strong>Basic knowledge of <a target="_blank" href="https://reactjs.org/">React</a></strong>, <a target="_blank" href="https://git-scm.com/"><strong>Git</strong></a> <strong>installed on your computer</strong>, and <a target="_blank" href="https://www.npmjs.com/"><strong>NPM</strong></a> <strong>installed on your compute</strong>r.</p>
<h3 id="heading-setting-up">Setting Up</h3>
<p>If you have Git installed locate the empty <a target="_blank" href="https://github.com/emmyyusufu/react-router-demos">source files</a> (at the <strong><em>master</em></strong> branch) and clone to your computer using:</p>
<pre><code>git clone https:<span class="hljs-comment">//github.com/emmyyusufu/react-router-demos.git</span>
</code></pre><p>Open the folder in your text editor and discover the sub-folders inside:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/XRn9UAhItLVs86chje0B66vJQMZPxQrrVZqG" alt="Image" width="587" height="164" loading="lazy"></p>
<p>This post is divided into four subsections according to the folders which are: <code>**Basic routing**</code>, <code>**Nested routing**</code>, <code>**Nested routing with path parameters**</code> and <code>**Authenticated routing**</code>.</p>
<p>To launch the demos, open a given folder in your terminal then run <code>npm install</code> followed by <code>npm start</code>.</p>
<h3 id="heading-1-basic-routing">#1 Basic routing</h3>
<p>Lets start out from scratch. Notice the folder structure of the basic routing folder.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/Pud2nRBawlErdg9HXTiHOvT-6ALjicE-sHv-" alt="Image" width="215" height="404" loading="lazy">
<em>Modified create-react-app folder structure.</em></p>
<p>All the demos in this post have been initially created using <a target="_blank" href="https://github.com/facebookincubator/create-react-app">create-react-app</a>. This brings some benefits such an already configured Webpack server that would bundle all the JavaScript file in our app into a <code>**bundle.js**</code> file that would be attached into the <code>**index.html**</code> file at run time. At run time Webpack’s dev server will listen to any change in our file and update it as the app runs during development.</p>
<p>I’ve created a <code>**components/**</code> folder to keep all our components. Note that:</p>
<ul>
<li><code>**index.js**</code> is the entry file to all <code>**.js**</code> files in our app. This is where Webpack’s bundling will be performed so all <code>**.js**</code> files should be imported into it.</li>
<li><code>**App.js**</code> file will contain everything pertaining to our React app.</li>
</ul>
<p>By default, create-react-app does not place <code>**App.js**</code> in this folder. But because I’ve modified the folder structure, I made the appropriate changes to the path url and imported it into <code>**index.js**</code>. To learn more about create-react-app, this <a target="_blank" href="https://github.com/facebookincubator/create-react-app">reference</a> would be helpful.</p>
<p>Navigate to the first folder <code>**01-basic-routing**</code> and run <code>npm install</code>.</p>
<p>Open the <code>App.js</code> file and you should see the following:</p>
<pre><code><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> <span class="hljs-string">'../styles/App.css'</span>;
</code></pre><pre><code><span class="hljs-comment">// import route Components here</span>
</code></pre><pre><code><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">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>
</code></pre><pre><code>&lt;div className=<span class="hljs-string">"container"</span>&gt;          <span class="xml"><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>Hello<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">""</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">""</span>&gt;</span>Books<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>          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">hr</span>/&gt;</span></span>
</code></pre><pre><code>{<span class="hljs-comment">/* Routes will go here */</span>}
</code></pre><pre><code>&lt;/div&gt;
</code></pre><pre><code>&lt;/div&gt;    );  }}
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><p>Run npm start and view the app. No changes made yet.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/fC9N5djph47YrTSWQ8jZLeDsoYR1FlkEQtch" alt="Image" width="751" height="368" loading="lazy">
<em>No changes made</em></p>
<p>Lets install React Router via NPM. With the folder open in your terminal, run:</p>
<pre><code>npm install react-router-dom
</code></pre><p>Why <code>**react-router-dom**</code>? This is because the React router library comprises of three packages: <code>react-router</code>, <code>react-router-dom</code>, and <code>react-router-native</code>.</p>
<p><code>react-router</code> is the core package for the router, whereas the other two are environment specific. You should use <code>react-router-dom</code> if you’re building for the web, and <code>react-router-native</code> if you’re on a mobile app development environment using React Native.</p>
<p>Import the following into <code>**App.js**</code></p>
<pre><code><span class="hljs-comment">// import route Components hereimport {  BrowserRouter as Router,  Route,  Link,  Switch,  Redirect} from 'react-router-dom'</span>
</code></pre><p>Later on we will understand what those components do. All of the routing components depend on <code>BrowserRouter</code> to provide the browser’s HTML5 History APIs to them.</p>
<p>Note that <strong>React components</strong> have their first letter capitalized so as to identify them differently from default HTML tags.</p>
<p><em>The History API is an object that lets us manage the current location via <code>history.location</code> as well as previous locations. Think of the <code>location</code> property of the object as an array. The current location is the last item on the array and we manipulate the array through methods such as <code>history.push()</code> or<code>history.replace</code>. Whatever manipulation is made on the array will trigger a page transition to the current location. This is what happens behind the scene when using <code>Link</code> and <code>Redirect</code> components as we’ll see soon.</em></p>
<p>We’ve imported the contents of <code>BrowserRouter</code> into the <code>Router</code> variable. We need to wrap our entire app with it so it supplies the needed APIs all through the app. In <code>**App.js**</code> add:</p>
<pre><code><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> <span class="hljs-string">'../styles/App.css'</span>;
</code></pre><pre><code><span class="hljs-comment">// import route Components hereimport {  BrowserRouter as Router,  Route,  Link,  Switch,  Redirect} from 'react-router-dom'</span>
</code></pre><pre><code><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">Component</span> </span>{  render() {    <span class="hljs-keyword">return</span> (      &lt;Router&gt;        &lt;div className="App"&gt;
</code></pre><pre><code>&lt;div className=<span class="hljs-string">"container"</span>&gt;            <span class="xml"><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>Hello<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">""</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">""</span>&gt;</span>Books<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>            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">hr</span>/&gt;</span></span>
</code></pre><pre><code>{<span class="hljs-comment">/* Routes will go here */</span>}
</code></pre><pre><code>&lt;<span class="hljs-regexp">/div&gt;        &lt;/</span>div&gt;      &lt;/Router&gt;    );  }}
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre><h4 id="heading-the-component">The <strong>&lt;Route</strong> /&gt; component</h4>
<p>Lets begin exploring the <code>_Route_</code> component. This component renders a page if the <strong>current URL location</strong> matches the <code>_path_</code> prop specified in it. It also accepts <code>_component_</code>, <code>_render_</code> and <code>[children](https://reacttraining.com/react-router/web/api/Route/children-func)</code> props.</p>
<p>Lets create ours where its written <strong>{/<em> Routes will go here </em>/}:</strong></p>
<pre><code>&lt;Route path=<span class="hljs-string">"/hello"</span> component={Hello} /&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">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</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">"/books"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Books}</span> /&gt;</span></span>
</code></pre><p>But those components don’t exist! Yes, you are right.</p>
<p>Again before we create them, lets add more imports to <code>App.js</code>. Import from the <code>HelloComponent.js</code>, <code>AboutComponent.js</code>, and <code>BooksComponent.js</code> using <code>Hello</code>, <code>About</code>, and <code>Books</code> as variables. <strong>The <code>component={}</code> prop uses curly braces to refer to variables not strings.</strong></p>
<pre><code><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> <span class="hljs-string">'../styles/App.css'</span>;
</code></pre><pre><code><span class="hljs-keyword">import</span> Hello <span class="hljs-keyword">from</span> <span class="hljs-string">'./HelloComponent'</span>;<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">'./AboutComponent'</span>;<span class="hljs-keyword">import</span> Books <span class="hljs-keyword">from</span> <span class="hljs-string">'./BooksComponent'</span>;
</code></pre><pre><code><span class="hljs-comment">// import route Components hereimport {  BrowserRouter as Router,  Route,  Link,  Switch,  Redirect} from 'react-router-dom'</span>
</code></pre><p>Note that <code>render</code> is similar to <code>component={}</code> but it lets us define a component inline and right there:</p>
<pre><code>&lt;Route path=<span class="hljs-string">"/hello"</span> render={<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> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>Hello, world!<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>             );      }}/&gt;
</code></pre><p>Head over to the empty <code>HelloComponent.js</code> file and paste:</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre><pre><code><span class="hljs-keyword">const</span> Hello = <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> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>Hello, world!<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>    );}
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Hello;
</code></pre><p>We used a Stateless functional component (hence the arrow function) above. We use them for components that render only static content to a webpage as opposed to components that render stateful/changing content.</p>
<p>If you didn’t notice, we are using Bootstrap 4 styles in our <code>App.css</code> file hence the <code>jumbotron</code> class in the <code>div</code>.</p>
<pre><code><span class="hljs-comment">// inside App.css. You'll need internet connection to load the Bootstrap 4 styles.</span>
</code></pre><pre><code>.App {  padding-top: <span class="hljs-number">50</span>px;}
</code></pre><pre><code>@<span class="hljs-keyword">import</span> url(<span class="hljs-string">'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css'</span>);
</code></pre><p>Head over to the empty <code>AboutComponent.js</code> file and paste:</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre><pre><code><span class="hljs-keyword">const</span> About = <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> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>About Me<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>    );}
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> About;
</code></pre><p>Finally, head over to the empty <code>BooksComponent.js</code> file and paste:</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre><pre><code><span class="hljs-keyword">const</span> Books = <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> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>My Books<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>    );}
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Books;
</code></pre><p>One more thing we need to wrap up this section is the <code>Link</code> component.</p>
<h4 id="heading-the"><strong>The &lt;/Link&amp;g</strong>t; Component</h4>
<p>This is replaces the default <code>&lt;a href=""&amp;g</code>t; HTML tag. I<code>t acc</code>epts a to="" prop that points to the URL location we want to go to.</p>
<p>Inside <code>App.js</code>, replace the default anchor tags with <code>Link</code>:</p>
<pre><code>&lt;ul&gt;   <span class="xml"><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">"/hello"</span>&gt;</span>Hello<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>   <span class="xml"><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>   <span class="xml"><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">"/books"</span>&gt;</span>Books<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>&lt;/ul&gt;
</code></pre><p>Run <code>npm start</code> from your terminal and see the complete app:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/Bikt0cXslah4RGYMXJaH9lahKt4XQDq-VHx2" alt="Image" width="800" height="368" loading="lazy">
<em>using Route and Link</em></p>
<p>How would you render a component if the <code>/</code> URL is visited, such as the landing or home page. Your guess may be to create a route for it:</p>
<pre><code>&lt;Route path=<span class="hljs-string">"/"</span> component={Home} /&gt;
</code></pre><p>This is fine but remember that the other paths have <code>/</code> in them. So if we visited other URLs such as <code>/hello</code>, <code>/about</code> and <code>/books</code>, the<code>Home</code> component will keep being rendered by default. To fix this, write another prop <code>exact</code> setting it to <code>true</code> or just write <code>exact</code>.</p>
<pre><code>&lt;Route path=<span class="hljs-string">"/"</span> exact={<span class="hljs-literal">true</span>} component={Home} /&gt;
</code></pre><p>This would ensure to <code>Home</code> component is rendered only in cases where the URL is exactly matches this:<code>/</code>.</p>
<p>Create a new <code>HomeComponent.js</code> file in <code>**components/**</code> folder. Paste this in:</p>
<pre><code><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre><pre><code><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> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>Landing 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>    );}
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Home;
</code></pre><p>Import into <code>App.js</code></p>
<pre><code><span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./HomeComponent'</span>;
</code></pre><p>Add to the list of routes</p>
<pre><code>&lt;Route exact={<span class="hljs-literal">true</span>} path=<span class="hljs-string">"/"</span> component={Home} /&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">"/hello"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Hello}</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">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</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">"/books"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Books}</span> /&gt;</span></span>
</code></pre><p>Visit <code>http://localhost:3000</code> and view:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ZsRK0kSHsjC4ktFeW7iP9tWRpKP49s-w7dWb" alt="Image" width="800" height="269" loading="lazy">
<em>Home component rendered on ‘/’ path</em></p>
<p>Do some experiments. Remove <code>exact={true}</code> from Home route and see what happens. You’ll see why its important.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/IZKcPI-ckbkiqnv-AcYaBJZSRLAmvObQe9VC" alt="Image" width="800" height="419" loading="lazy">
<em>Always use exact={true} on the ‘/’ path, else every &lt;Route /&gt; will render.</em></p>
<h4 id="heading-the-component-1">The  Component</h4>
<p>This will require being wrapped around <code>_Route_</code> components when needed for implementation. When a URL path is visited, it allows only the first <code>_&lt;Rou_</code>te&gt;that matches the path to be rendered.</p>
<p>Earlier we had an issue with <code>/</code> rendering the <code>Home</code> component and that of other paths. If we create a <code>/hello/goodmorning</code> path, what will happen? The components of the <code>/hello</code> and <code>/hello/goodmorning</code> path will be rendered. Switch will help in this case again by choosing only one route to render but the most important route must be arranged to come first.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/kko7Mjm1XatlbgdBczhMEYgt6-ty1jCnG6xn" alt="Image" width="800" height="391" loading="lazy">
<em>Just an experiment</em></p>
<p>Using <code>Switch</code>, we can avoid what happens in the image above but only for URLs other than <code>/</code>. <code>exact={true}</code> handles it for <code>/</code>. Remember that <code>Switch</code> will pick only the first matching <code>Route</code>. Lets put it to work and see the result.</p>
<pre><code>&lt;Switch&gt;    <span class="xml"><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>    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/hello"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Hello}</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">"/hello/goodmorning"</span> <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Goodmorning<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> }} /&gt;</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">"/books"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Books}</span> /&gt;</span></span>&lt;/Switch&gt;
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/Y3oc88nscEDu-lRKnUIdD6f9NvxgM9WBaANZ" alt="Image" width="800" height="387" loading="lazy">
<em>Only the first route that matches /hello/goodmorning is rendered.</em></p>
<p>Furthermore, <code>Switch</code> allows us to specify a route to render if the URL matches no location. For that route, leave the <code>path</code> prop empty.</p>
<pre><code><span class="hljs-comment">// Just an example. Don't implement. This catch-all Route would be at the bottom if implemented.</span>
</code></pre><pre><code>&lt;Route component={NoMatch} /&gt;
</code></pre><p>In summary <code>Switch</code> will do the following:</p>
<ul>
<li>avoid inclusive route rendering.</li>
<li>include a catch-all Route at the bottom of our Switch container.</li>
</ul>
<h3 id="heading-2-nested-routing">#2 Nested routing</h3>
<p>Remember that we could render components via <code>Route</code> inline or by specifying the component:</p>
<pre><code>&lt;Route component={SomeComponent}/&gt;
</code></pre><p>Or</p>
<pre><code>&lt;Route render={<span class="hljs-function">() =&gt;</span> { <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Soemthing<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span> }/&gt;
</code></pre><p>The component that will be created via <code>Route</code> will automatically be passed the following<code>prop</code> objects:</p>
<ul>
<li>match</li>
<li>location</li>
<li>history</li>
</ul>
<p>We will only explore the use of <code>match</code> as it is helpful for implementing nested routes. The <code>match</code> object contains the following properties:</p>
<ul>
<li><strong>params</strong> — (object) Key/value pairs parsed from the URL corresponding to the dynamic segments of the path.</li>
<li><strong>isExact</strong> — (boolean) true if the entire URL was matched (no trailing characters).</li>
<li><strong>path</strong> — (string) The path pattern used to match. Useful for building nested s</li>
<li><strong>url</strong> — (string) The matched portion of the URL. Useful for building nested s</li>
</ul>
<p>We want to add new routes under the <code>/book</code> route. They will be books:</p>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>React</li>
</ul>
<p>Navigate to the second sub-folder <code>**02-nested-routing**</code> on your terminal and run <code>npm install</code>.</p>
<p>In your code editor, open <code>BookComponent.js</code> and modify:</p>
<pre><code><span class="hljs-keyword">const</span> Books = <span class="hljs-function">(<span class="hljs-params">{ match }</span>) =&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">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>My Books<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>
</code></pre><pre><code>  &lt;div className=<span class="hljs-string">"container"</span>&gt;    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"row"</span>&gt;</span></span>
</code></pre><pre><code>      &lt;div className=<span class="hljs-string">"col-md-3"</span>&gt;          <span class="xml"><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>HTML<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>&gt;</span>CSS<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>&gt;</span>React<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>      &lt;/div&gt;      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-md-9"</span>&gt;</span></span>
</code></pre><pre><code>               {<span class="hljs-comment">/* place routes here */</span>}      &lt;<span class="hljs-regexp">/div&gt;    &lt;/</span>div&gt;
</code></pre><pre><code>   &lt;/div&gt;
</code></pre><pre><code>&lt;/div&gt;    );}
</code></pre><p>Lets install React Router via NPM. With the folder open in your terminal, run:</p>
<pre><code>npm install react-router-dom
</code></pre><p>We’ve demonstrated syntactically that the <code>match</code> object is passed as <code>props</code>. Remember that the classes used are for Bootstrap’s styles to take effect. Don’t forget to import all React Router components after importing React:</p>
<pre><code><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,    Route,    Link,    Switch,    Redirect  } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
</code></pre><p>We didn’t need to import all of them but we did anyway. Place the routes:</p>
<pre><code>&lt;Route path=<span class="hljs-string">""</span> render={<span class="hljs-function">() =&gt;</span> { <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>HTML by Ducket book<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span> }}/&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">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>CSS by Racheal Andrews<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> }}/&gt;</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">""</span> <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>React by Fullstack.io book<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> }}/&gt;</span>
</code></pre><p>We are using inline component rendering to save time. Now, lets populate the <code>to=""</code> of <code>Link</code> and <code>path=""</code> of <code>Route</code>.</p>
<p>Make these changes:</p>
<pre><code>&lt;div className=<span class="hljs-string">"col-md-3"</span>&gt;    <span class="xml"><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">match.url</span>}/<span class="hljs-attr">html</span>`}&gt;</span>HTML<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">match.url</span>}/<span class="hljs-attr">css</span>`}&gt;</span>CSS<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">match.url</span>}/<span class="hljs-attr">react</span>`}&gt;</span>React<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>&lt;/div&gt; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-md-9"</span>&gt;</span></span>
</code></pre><pre><code>      &lt;Route path={<span class="hljs-string">`<span class="hljs-subst">${match.path}</span>/html`</span>} render={<span class="hljs-function">() =&gt;</span> { <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>HTML by Ducket book<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span> }}/&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">match.path</span>}/<span class="hljs-attr">css</span>`} <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>CSS by Racheal Andrews<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> }}/&gt;</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">{</span>`${<span class="hljs-attr">match.path</span>}/<span class="hljs-attr">react</span>`} <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>React by Fullstack.io book<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> }}/&gt;</span>
</code></pre><pre><code>&lt;/div&gt;
</code></pre><p><code>${match.url}</code> evaluates to <code>/books</code> and <code>${match.path}</code> evaluates to <code>localhost://3000/books.</code> The back ticks used are ES6’s way of concatenating strings containing variables.</p>
<p>Give it a save, run <code>npm start</code> and view the working app.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/W0Cakd4qGPzbCGb0otMjwNHTNTg6z-Rc7i-o" alt="Image" width="800" height="452" loading="lazy">
<em>Nested routes</em></p>
<h3 id="heading-3-nested-routing-with-path-parameters">#3 Nested routing with path parameters</h3>
<p>Any URL that ends with <code>/:id</code>,<code>/:user</code> or <code>/:whatever</code> indicates that that portion is a dynamically generated part of the URL that could be any value.</p>
<p>We can access such portions via <code>match.params.id</code> for use in routing.</p>
<p>Again open the third sub-folder <code>**03-nested-routing-with-path-parameters**</code> in your terminal and run <code>npm install</code>.</p>
<p>Also, lets install React Router via NPM. With the folder open in your terminal, run:</p>
<pre><code>npm install react-router-dom
</code></pre><p>To illustrate the how path parameters can be used for routing, paste the following in <code>Book.js</code>:</p>
<pre><code><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,    Route,    Link,    Switch,    Redirect  } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
</code></pre><pre><code><span class="hljs-keyword">const</span> Books = <span class="hljs-function">(<span class="hljs-params">{ match }</span>) =&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">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>                <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>My Books<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>
</code></pre><pre><code>&lt;div className=<span class="hljs-string">"container"</span>&gt;          <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"row"</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-md-3"</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>`${<span class="hljs-attr">match.url</span>}/<span class="hljs-attr">html</span>`}&gt;</span>HTML<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">match.url</span>}/<span class="hljs-attr">css</span>`}&gt;</span>CSS<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">match.url</span>}/<span class="hljs-attr">react</span>`}&gt;</span>React<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">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">"col-md-9"</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">match.path</span>}/<span class="hljs-attr">html</span>`} <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>HTML by Ducket book<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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">match.path</span>}/<span class="hljs-attr">css</span>`} <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>CSS by Racheal Andrews<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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">match.path</span>}/<span class="hljs-attr">react</span>`} <span class="hljs-attr">render</span>=<span class="hljs-string">{()</span> =&gt;</span> { return <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>React by Fullstack.io book<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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">match.path</span>}/<span class="hljs-attr">:id</span>`} <span class="hljs-attr">component</span>=<span class="hljs-string">{Child}</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>            &lt;<span class="hljs-regexp">/div&gt;        &lt;/</span>div&gt;    );}
</code></pre><pre><code><span class="hljs-keyword">const</span> Child = <span class="hljs-function">(<span class="hljs-params">{ match }</span>) =&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">h3</span>&gt;</span>URL ID parameter: {match.params.id}<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>);
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Books;
</code></pre><p>Run <code>npm start</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/nFQfQLFAgCL1vj80YweZ630o3bzFqjTIOz9j" alt="Image" width="800" height="452" loading="lazy">
<em>accessing /:id parameter value</em></p>
<h3 id="heading-4-protected-path-routing">#4 Protected path routing</h3>
<p>This kind of routing is for pages of a website that need the user to login and be authentication before viewing such pages. An example is an <strong>Admin</strong> page.</p>
<p>To handle protected paths, we’ll need to use <code>&lt;Redirec</code>t/&gt; (a standard componen<code>t)and &lt;Priva</code>teRoute/&gt; (a custom component).</p>
<p><code>&lt;PrivateRout</code>e/&gt; is not the sta<code>ndard &amp;l</code>t;Route/&gt; component. The standard route component provided by Rea<code>ct Route</code>r is<code>;. We will defi</code>ne  as` our own custom .</p>
<p>Custom routes are necessary when we need to make a decision whether a <code>&lt;Rout</code>e/&gt; of interest should be rendered or not. As you’ll see in the code, we will <code>list &lt;Priva</code>teRoute/&gt; together <code>with ot</code>her s.</p>
<h3 id="heading-the-component-2"><strong>The  com</strong>ponent</h3>
<p>Rendering a  will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do.</p>
<p><code>&lt;Redirec</code>t/&gt; has a couple of props but we will be usin<code>g</code> the to object prop this way:</p>
<pre><code>&lt;Redirect to={{        <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/login'</span>,        <span class="hljs-attr">state</span>: { <span class="hljs-attr">from</span>: props.location }      }}/&gt;
</code></pre><p>When used, this will redirect to the <code>/login</code> path. Information about the last location before the redirect was done will be accessible by the <code>LoginPage</code> component via <code>this.props.location.state</code>.</p>
<p>Navigate to the last sub-folder <code>**04-authenticated-routing**</code>. Run <code>npm install</code>.</p>
<p>Install React Router via NPM. With the folder open in your terminal, run:</p>
<pre><code>npm install react-router-dom
</code></pre><p>Open <code>App.js</code> and add a new list <code>/admin</code> item to the existing ones.</p>
<pre><code>&lt;ul&gt;     <span class="xml"><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">"/hello"</span>&gt;</span>Hello<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>     <span class="xml"><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>     <span class="xml"><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">"/books"</span>&gt;</span>Books<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>     <span class="xml"><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">"/admin"</span>&gt;</span>Admin<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>&lt;/ul&gt;
</code></pre><p>Add <code>&lt;PrivateRout</code>e/&gt;<code>; and</code> /login route to the group of exi<code>sting &amp;l</code>t;Route/&gt;s.</p>
<pre><code>&lt;Switch&gt;            <span class="xml"><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>            <span class="xml"><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>            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/hello"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Hello}</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">"/books"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Books}</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">"/login"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Login}/</span>&gt;</span></span>            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">PrivateRoute</span> <span class="hljs-attr">authed</span>=<span class="hljs-string">{fakeAuth.isAuthenticated}</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/admin"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Admin}</span> /&gt;</span></span>          &lt;/Switch&gt;
</code></pre><p>Now create the <code>&lt;PrivateRout</code>e/&gt; component outsi<code>de</code> of App component:</p>
<pre><code><span class="hljs-keyword">const</span> PrivateRoute = <span class="hljs-function">(<span class="hljs-params">{ component: Component, ...rest }</span>) =&gt;</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> (    fakeAuth.isAuthenticated ? (      <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>        <span class="hljs-attr">pathname:</span> '/<span class="hljs-attr">login</span>',        <span class="hljs-attr">state:</span> { <span class="hljs-attr">from:</span> <span class="hljs-attr">props.location</span> }      }}/&gt;</span>    )  )}/&gt;</span>)
</code></pre><p><code>&lt;PrivateRout</code>e/&gt; will eventually render down <code>to a &amp;</code>lt;Route&gt; com<code>ponent.</code> The  component uses a ternary operation to determine what to render based on whether the user is lo<code>gged in or</code> not: a &lt;Redirect/&amp;g<code>t; to</code> the login page or Admin page component.</p>
<p>Create the <code>Admin</code> component:</p>
<pre><code><span class="hljs-keyword">const</span> Admin = <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> <span class="hljs-attr">className</span>=<span class="hljs-string">"jumbotron"</span>&gt;</span>      <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"display-3"</span>&gt;</span>Admin Access granted<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>  );}
</code></pre><p>Also, create the <code>Login</code> component:</p>
<pre><code><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Login</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{      <span class="hljs-keyword">constructor</span>() {      <span class="hljs-built_in">super</span>();        <span class="hljs-built_in">this</span>.state = {        <span class="hljs-attr">redirectToReferrer</span>: <span class="hljs-literal">false</span>      }      <span class="hljs-comment">// binding 'this'      this.login = this.login.bind(this);    }      login() {        fakeAuth.authenticate(() =&gt; {        this.setState({ redirectToReferrer: true })      })    }      render() {      const { from } = this.props.location.state || { from: { pathname: '/' } }      const { redirectToReferrer } = this.state;        if (redirectToReferrer) {        return (          &lt;Redirect to={from} /&gt;        )      }        return (        &lt;div className="jumbotron"&gt;            &lt;h1 className="display-3"&gt;Login required&lt;/h1&gt;            &lt;p className="lead"&gt;You must log in to view the page at {from.pathname}.&lt;/p&gt;            &lt;p className="lead"&gt;              &lt;a className="btn btn-primary btn-lg" onClick={this.login} role="button"&gt;Login&lt;/a&gt;            &lt;/p&gt;        &lt;/div&gt;      )    }  }    /* A fake authentication function */  export const fakeAuth = {      isAuthenticated: false,    authenticate(cb) {      this.isAuthenticated = true       setTimeout(cb, 100)    },  }</span>
</code></pre><p>This <code>Login</code> component implements a fake authentication function that will set a user either logged in or out.</p>
<p>Run <code>npm start</code> and see the working app.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/PzSzkdxoxPdjdiLrnpGfrcPyxeGPSyElyqeg" alt="Image" width="800" height="452" loading="lazy">
<em>Authentication process</em></p>
<p>This brings us to the end of the article. Kudos to you if you made it this far. If you would like more details about React Router, view the <a target="_blank" href="https://reacttraining.com/react-router/web/guides/philosophy">Documentations</a>.</p>
<p>If you would like the completed version of the code, visit the completed branch on <a target="_blank" href="https://github.com/emmyyusufu/react-router-demos/tree/completed">Github</a>.</p>
<p>Feel free to support me (<a target="_blank" href="http://www.devapparel.co">devapparel.co</a>) and look good while at it. Also, Comment or Share this post. Thanks for reading!</p>
<p><em>Originally published at <a target="_blank" href="https://www.zeolearn.com/magazine/beginners-guide-to-react-router-4">Zeolearn Blog</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
