Skip to content
tezvyn:

Primary roles of BrowserRouter, Routes, and Route, and basic route setup

Source: reactrouter.comEasyHow cards are made

Primary roles of BrowserRouter, Routes, and Route, and basic route setup

Tests v6 routing hierarchy. BrowserRouter provides history; Routes picks one match; Route maps path to element. Wrap Routes in BrowserRouter with nested Route elements for / and /about. Red flag: omitting the router or confusing Routes with Switch.

What's really being asked

The interviewer wants to know if you understand the separation of concerns in React Router v6 at a component level. Specifically, they are checking that you know which piece owns the browser history API, which piece performs matching, and which piece declares individual mappings. They also want to see if you can compose these three primitives into a working route table without relying on copy-paste boilerplate you do not understand.

The full answer

Four things in order. First, BrowserRouter is the root provider that creates a history object and subscribes to the browser's URL so the entire app can react to navigation without page reloads. Second, Routes is a matching engine that looks at the current location and renders exactly one of its child Route elements, choosing the best match by rank. Third, Route is a declarative unit that associates a URL path with a React element via the path and element props. Fourth, the wiring is done by nesting Route components inside Routes and then wrapping the whole tree in BrowserRouter at the top level of the application.

The mistakes people make

Several patterns signal shallow knowledge. One is omitting BrowserRouter entirely and expecting Routes to work, which fails because no routing context exists. Another is using component or render props from v5 instead of the element prop, which is the v6 API. A third is confusing Routes with the old Switch component and not knowing that Routes is smarter about relative links and ranked matching. Finally, some candidates place Routes inside every page instead of once at the app root, which breaks nested route composition.

What usually comes next

The interviewer may ask how nested routes work in v6, what happens if two Route components both match, or how you would navigate programmatically instead of with a Link. They might also ask about the difference between BrowserRouter and HashRouter, or when you would reach for createBrowserRouter instead of the declarative components.

A concrete example

A minimal implementation looks like this. In index.js, render App wrapped by BrowserRouter. In App.js, return a Routes block containing two Route elements. The first Route has path="/" and element={<Home />}. The second Route has path="/about" and element={<About />}. When the user visits the root URL, Routes selects the home mapping. When they visit slash about, it selects the about mapping. No full page refresh occurs because BrowserRouter intercepts the history change.

Interview question

In React Router v6, when two Route paths could both match the current URL, which component picks exactly one to render?

  • a.Routes, because it ranks all child matches and renders only the best fitCorrect
  • b.Switch, because it replaces Routes in v6 to handle exclusive rendering
  • c.BrowserRouter, because it creates the history object and resolves which path should win
  • d.Route, because it declares the path-to-element mapping and controls its own visibility
Why?

Routes is the matching engine that ranks its child Route elements and renders exactly the best fit. Switch is the old v5 component; it does not replace Routes in v6, which uses ranked matching rather than simply stopping at the first match.

Just read this? Test yourself on what you have been reading.

Read the original → reactrouter.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles