JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

Routing

react
mid
routing

How does React Router work and what are its key features?

react-router
routing
navigation
spa
useNavigate
useParams
Quick Answer

React Router provides client-side routing for React applications, enabling navigation without page reloads. It uses components like BrowserRouter, Routes, Route, and Link, with hooks like useNavigate and useParams for programmatic navigation and accessing route parameters.

Detailed Explanation

What is React Router:

  • Client-side routing library for React
  • URL changes update UI without server requests
  • Maintains browser history for back/forward navigation

Key Concepts (v7):

  1. Router Types:

    • BrowserRouter: Uses HTML5 history API (clean URLs)
    • HashRouter: Uses URL hash (for static file hosting)
  2. Core Components:

    • Routes: Wrapper for route definitions
    • Route: Maps URL path to component
    • Link/NavLink: Navigation without page reload
    • Outlet: Renders child routes in nested layouts
  3. Key Hooks:

    • useNavigate(): Programmatic navigation
    • useParams(): Access URL parameters
    • useLocation(): Current location object
    • useSearchParams(): Query string parameters
  4. Advanced Features (v6.4+/v7):

    • Loaders: Fetch data before rendering
    • Actions: Handle form submissions
    • Code splitting with lazy routes

React Router v7 Framework Mode:

  • React Router v7 also offers a framework mode with file-based routing, SSR, and data loading (evolved from Remix)

Code Examples

Basic routing setupJSX
import { BrowserRouter, Routes, Route, Link, NavLink } from 'react-router';

function App() {
  return (
    <BrowserRouter>
      {/* Navigation */}
      <nav>
        <Link to="/">Home</Link>
        {/* NavLink adds 'active' class when route matches */}
        <NavLink 
          to="/about"
          className={({ isActive }) => isActive ? 'active' : ''}
        >
          About
        </NavLink>
        <Link to="/users">Users</Link>
      </nav>

      {/* Route definitions */}
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="/users" element={<Users />} />
        <Route path="*" element={<NotFound />} />
      </Routes>
    </BrowserRouter>
  );
}

Real-World Applications

Use Cases

SPA Navigation

Client-side routing for single-page applications with browser history integration

Protected Routes

Route guards that redirect unauthenticated users to login pages

Code Splitting by Route

Lazy loading route components with React.lazy and Suspense for performance

Mini Projects

Admin Dashboard Router

intermediate

Build a dashboard with nested routes, layouts, breadcrumbs, and role-based route protection

Route Transition Animations

intermediate

Implement page transition animations using React Router with Framer Motion

Industry Examples

Remix/React Router v7

Framework mode combining routing with data loading and mutations

Shopify

Uses React Router for their merchant admin dashboard navigation

Resources

React Router Documentation

docs

React Router Tutorial

docs

Related Questions

What is React Context and when should you use it?

mid
context

How does file-based routing work in Next.js?

junior
routing
Previous
What is the difference between controlled and uncontrolled components in React forms?
Next
How do you ensure accessibility (a11y) in React applications?
PrevNext