Consider this React component using React Router v6:
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<nav>
<Link to='/home'>Home</Link>
<Link to='/about'>About</Link>
</nav>
<Routes>
<Route path='/home' element=<h1>Home Page</h1> />
<Route path='/about' element=<h1>About Page</h1> />
</Routes>
</BrowserRouter>
);
}If the user navigates to /about, what will be displayed?
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom'; function App() { return ( <BrowserRouter> <nav> <Link to='/home'>Home</Link> <Link to='/about'>About</Link> </nav> <Routes> <Route path='/home' element={<h1>Home Page</h1>} /> <Route path='/about' element={<h1>About Page</h1>} /> </Routes> </BrowserRouter> ); }
Remember that the Routes component renders the matching Route element based on the URL path.
The BrowserRouter wraps the app and enables routing. The nav always shows the two links. When the URL is /about, the Route with path /about matches and renders its element, which is the heading 'About Page'.
In React Router v6, how do you correctly nest a child route inside a parent route?
Nested routes use relative paths without a leading slash and the element prop, not component.
Option C correctly nests the 'stats' route inside 'dashboard' using a relative path and the element prop. Option C defines separate routes but not nested. Option C uses an absolute path for the child which breaks nesting. Option C uses the deprecated component prop.
Given this React Router snippet:
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Link to='/profile?tab=info'>Profile Info</Link>
<Routes>
<Route path='/profile' element=<Profile /> />
</Routes>
</BrowserRouter>
);
}
function Profile() {
return <h2>User Profile</h2>;
}What will the browser URL be after clicking the 'Profile Info' link?
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Link to='/profile?tab=info'>Profile Info</Link> <Routes> <Route path='/profile' element={<Profile />} /> </Routes> </BrowserRouter> ); } function Profile() { return <h2>User Profile</h2>; }
Query parameters start with a question mark ? in URLs.
The to prop in the Link includes the query string ?tab=info. Clicking the link updates the URL to include this query string after the path.
Look at this React Router v6 code snippet:
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element=<Home /> />
<Route path='/about' element=<About /> />
</Routes>
</BrowserRouter>
);
}
function Home() {
return <h1>Welcome Home</h1>;
}
function About() {
return <h1>About Us</h1>;
}When the app loads at the root URL /, the page is blank. What is the most likely cause?
import { BrowserRouter, Routes, Route } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Routes> <Route path='/' element={<Home />} /> <Route path='/about' element={<About />} /> </Routes> </BrowserRouter> ); } function Home() { return <h1>Welcome Home</h1>; } function About() { return <h1>About Us</h1>; }
React Router v6 matches routes partially by default. Check if the root route is rendered correctly.
React Router v6 matches routes partially, so the path '/' matches all routes starting with '/'. The blank page is likely caused by missing a default route or a layout component. However, in this simple example, the code should render the Home component at '/'. The blank page might be caused by missing a React.StrictMode wrapper or a rendering issue outside routing.
In React Router v6, what happens when you call const navigate = useNavigate(); and then navigate('/dashboard', { replace: true }); inside a component?
The replace option controls whether navigation replaces or pushes history entries.
Calling navigate('/dashboard', { replace: true }) changes the URL and replaces the current history entry. This means the previous page is removed from history, so pressing back skips it. Without replace: true, navigation pushes a new entry.