React Router Interview Questions
With Answers & Code Examples

2 carefully curated React Router interview questions with working code examples and real interview gotchas.

2questions
1beginner
1core
0advanced

How does React Router v6 work and what are the key hooks?

Question 1Beginner

Hint: Nested Routes with Outlet, useNavigate instead of useHistory, useParams, useSearchParams

import { BrowserRouter, Routes, Route, Outlet } from 'react-router-dom';

// Route definition — v6 syntax
function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Layout />}>
          <Route index element={<Home />} />
          <Route path="users" element={<Users />} />
          <Route path="users/:id" element={<UserDetail />} />
          <Route path="*" element={<NotFound />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

// Layout component — Outlet renders matched child route
function Layout() {
  return (
    <div>
      <Nav />
      <main><Outlet /></main>
    </div>
  );
}

Key hooks:

// Navigate programmatically
const navigate = useNavigate();
navigate('/users/123');
navigate(-1); // go back

// Route params — /users/:id
const { id } = useParams();

// Query string — /users?tab=active
const [searchParams, setSearchParams] = useSearchParams();
const tab = searchParams.get('tab');

// Current location
const location = useLocation();
console.log(location.pathname, location.search, location.state);
💡 v6 removed useHistory (now useNavigate), Switch (now Routes), exact prop (now default), and Redirect (now Navigate component). Nested routes no longer need full paths — relative routing is the default.

How do you implement protected routes in React Router?

Question 2Core

Hint: Wrapper component that checks auth state and redirects — compose with Outlet for nested protection

// ─── Option 1: Wrapper component ─────────────────────────────────────────────
function ProtectedRoute({ children }) {
  const { user } = useAuth();
  const location = useLocation();

  if (!user) {
    // Redirect to login, save the page they tried to visit
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
}

// Usage
<Route path="/dashboard" element={
  <ProtectedRoute>
    <Dashboard />
  </ProtectedRoute>
} />

// ─── Option 2: Layout route (cleaner for many protected routes) ───────────────
function RequireAuth() {
  const { user } = useAuth();
  const location = useLocation();

  if (!user) {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return <Outlet />; // render the matched child
}

// Wrap all protected routes under one parent
<Routes>
  <Route path="/login" element={<Login />} />
  <Route element={<RequireAuth />}>          {/* no path — just a guard */}
    <Route path="/dashboard" element={<Dashboard />} />
    <Route path="/settings" element={<Settings />} />
    <Route path="/profile" element={<Profile />} />
  </Route>
</Routes>

// After login — redirect to original destination
function Login() {
  const navigate = useNavigate();
  const location = useLocation();
  const from = location.state?.from?.pathname || '/dashboard';

  async function handleLogin(creds) {
    await login(creds);
    navigate(from, { replace: true }); // replace so back button works
  }
}
💡 Always use replace when redirecting after login — so the user's back button goes to where they originally wanted to go, not to a login loop.

Other React Interview Topics

Ready to practice React Router?

Get AI feedback on your answers, predict code output, and fix real bugs.