Navigation links help users move between different parts of a website or app easily.
Navigation links in React
import { Link } from 'react-router-dom'; <Link to="/about">About Us</Link>
Use Link from react-router-dom to create navigation links without full page reloads.
The to attribute sets the path to navigate to.
import { Link } from 'react-router-dom'; <Link to="/home">Home</Link>
<Link to="/profile" aria-label="Go to your profile">Profile</Link>
<Link to="/settings" className="nav-link active">Settings</Link>
This React app uses react-router-dom to create navigation links. Clicking 'Home' or 'About' changes the displayed page without reloading the browser. The navigation is keyboard accessible and uses semantic HTML.
import React from 'react'; import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; function Home() { return <h2>Home Page</h2>; } function About() { return <h2>About Us</h2>; } function App() { return ( <Router> <nav aria-label="Main navigation"> <ul style={{ display: 'flex', gap: '1rem', listStyle: 'none' }}> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> </ul> </nav> <main> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes> </main> </Router> ); } export default App;
Always use semantic HTML elements like <nav> and <ul> for navigation lists.
Use ARIA labels to improve accessibility for screen readers.
React Router's Link prevents full page reloads, making navigation faster and smoother.
Navigation links let users move between pages or sections easily.
Use Link from react-router-dom for smooth client-side navigation.
Make navigation accessible with semantic HTML and ARIA labels.
Practice
Link from react-router-dom in a React app?Solution
Step 1: Understand the role of
LinkLinkis used to navigate between pages in a React app without refreshing the whole page.Step 2: Compare with other options
Options A, C, and D describe unrelated tasks like styling, data fetching, and state management, whichLinkdoes not handle.Final Answer:
To create navigation links that change pages without reloading the browser -> Option AQuick Check:
Navigation without reload = B [OK]
- Confusing <Link> with HTML <a> tag behavior
- Thinking <Link> manages data or state
- Assuming <Link> reloads the page
react-router-dom?Solution
Step 1: Identify correct attribute for
TheLinkLinkcomponent uses thetoattribute to specify the path.Step 2: Differentiate from HTML anchor tag
HTMLatags usehref, butLinkrequiresto. About uses correct syntax.Final Answer:
<Link to='/about'>About</Link> -> Option CQuick Check:
Linkusestoattribute [OK]
- Using href instead of to with <Link>
- Using to attribute on <a> tag
- Mixing <Link> and <a> syntax
import { Link } from 'react-router-dom';
function Nav() {
return (
<nav>
<Link to='/'>Home</Link>
<Link to='/profile'>Profile</Link>
</nav>
);
}Solution
Step 1: Analyze the JSX structure
The component returns a<nav>element containing two<Link>components with paths '/' and '/profile'.Step 2: Understand
LinkbehaviorLinkrenders as clickable links that navigate client-side without reloading the page.Final Answer:
Two clickable links labeled 'Home' and 'Profile' that navigate without page reload -> Option BQuick Check:
Links inside nav render clickable navigation [OK]
- Thinking <Link> renders plain text
- Assuming <Link> causes page reload
- Believing <Link> cannot be inside <nav>
import { Link } from 'react-router-dom';
function Menu() {
return (
<nav>
<Link href='/dashboard'>Dashboard</Link>
</nav>
);
}Solution
Step 1: Check attribute usage on
TheLinkLinkcomponent requires thetoattribute, nothref.Step 2: Verify other code parts
The<nav>tag is properly closed, andLinkcan be insidenav. React import is not mandatory in React 17+ for JSX.Final Answer:
Using 'href' instead of 'to' on <Link> causes navigation failure -> Option DQuick Check:
Linkneeds 'to', not 'href' [OK]
- Using href attribute on <Link>
- Thinking <Link> can't be inside <nav>
- Assuming React import is always required
react-router-dom?Solution
Step 1: Choose semantic HTML for navigation
The<nav>element is the correct semantic container for navigation links, improving accessibility.Step 2: Use
Linkwith descriptive text and ARIA labelsLinkcomponents fromreact-router-domprovide client-side navigation. Addingaria-labelonnavhelps screen readers understand the menu purpose.Step 3: Avoid incorrect elements
Usingdivor buttons for navigation is less semantic and harms accessibility. Usingatags withhrefreloads pages, not client-side navigation.Final Answer:
Use <nav> with <Link> components, add aria-label on nav, and ensure links have descriptive text -> Option AQuick Check:
Semantic nav + Link + ARIA = A [OK]
- Using div instead of nav for navigation
- Skipping aria-label for screen readers
- Using buttons instead of links for navigation
Ready to prove it?
Pass the test and this pattern moves unknown
