Bird
Raised Fist0
Reactframework~20 mins

Navigation links in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this React navigation component?
Consider this React component that renders navigation links. What will be displayed in the browser?
React
import React from 'react';
import { Link } from 'react-router-dom';

export default function Nav() {
  return (
    <nav aria-label="Main navigation">
      <ul>
        <li><Link to="/home">Home</Link></li>
        <li><Link to="/about">About</Link></li>
        <li><Link to="/contact">Contact</Link></li>
      </ul>
    </nav>
  );
}
AA navigation bar with three clickable links labeled Home, About, and Contact.
BA navigation bar with three links but only Home and About are clickable; Contact is plain text.
CAn error because Link components require an href attribute instead of to.
DNothing renders because the component is missing a return statement.
Attempts:
2 left
💡 Hint
Remember that React Router's Link uses 'to' for navigation paths.
❓ state_output
intermediate
2:00remaining
What is the active link after clicking?
This React component highlights the active navigation link. After clicking the About link, what is the value of the active state?
React
import React, { useState } from 'react';

export default function Nav() {
  const [active, setActive] = useState('Home');

  return (
    <nav aria-label="Main navigation">
      <ul>
        <li>
          <button onClick={() => setActive('Home')} aria-current={active === 'Home' ? 'page' : undefined}>Home</button>
        </li>
        <li>
          <button onClick={() => setActive('About')} aria-current={active === 'About' ? 'page' : undefined}>About</button>
        </li>
        <li>
          <button onClick={() => setActive('Contact')} aria-current={active === 'Contact' ? 'page' : undefined}>Contact</button>
        </li>
      </ul>
    </nav>
  );
}
A"Contact"
B"Home"
C"About"
Dundefined
Attempts:
2 left
💡 Hint
Clicking the About button triggers setActive with 'About'.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this React navigation code?
Identify which code snippet will cause a syntax error when used in a React component rendering navigation links.
React
function Nav() {
  return (
    <nav>
      <ul>
        <li><a href="/home">Home</a></li>
        <li><a href="/about">About</a></li>
        <li><a href="/contact">Contact</a></li>
      </ul>
    </nav>
  );
}
Areturn <nav><ul><li><a href="/home">Home</a></li><li><a href="/about">About</a></li><li><a href="/contact">Contact</a></li></ul></nav>;
Breturn (<nav><ul><li><a href="/home">Home</a><li><a href="/about">About</a></li><li><a href="/contact">Contact</a></li></ul></nav>);
C;)>van/<>lu/<>il/<>a/<tcatnoC>"tcatnoc/"=ferh a<>il<>il/<>a/<tuobA>"tuoba/"=ferh a<>il<>il/<>a/<emoH>"emoh/"=ferh a<>il<>lu<>van<( nruter
Dreturn (<nav><ul><li><a href="/home">Home</a></li><li><a href="/about">About</a></li><li><a href="/contact">Contact</a></li></ul></nav>);
Attempts:
2 left
💡 Hint
Check if all tags are properly closed.
B. About
C. About

Solution

  1. Step 1: Identify correct attribute for Link

    The Link component uses the to attribute to specify the path.
  2. Step 2: Differentiate from HTML anchor tag

    HTML a tags use href, but Link requires to. About uses correct syntax.
  3. Final Answer:

    <Link to='/about'>About</Link> -> Option C
  4. Quick Check:

    Link uses to attribute [OK]
Hint: Use 'to' with <Link>, 'href' with <a> [OK]
Common Mistakes:
  • Using href instead of to with <Link>
  • Using to attribute on <a> tag
  • Mixing <Link> and <a> syntax
3. Given this React code snippet, what will be rendered on the page?
import { Link } from 'react-router-dom';

function Nav() {
  return (
    <nav>
      <Link to='/'>Home</Link>
      <Link to='/profile'>Profile</Link>
    </nav>
  );
}
medium
A. An error because <Link> cannot be used inside <nav>
B. Two clickable links labeled 'Home' and 'Profile' that navigate without page reload
C. Buttons that submit a form
D. Plain text 'Home' and 'Profile' with no navigation

Solution

  1. Step 1: Analyze the JSX structure

    The component returns a <nav> element containing two <Link> components with paths '/' and '/profile'.
  2. Step 2: Understand Link behavior

    Link renders as clickable links that navigate client-side without reloading the page.
  3. Final Answer:

    Two clickable links labeled 'Home' and 'Profile' that navigate without page reload -> Option B
  4. Quick Check:

    Links inside nav render clickable navigation [OK]
Hint: Links inside nav create clickable navigation [OK]
Common Mistakes:
  • Thinking <Link> renders plain text
  • Assuming <Link> causes page reload
  • Believing <Link> cannot be inside <nav>
4. What is wrong with this React navigation code?
import { Link } from 'react-router-dom';

function Menu() {
  return (
    <nav>
      <Link href='/dashboard'>Dashboard</Link>
    </nav>
  );
}
medium
A. No import statement for React
B. Missing closing tag for <nav>
C. Link component cannot be used inside <nav>
D. Using 'href' instead of 'to' on <Link> causes navigation failure

Solution

  1. Step 1: Check attribute usage on Link

    The Link component requires the to attribute, not href.
  2. Step 2: Verify other code parts

    The <nav> tag is properly closed, and Link can be inside nav. React import is not mandatory in React 17+ for JSX.
  3. Final Answer:

    Using 'href' instead of 'to' on <Link> causes navigation failure -> Option D
  4. Quick Check:

    Link needs 'to', not 'href' [OK]
Hint: Use 'to' with <Link>, not 'href' [OK]
Common Mistakes:
  • Using href attribute on <Link>
  • Thinking <Link> can't be inside <nav>
  • Assuming React import is always required
5. You want to create a navigation menu with links that have accessible names and keyboard focus. Which approach is best in React using react-router-dom?
hard
A. Use <nav> with <Link> components, add aria-label on nav, and ensure links have descriptive text
B. Use <div> with <Link> components and no ARIA labels
C. Use <nav> with <a> tags and href attributes for navigation
D. Use buttons with onClick handlers instead of links for navigation

Solution

  1. Step 1: Choose semantic HTML for navigation

    The <nav> element is the correct semantic container for navigation links, improving accessibility.
  2. Step 2: Use Link with descriptive text and ARIA labels

    Link components from react-router-dom provide client-side navigation. Adding aria-label on nav helps screen readers understand the menu purpose.
  3. Step 3: Avoid incorrect elements

    Using div or buttons for navigation is less semantic and harms accessibility. Using a tags with href reloads pages, not client-side navigation.
  4. Final Answer:

    Use <nav> with <Link> components, add aria-label on nav, and ensure links have descriptive text -> Option A
  5. Quick Check:

    Semantic nav + Link + ARIA = A [OK]
Hint: Use semantic nav + Link + aria-label for accessible navigation [OK]
Common Mistakes:
  • Using div instead of nav for navigation
  • Skipping aria-label for screen readers
  • Using buttons instead of links for navigation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard