Facebook Pixel

How to Set Up React Router in Your React App From Scratch

A step-by-step guide to setting up React Router in a React app, including routes, links, and a 404 page.

How to Set Up React Router in Your React App From Scratch

Setting up React Router is straightforward once you know the steps. Here is how to do it from scratch.

Step 1: Install React Router

Install the package with npm. Use the main react-router-dom package for web apps.

Step 2: Wrap Your App in a Router

At the root of your app, wrap your components in a BrowserRouter. This enables routing throughout the app and connects React Router to the browser history.

Step 3: Define Routes

Inside the router, define Routes that map paths to components. Each Route has a path and an element to render when the path matches.

Step 4: Add Links

Use the Link component for navigation instead of plain anchor tags. Link updates the URL without a full page reload, which is what makes client-side routing work.

Step 5: Add a 404 Page

Add a catch-all route at the end that renders a not-found component when no path matches. This handles invalid URLs gracefully.

Step 6: Use Nested Routes for Layouts

For shared layouts, define a parent route with an Outlet, and nest child routes inside it. The layout renders once, and children render in the Outlet.

The Common Mistake

Forgetting to wrap the app in a Router, or using plain anchor tags instead of Link, which causes full page reloads and breaks the single-page experience.

The Takeaway

Install the package, wrap your app in a Router, define Routes that map paths to components, use Link for navigation, add a 404, and use nested routes for shared layouts. That is the full setup.

Install react-router-dom, wrap your app in a BrowserRouter, define Routes that map paths to components, use Link for navigation instead of anchor tags, add a catch-all 404 route, and use nested routes with an Outlet for shared layouts.

Because the router enables routing throughout the app and connects React Router to the browser history. Without it, Routes and Links do not work, because there is no router context to manage the URL.

Because Link updates the URL without a full page reload, which is what makes client-side routing work. Plain anchor tags trigger a full reload, which breaks the single-page experience and loses your app state.

Add a catch-all route at the end of your Routes that renders a not-found component when no other path matches. This handles invalid URLs gracefully instead of showing a blank page.

Define a parent route that renders a layout component with an Outlet inside it, and nest child routes inside that parent. The layout renders once, and the matched child renders in the Outlet.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.