React Easy technical 0 views 2 min read

How do you implement protected/private routes?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

A protected (or private) route only renders its content when the user meets some condition (usually "is authenticated"), otherwise it redirects them elsewhere (e.g., to /login). In React Router v6+, the cleanest way is a small wrapper component that renders <Outlet /> for its children or a <Navigate> redirect:

     import { Navigate, Outlet, useLocation } from "react-router-dom";

     function RequireAuth() {
       const { user } = useAuth(); // your own auth hook/context
       const location = useLocation();

       if (!user) {
         // Redirect to login, remembering where the user was headed
         return <Navigate to="/login" state={{ from: location }} replace />;
       }

       return <Outlet />; // render the matched child route
     }
     
     import { Routes, Route } from "react-router-dom";

     function App() {
       return (
         <Routes>
           <Route path="/login" element={<Login />} />

           {/* Every nested route below requires auth */}
           <Route element={<RequireAuth />}>
             <Route path="/dashboard" element={<Dashboard />} />
             <Route path="/settings" element={<Settings />} />
           </Route>
         </Routes>
       );
     }
     

After a successful login, read location.state?.from (set above) and navigate(from, { replace: true }) to send the user back to the page they originally tried to reach.

Key points:

  • Wrapping with a layout route (<Route element={<RequireAuth />}>) that renders <Outlet /> lets you protect many nested routes at once, instead of guarding each element individually.
  • Use replace on the redirect so the protected URL isn't left in history (prevents "back button" from flashing the protected page).
  • For role-based protection (e.g., admin-only), generalize RequireAuth to accept an allowedRoles prop and check both authentication and authorization.
  • Client-side route guards are a UX convenience, not a security boundary — always enforce access control on the server/API too.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?