LetĀ travel back to the very beginnings of the World Wide Web. š¤
At that time,Ā most websites consisted of a group of pages sent by the server, which rendered as users navigated through the site. For every interaction, like sending a form, the whole page had to be refreshed.
However, at the start of the new millennium, the concept of single-page applications (SPA) started to emerge. The main ideas behind this concept are:
Users only load a web page once (the famous Ā index.htmlĀ ).
Rather than getting every page with an API call, you get them bit by bit,Ā making user interaction much more dynamic.
As users navigate between different pages, JavaScript (in our case, React) manages the rendering of new pages within the same domain without needing the page to refresh completely .Ā

Does that mean that SPA is better? Are all new sites coded to be SPA?
Well, no. Not all applications are SPA. There are certain drawbacks to be aware of when you code a website as a single-page application. In particular, your users will have to have JavaScript for your site to work, and search engine optimization (SEO) is harder for single-page applications.
The projects made with Create React App arenāt considered single-page applications as they lack aĀ routingĀ solution.
Unlike frameworks such as Angular, React does not directly provide a solution for managing your applicationās routes. Not a problem, though ā like with almost everything in React, the ecosystem quickly addressed this need, and now several routing solutions exist. We'll work with React Router (well-named, don't you think? š) in this course.
What exactly is a route?
AsĀ you can see in the React Router documentation, a route allows you to conditionally render components if the URL path matches the route path.
You pass the path that the route matches as a prop, which takes care of rendering the children that are passed to it.
This library, created by React Training, provides access to all of the tools needed to manage client-side, in-app navigation.Ā
So letās get familiar with React Router. š
Start by installing the library with Ā yarn add react-router-domĀ . If you want to learn more about its configuration,Ā read the React Router documentation.
You can now start using React Router! š
So far,Ā Ā HomeĀ is the only feature, so letās create a new component for the survey.
To do this,Ā make aĀ Ā Survey.jsxĀ Ā file inĀ Ā pagesĀ . For now,Ā let's keep the component simple:
function Survey() {
return (
<div>
<h1>Survey š§®</h1>
</div>
)
}Your mission is to enableĀ navigation between the homepage and the survey.Ā šµļøāāļø
As youāve probably already guessed, to do this, youāre going to use React Router and its Router and Route components.
Letās start with the router. Place it at the root of the component tree to cover all of the routes that you define.
Now you have the Ā index.jsxĀ file at the project route:Ā
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom'
ReactDOM.render(
<React.StrictMode>
<Router>
<Home />
</Router>
</React.StrictMode>,
document.getElementById('root')
)And now put all of the routes that will be accessible into the router.

So letās create a route for the homepage and survey.
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom'
ReactDOM.render(
<React.StrictMode>
<Router>
<Route path="/">
<Home />
</Route>
<Route path="/survey">
<Survey />
</Route>
</Router>
<React.StrictMode>
)If you head toĀ http://localhost:3000/, youāll see the homepage. Itās a different story for http://localhost:3000/survey, though⦠Uh oh! Both are rendering! It's OK. All you need to do is add the prop Ā exactĀ in your route forĀ Ā HomeĀ :
<Route exact path="/">
<Home />
</Route>It works perfectly! š

