How do you implement protected/private routes?
Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
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 eachelementindividually. - Use
replaceon 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
RequireAuthto accept anallowedRolesprop 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
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.