In this article, we’ll cover the basics of React Router v6. We’ll explain what it is and how we install it.
We use React Router for a multi-page functionality to develop websites with React.
React is easy to implement. It offers a wide range of node packages that help in development. It offers speed and quality.
The only drawback with React is that it lacks multi-page functionality. To overcome this, we use an npm
package React-router
.
Open the project terminal and install the npm
package in the client folder.
npm i react-router-dom@6
Note: In the code widget above, @6 means that we’ll download the latest version at the moment that is version 6.
Now that we’ve installed the dependency, we’ll create some routes and links to jump between the pages. We’ll follow the steps below to do this:
First, we’ll create the application components. The structure of this app is as follows:
We'll proceed to create the components needed for this example.
Here we'll make React Router accessible to all the components in our app.
BrowserRouter
to the index.js file:import {BrowserRouter} from 'react-router-dom';
<App />
component with <BrowserRouter>
:<React.StrictMode><BrowserRouter><App /></BrowserRouter></React.StrictMode>
Routes
and the Route
from the React Router dom to create our pages. The <Routes>
will wrap the <Route>
nested within that will be the pages. import {Routes, Route} from 'react-router-dom'
<Routes><Route path='/home' element={<Home />} /><Route path='shop' element={Shop />} /><Route path='/cart' element={<Cart />} /></Routes>;
The nav component will be in charge of containing the links that will take us to the different pages within our application. To do this, we will:
<Link>
from react router dom.href
attribute, we replace it with the to=''
.<Link to='/'>Home</Link>
The '/'
means that we are targeting the home page, the index. So if we want to go to another page that is not the home page, instead of using just '/'
we need to add to it the name of the route that we created in the app.js. In this case '/shop'
and '/cart'
.
The component should look like this:
import { Link } from "react-router-dom";export function Nav() {return (<nav><Link to='/'>Home</Link><Link to='/shop'>Shop</Link><Link to='/cart'>Cart</Link></nav>);}
If we remember correctly, we use href
in common websites built in JavaScript to go from one page to another, redirecting us to another tab, making the website reload.
React works different since it is a single-page application that only reloads/updates the dynamic components and doesn't touch the component that is not changed.
Therefore, we can’t use the anchor tag with the href
attribute. It’s easy to work with routes in React. In this article, we highlighted version 6 of React Router, which we can modify and will be useful in the future.
Now is the time to put everything into practice and make it work. Below is the entire project:
// jest-dom adds custom jest matchers for asserting on DOM nodes. // allows you to do things like: // expect(element).toHaveTextContent(/react/i) // learn more: https://github.com/testing-library/jest-dom import '@testing-library/jest-dom';
index.js
: We import BrowserRouter
in line 4, then wrap the <App/>
component with it in lines 10-12 to make all the components accessible to react router.app.js
: We added some routes in lines 15-19 linking us to the Home, Shop, and Cart page, respectively, importing the components in lines 5-8. Also imported Routes, Route
from react-router-dom
.nav.js
: Added some Links
that allows moving within the website in lines 8-10. These act like the anchor tag and href
attribute.home.js
, shop.js
, and cart.js
have some boilerplates to identify which page we are in. In line 6 each one of them, we added the <h1>
with the corresponding page name.