React · React Router

React Router Interview Questions
With Answers & Code Examples

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

Practice Interactively →← All Categories
2 questions1 beginner1 core0 advanced
Q1Beginner

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

💡 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 (
    
      
        }>
          } />
          } />
          } />
          } />
        
      
    
  );
}

// Layout component — Outlet renders matched child route
function Layout() {
  return (
    
); }

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.
Practice this question →
Q2Core

How do you implement protected routes in React Router?

💡 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 ;
  }

  return children;
}

// Usage

    
  
} />

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

  if (!user) {
    return ;
  }

  return ; // render the matched child
}

// Wrap all protected routes under one parent

  } />
  }>          {/* no path — just a guard */}
    } />
    } />
    } />
  


// 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.
Practice this question →

Other React Interview Topics

Rendering Strategies →Core JS →Type System →React Fundamentals →Functions →Microfrontends →Generics →Async JS →Hooks →Objects →Monorepo →Arrays →'this' Keyword →Utility Types →Error Handling →Modern JS →Bundle Optimization →Performance →DOM & Events →State Management →Classes & OOP →Caching Strategies →Component Patterns →Advanced Types →Authentication →Forms →Advanced Patterns →Frontend Security →Concurrent React →Server Components →Testing →Ecosystem →Network Optimization →Core Web Vitals →Browser APIs →

Ready to practice React Router?

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

Start Free Practice →