However, itās not really practical to type all of your URLs into the address bar to load the page. š
So letās createĀ a header with links to different pages in the app.
In theĀ Ā /components folderĀ , create a newĀ Ā /HeaderĀ folder with an Ā index.jsxĀ file inside it, which gives youĀ Ā /components/Header/index.jsxĀ :
import { Link } from 'react-router-dom'
function Header() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/survey">Survey</Link>
</nav>
)
}
export default HeaderI usedĀ Ā LinkĀ , which comes from React Router and behaves like an Ā anchorĀ tag. It is very important to use this to navigate so that your app is accessible (and not use redirections triggered by Ā onClickĀ ).
Letās now useĀ Ā HeaderĀ inĀ Ā index.jsxĀ at the project root:
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router, Route } from 'react-router-dom'
import Home from './pages/Home'
import Survey from './pages/Survey'
import Header from './components/Header'
ReactDOM.render(
<React.StrictMode>
<Router>
<Header />
<Route exact path="/">
<Home />
</Route>
<Route path="/survey">
<Survey />
</Route>
</Router>
</React.StrictMode>,
document.getElementById('root')
)You now have the app base with navigation ā congratulations! Great work! š
Youāve seen how to set up routing. Next, youāll learn how to split your router when you have many routes to manage, for example, in a larger code project. Follow me in the screencast below!
Navigation inĀ the application isĀ functioning well.

But what ifĀ I want to pass parameters? For example, when I do the survey, to get each question number from the URL?
Good question! The router allows you to get parameters. To do this, you simply have to write your route like this in the Ā index.jsxĀ file at the Ā /src routeĀ :
<Route path="/survey/:questionNumber">
<Survey />
</Route>And then put a question number in Ā components/Header/index.jsxĀ :
function Header() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/survey/42">Survey</Link>
</nav>
)
}YouĀ get this parameter in Ā survey/index.jsxĀ using the hookĀ Ā useParamsĀ Ā provided by React Router:
import { useParams } from 'react-router-dom'
function Survey() {
const { questionNumber } = useParams()
return (
<div>
<h1>Survey š§®</h1>
<h2>Question {questionNumber}</h2>
</div>
)
}Congratulations! Youāve now got your question number as a parameter. āļø
Amazing ā everything is working as hoped! But what happens if you type anything into the URL? For example, if you try to access http://localhost:3000/helloHowAreYou?
You can see the header, but nothing else. Wouldnāt it be better to signal to the user that nothing exists at this address? Have you ever heard of error pages? Thatās what weāre going to do here: display a 404 page.
Start by creating a simple Ā ErrorĀ component in Ā components/Error/index.jsxĀ :
function Error() {
return (
<div>
<h1>Oops š This page doesnāt exist</h1>
</div>
)
}
export default ErrorGoing back to the router, youāll need to useĀ SwitchĀ from React Router: Ā SwitchĀ allows you to render only the first route that matches the path, and you add a route to which you donāt pass a path prop.
So in the router, that gives you:
ReactDOM.render(
<React.StrictMode>
<Router>
<Header />
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/survey/:questionNumber">
<Survey />
</Route>
<Route>
<Error />
</Route>
</Switch>
</Router>
</React.StrictMode>,
document.getElementById('root')
)Letās test it inĀ the browser:

Yes! šŖ

Now thatĀ you have your application base, itās time to see how youĀ do on your own.
Youāll find the base for the exercise on branch P1C3-begin. Your goals are to:
Create a newĀ Ā ResultsĀ page and add it to the router.
Create a newĀ Ā FreelancersĀ page, add it to the router, and create a link in the Ā HeaderĀ .
In Ā Survey.jsxĀ , code a link forĀ Ā backĀ andĀ one forĀ Ā nextĀ to allow users to move onto the next question or back to the previous one.Ā
On question 1, the back link should not be active.Ā
On question 10, the next link should not be displayed. Instead, a Results linkĀ shouldĀ redirect to the Results page.
You can find the solution on branch P1C3-solution.
With single-page applications (SPAs), the user has the impression of navigating between different pages. However, only a single HTML page exists, onto which you graft the content with JavaScript.
React Router is one of the libraries you can use to turn a React app into a SPA.Ā
The router, routes, and switch manage the rendering of different pages.
It is possible to pass parameters in a route and then get them with Ā useParams()Ā .
By specifying a route with no path at the end of the switch, you capture all routes with a path that doesn't match any declared route, creating a 404 page.
The app now contains all of the pages for the next step! š In the following chapter, you'll see how to secure props with PropTypes. See you there!