0
0
Reactframework~20 mins

Defining routes in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Router Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this React Router setup?
Consider this React component using React Router. What will be displayed when the URL path is /about?
React
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path='/' element={<h1>Home</h1>} />
        <Route path='/about' element={<h1>About Us</h1>} />
        <Route path='/contact' element={<h1>Contact</h1>} />
      </Routes>
    </BrowserRouter>
  );
}
A<h1>About Us</h1>
B<h1>Home</h1>
C<h1>Contact</h1>
DNothing is rendered
Attempts:
2 left
💡 Hint
Look at the path that matches the URL /about.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a nested route in React Router?
You want to define a nested route where /dashboard/settings renders a Settings component inside Dashboard. Which code snippet is correct?
A
&lt;Route path='/dashboard' element={&lt;Dashboard /&gt;}&gt;
  &lt;Route path='settings' element={&lt;Settings /&gt;} /&gt;
&lt;/Route&gt;
B
&lt;Route path='/dashboard' element={&lt;Dashboard /&gt;} /&gt;
&lt;Route path='/dashboard/settings' element={&lt;Settings /&gt;} /&gt;
C
&lt;Route path='/dashboard/settings' element={&lt;Settings /&gt;}&gt;
  &lt;Route path='/dashboard' element={&lt;Dashboard /&gt;} /&gt;
&lt;/Route&gt;
D<Route path='/dashboard/settings' element={<Dashboard><Settings /></Dashboard>} />
Attempts:
2 left
💡 Hint
Nested routes are defined by placing child Route inside a parent Route.
🔧 Debug
advanced
2:00remaining
Why does this route not render the expected component?
Given this code, navigating to /profile shows a blank page. What is the cause?
React
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path='/profile' element={<Profile />} />
      </Routes>
    </BrowserRouter>
  );
}
AThe <code>Routes</code> component must be outside <code>BrowserRouter</code>.
BThe <code>Profile</code> component is not imported.
CThe <code>BrowserRouter</code> is missing a closing tag.
DThe <code>Route</code> uses <code>component</code> prop instead of <code>element</code>.
Attempts:
2 left
💡 Hint
Check the prop name used to specify the component to render in Route.
state_output
advanced
2:00remaining
What is the output when navigating between routes with state?
Consider this React Router code. What will be logged when navigating from /home to /details with state { fromHome: true }?
React
import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom';

function Details() {
  const location = useLocation();
  console.log(location.state);
  return <h1>Details Page</h1>;
}

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path='/home' element={<button onClick={() => window.history.pushState({ fromHome: true }, '', '/details')}>Go to Details</button>} />
        <Route path='/details' element={<Details />} />
      </Routes>
    </BrowserRouter>
  );
}
A{ fromHome: true }
BThrows an error
Cnull
Dundefined
Attempts:
2 left
💡 Hint
React Router's useLocation reads state passed via Link or navigate, not window.history.pushState.
🧠 Conceptual
expert
2:00remaining
Which statement about React Router route matching is true?
React Router v6 matches routes based on the URL path. Which of these statements is correct about how it matches routes?
ARoutes are matched in order and the first match is rendered exclusively.
BRoutes are matched by best match, considering path segments and wildcards, ignoring order.
CRoutes are matched by longest path first, then by order of definition.
DRoutes are matched only if the path exactly equals the URL.
Attempts:
2 left
💡 Hint
Think about how React Router v6 chooses which route to render when multiple routes could match.