Setting Up React Router
Explore the process of setting up React Router with TypeScript in a React frontend project. Learn how to create blank pages and declare routes using BrowserRouter, Routes, and Route components to enable smooth multi-page navigation within a single-page application.
We'll cover the following...
In this section, we are going to install React Router with the corresponding TypeScript types by carrying out the following steps:
Make sure the frontend project is open in Visual Studio Code and enter the following command to install React Router in the terminal:
Note: Make sure react-router-dom version 6+ has been installed and listed in
package.json. If version 5 has been installed, then version 6 can be installed by runningnpm install react-router-dom@next.
React router has a peer dependency on the
historypackage, so let’s install this using the terminal as well:
A peer dependency is a dependency that is not automatically installed by npm. This is why we have installed it in our project.
That’s it—nice and simple! We’ll start to declare the routes in our app in the next section.