Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 10
React Router v6 Fundamentals
Setting up React Router and understanding the foundational components: BrowserRouter, Routes, Route, Link, NavLink - and the crucial difference between Link (navigation) and NavLink (active-aware navigation).
Basic Setup & Navigation
// main.tsx - wrap app in BrowserRouter
import { BrowserRouter } from 'react-router-dom';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './app/App';
createRoot(document.getElementById('root')!).render(
<StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>
);
// App.tsx - define top-level routes
import { Routes, Route, Navigate } from 'react-router-dom';
import { RootLayout } from './layouts/RootLayout';
import { HomePage } from './pages/HomePage';
import { AboutPage } from './pages/AboutPage';
import { NotFoundPage } from './pages/NotFoundPage';
export function App() {
return (
<Routes>
<Route path="/" element={<RootLayout />}>
<Route index element={<HomePage />} />
<Route path="about" element={<AboutPage />} />
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
);
}
// Link vs NavLink
import { Link, NavLink } from 'react-router-dom';
function Navigation() {
return (
<nav>
{/* Link - basic navigation, no active state */}
<Link to="/home">Home</Link>
{/* NavLink - adds 'active' class and aria-current when route matches */}
<NavLink
to="/about"
className={({ isActive }) =>
isActive ? 'nav-link nav-link--active' : 'nav-link'
}
style={({ isActive }) => ({ fontWeight: isActive ? 700 : 400 })}
>
About
</NavLink>
</nav>
);
}Common Mistakes - Router Fundamentals
- Wrapping BrowserRouter inside a component that re-renders - always put it at the very top of the tree (main.tsx); re-creating the router resets navigation history
- Using `<a href>` instead of `<Link to>` - native anchor tags cause full page reloads, destroying React state; always use Link inside a React Router app
- Forgetting the `*` catch-all route - without it, navigating to an unknown URL renders nothing instead of a 404 page
- Absolute paths in nested routes - in v6, nested route paths are RELATIVE to the parent; `path="about"` not `path="/dashboard/about"`
Tip
Tip
Practice React Router v6 Fundamentals in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for React Router v6 Fundamentals
Practice Task
Note
Practice Task - (1) Write a working example of React Router v6 Fundamentals from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with React Router v6 Fundamentals is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Setting up React Router and understanding the foundational components: BrowserRouter, Routes, Route, Link, NavLink - and the crucial difference between Link (navigation) and NavLink (active-aware navigation).
- Wrapping BrowserRouter inside a component that re-renders - always put it at the very top of the tree (main.tsx); re-creating the router resets navigation history
- Using `<a href>` instead of `<Link to>` - native anchor tags cause full page reloads, destroying React state; always use Link inside a React Router app
- Forgetting the `*` catch-all route - without it, navigating to an unknown URL renders nothing instead of a 404 page
Finished this lesson?