2.2.3: React Router
- 1.React Router allows us to keep our app URLs in sync with components we are viewing.
- 2.How implement React Router in our applications.
- 3.How to control user flow with React Router.
- 4.How to implement Private Routing within our applications.
React Router is a React library that enables us to keep our app URLs in sync with components we are viewing. Prior to React Router we needed to extensively add supplementary code to our app so that our URL did update, which is fine for small single-page apps but less so when our apps gets more complex.
Scroll through the React Router homepage to become familiar with the implementation concepts that are used by this package. The implementation that follows is Rockets way of giving you routes in React quickly and without fuss.
Complete the official React Router tutorial to familiarise yourself with React Router. Once we learn the mechanics of React Router we will integrate it into our apps with Create React App.

Feature Overview v6.6.1
remix_run
React Router Official Tutorial
- 1.We will not be using Remix at Rocket, the other framework mentioned in the tutorial
- 2.We will not be using createBrowserRouter, instead we use BrowserRouter its implementation is shown below
- 3.Pay attention to how we can use nested
Route
s - 4.Pay attention to how the
Outlet
component renders out sub components - 5.Always add the "no match" case in our apps for robustness
- 6.Note we need to import the React Router React Hook
useParams
fromreact-router-dom
to get URL params - 7.We can skip the section on Custom Behaviour; those use cases will most likely not apply to us and we can come back to this when we need to.
Rocket advises using the following setup when developing Routing in React, this setup is straightforward and easy to understand. Read more about the implementation here.
BrowserRouter This tool used to update the current url path that is rendered in the browser. All of the
Routes
, Route
, Link
's and React components that are nested will be connected via React Routing, by React Router Dom.Routes This component matches the current url to the component that should be rendered,
Routes
will render the component within the Route
with the matching path parameter. Only one component will be rendered unless nested routes are implemented.Route These components are used to define the component or components that will render depending on the current url path that is in the browser. If an
Outlet
is contained in this component, nested Route
components may also be rendered onto the screen.Outlet This component is used so that multiple path components can be rendered within a
Routes
Component. This makes rendering children of routes easy.Link This component allows our users to navigate through the application such that they can visit every path and therefore every component. The links have a to prop which should match a
Route
's path prop. The implementation below is how we can develop a routing system within our React applications using the
BrowserRouter
, you can use the navigate hook as well as Link
s within the BrowserRouter
. You will need to clone this repo, to see how this application functions. Remember to install your node_modules. Notice how
react-router-dom
is listed as a dependency in the package.json
.Simple Example
1
2
<BrowserRouter>
3
4
<div>
5
<Link to="/">Home</Link>
6
<Link to="/fruits">Fruit</Link>
7
<Link to="/fruits/form"> Fruit Form</Link>
8
<Link to="/fruits/list">Fruit Lists</Link>
9
</div>
10
11
<Routes>
12
13
<Route path="/" element={<Home />} />
14
15
<Route path="/fruits" element={<Fruits />}>
16
<Route path="form" element={<AddFruit />} />
17
<Route path="list" element={<List />} />
18
</Route>
19
20
<Route path="*" element={<Error />} />
21
</Routes>
22
23
</BrowserRouter>
24