React Router is a library that allows us to make our single page React applications mimic the behavior of multipage apps. It provides the ability to use browser history, allowing users to navigate with forward / back buttons and bookmark links to specific views of the app. Most modern sites use some form of routing. React Router exposes this functionality through a series of components. Let's start by looking at the overall structure of an app using router:
1. Take a look at the quick start page of the React Router docs. Take note of the syntax and organization of the page. No worries if this looks unclear right now! (nothing to answer here)
React Router DOM
npm install react-router-dom
- At the top of the file import react router DOM:
import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
React Router provides a series of helpful components that allow our apps to use routing. These can be split into roughly 3 categories:
- Routers
- Route Matcher
- Route Changers
Any code that uses a React-Router-provided component must be wrapped in a router component. There are lots of router components we can use, but we'll focus on one in particular. Let's look into the docs to learn more.
It is a <Router>
that uses the HTML5 history API to keep your UI in sync with the URL.
- pushState
- replaceState
- the popstate event
All of the components that you use in a web application should be imported from react-router-dom:
<BrowserRouter
basename={optionalString}
forceRefresh={optionalBool}
getUserConfirmation={optionalFunc}
keyLength={optionalNumber}
>
<App />
</BrowserRouter>
A <BrowserRouter>
uses regular URL paths. These are generally the best-looking URLs, but they require your server to be configured correctly.
- Users can use urls to bookmark pages
- Users can use the back or forward button
- Users can easily share content from a page in the app
Render some UI when a location matches the route’s path.
When a location matches the route’s path.
Switch Renders the first child or that matches the location. is unique in that it renders a route exclusively (only one route wins).
When a <Switch>
is rendered, it searches through its children <Route>
elements to find one whose path matches the current URL. When it finds one, it renders that <Route>
and ignores all others
- This means that you should put s with more specific (typically longer) paths before less-specific ones.
<Link>
component creates links in your application. Wherever you render a <Link>
, an anchor <a>
will be rendered in your HTML document. User can interact by clicking on it.
<Link to="/">Home</Link>
// <a href="/">Home</a>
The is a special type of that can style itself as “active” when its to prop matches the current location. User can click on it.
<NavLink to="/react" activeClassName="hurray">
React
</NavLink>
// When the URL is /react, this renders:
// <a href="/react" className="hurray">React</a>
// When it's something else:
// <a href="/react">React</a>
Any time that you want to force navigation, you can render a . When a renders, it will navigate using its to
prop.
<Redirect to="/login" />