What is Client-Side Routing?
In a Single Page Application (SPA), the browser doesn't request a new HTML document from the server every time a user clicks a link. Instead, React Router intercepts the URL change and updates the UI components dynamically. This results in a faster, app-like experience without full page reloads.
Core Components of React Router
- BrowserRouter: The parent component that stores the current location and navigates using the browser's history API.
- Routes & Route: The configuration where you define which component should render for a specific path.
- Link & NavLink: Components used to navigate between routes (replaces the standard
<a>tag). - Navigate: A component used for programmatically redirecting users.
Basic Route Setup
Dynamic Routing & URL Parameters
Often you need to render the same component for different IDs (e.g., a User Profile). You can use 'colon' syntax to define a parameter and the useParams hook to retrieve it.
Nested Routes & Outlets
Nested routing allows you to swap out only a sub-section of your UI while keeping a parent layout (like a sidebar) consistent. Use the <Outlet /> component to tell the parent where to render the child route.