React Router 101: Adding Navigation to Your React App

Welcome to this tutorial on React Router! React Router is a popular library for adding navigation to a React app. In this tutorial, we will explore the basics of React Router and how to use it to add navigation to a React app.

First, let's start by installing React Router. If you are using a package manager like npm or yarn, you can install React Router by running the following command:

  • BnBtIGluc3RhbGwgcmVhY3Qtcm91dGVyLWRvbQ==

  • EWFybiBhZGQgcmVhY3Qtcm91dGVyLWRvbQ==

Next, let's create a simple app with two pages, a home page and an about page. Here's an example of how you can use React Router to add navigation to your app:

AW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JzsKaW1wb3J0IHsgQnJvd3NlclJvdXRlciBhcyBSb3V0ZXIsIFJvdXRlLCBMaW5rIH0gZnJvbSAncmVhY3Qtcm91dGVyLWRvbSc7Cgpjb25zdCBIb21lID0gKCkgPT4gPGgxPkhvbWU8L2gxPjsKY29uc3QgQWJvdXQgPSAoKSA9PiA8aDE+QWJvdXQ8L2gxPjsKCmNvbnN0IEFwcCA9ICgpID0+ICgKICA8Um91dGVyPgogICAgPGRpdj4KICAgICAgPG5hdj4KICAgICAgICA8dWw+CiAgICAgICAgICA8bGk+CiAgICAgICAgICAgIDxMaW5rIHRvPSIvIj5Ib21lPC9MaW5rPgogICAgICAgICAgPC9saT4KICAgICAgICAgIDxsaT4KICAgICAgICAgICAgPExpbmsgdG89Ii9hYm91dCI+QWJvdXQ8L0xpbms+CiAgICAgICAgICA8L2xpPgogICAgICAgIDwvdWw+CiAgICAgIDwvbmF2PgogICAgICA8Um91dGUgZXhhY3QgcGF0aD0iLyIgY29tcG9uZW50PXtIb21lfSAvPgogICAgICA8Um91dGUgcGF0aD0iL2Fib3V0IiBjb21wb25lbnQ9e0Fib3V0fSAvPgogICAgPC9kaXY+CiAgPC9Sb3V0ZXI+Cik7CgpleHBvcnQgZGVmYXVsdCBBcHA7

In this example, we have created two functional components called Home and About that renders a simple heading element. We are using the BrowserRouter component from React Router to wrap our app and the Route component to define our application's routes. The Link component is used to create links between our routes.

In the App component, we have a nav element with a list of links to our routes, and we are using the Route component to render the corresponding component for each route. The exact prop is used to specify that the Home route should only be matched if the path is exactly "/".

This is just a simple example of how you can use React Router to add navigation to your app, but there are many other features and options available. Some other things you might want to consider when using React Router include:

  • Adding routes with dynamic parameters. You can use the path prop to specify a dynamic parameter in a route and use the match the object passed to the component to access the parameter value.

  • Using nested routes. You can use the Switch component to specify a group of routes that should only match if the path matches exactly, and you can use the Route component's children prop to render nested routes.

  • Protecting routes with authentication. You can use the PrivateRoute component from the react-router-dom library to create routes that are only accessible to authenticated users.

We hope this tutorial has been helpful in getting you started with React Router. If you have any questions or would like to learn more, there are many resources available online that can provide further guidance and information on this topic. Some useful resources for learning more about React Router include the official documentation and examples, as well as online tutorials and blog posts from other developers.

Want us to build yours instead?

On a project, every milestone ends with a link to the working build, and the code, the accounts and every password are yours at the handover.

What we’d build for you is on Services.

Send us the brief, or start with the free call.

Page updated .

Privacy preferences

Choose whether this browser sends visit data to our analytics tools. The site works the same either way.

Google Analytics, Amplitude and Microsoft Clarity show us which pages get read. Off means none of the three loads.

Always on

Sentry error reports, with personal data removed, and the Cloudflare spam check on the brief form.

Each cookie is listed in our Cookie Policy.