0
0
Reactframework~20 mins

React Router overview - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Router Mastery
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 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?

React
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>
  );
}
AThe page shows two links 'Home' and 'About' but no heading is displayed.
BThe page shows two links 'Home' and 'About' and below them the heading 'About Page'.
COnly the heading 'About Page' is shown, no links are visible.
DThe page shows two links 'Home' and 'About' and below them the heading 'Home Page'.
Attempts:
2 left
💡 Hint

Remember that the Routes component renders the matching Route element based on the URL path.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a nested route in React Router v6?

In React Router v6, how do you correctly nest a child route inside a parent route?

A
&lt;Route path='/dashboard' element={&lt;Dashboard /&gt;}&gt;
  &lt;Route path='stats' component={Stats} /&gt;
&lt;/Route&gt;
B
&lt;Route path='/dashboard' element={&lt;Dashboard /&gt;}&gt;
  &lt;Route path='/stats' element={&lt;Stats /&gt;} /&gt;
&lt;/Route&gt;
C
&lt;Route path='/dashboard' element={&lt;Dashboard /&gt;}&gt;
  &lt;Route path='stats' element={&lt;Stats /&gt;} /&gt;
&lt;/Route&gt;
D
&lt;Route path='/dashboard' element={&lt;Dashboard /&gt;} /&gt;
&lt;Route path='/dashboard/stats' element={&lt;Stats /&gt;} /&gt;
Attempts:
2 left
💡 Hint

Nested routes use relative paths without a leading slash and the element prop, not component.

state_output
advanced
2:00remaining
What is the value of the URL after clicking the Link?

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?

React
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>;
}
Ahttp://localhost/profile
Bhttp://localhost/profile/tab=info
Chttp://localhost/profile#tab=info
Dhttp://localhost/profile?tab=info
Attempts:
2 left
💡 Hint

Query parameters start with a question mark ? in URLs.

🔧 Debug
advanced
2:00remaining
Why does this React Router code cause a blank page?

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?

React
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>;
}
AThe <Route> path '/' matches only exactly '/', but React Router v6 matches partially by default, so the blank page is caused by missing a default route.
BThe <Routes> component requires a single child, but there are two <Route> elements.
CThe <Route> path '/' matches only exactly '/', but React Router v6 requires 'exact' prop to match root.
DThe <Route> for '/' does not have an 'index' attribute, so it does not render by default.
Attempts:
2 left
💡 Hint

React Router v6 matches routes partially by default. Check if the root route is rendered correctly.

🧠 Conceptual
expert
2:00remaining
Which statement best describes React Router's 'useNavigate' hook behavior?

In React Router v6, what happens when you call const navigate = useNavigate(); and then navigate('/dashboard', { replace: true }); inside a component?

AIt changes the URL to '/dashboard' and replaces the current entry in the browser history, so the back button skips the previous page.
BIt changes the URL to '/dashboard' and adds a new entry to the browser history, so the back button returns to the previous page.
CIt reloads the entire page at '/dashboard' ignoring React Router's client-side routing.
DIt queues the navigation but does not change the URL until the component unmounts.
Attempts:
2 left
💡 Hint

The replace option controls whether navigation replaces or pushes history entries.