How would you implement a basic protected route in React?
Whether you can gate React Router 7 routes declaratively.
Hold auth in useState, pass user to protected components, and return Navigate to login when absent.
What's really being asked
The interviewer wants to see if you understand how to gate routes declaratively in React Router 7 without relying on brittle imperative hacks. At the senior level they also care whether you distinguish authentication from authorization and whether you think about state propagation across the router. They are listening for familiarity with modern Navigate patterns rather than legacy redirect APIs.
The full answer
First, auth state lives in a parent component using useState and a user object is passed as a prop into protected route components. Second, inside the protected component an early return renders React Router's Navigate component with the replace prop pointing to a public route such as login when the user is null or undefined. Third, a strong candidate notes that authorization can be more granular than authentication, for example requiring specific roles or permissions like analyze or admin before granting access. Fourth, mention that public routes such as Landing should remain unguarded so anonymous visitors can still access them. Fifth, acknowledge that the replace prop prevents the protected route from accumulating in browser history so the back button behaves correctly.
The mistakes people make
Using window.location.href or history.push directly instead of React Router's Navigate component breaks the declarative model and loses router state. Returning null or a loading spinner without redirecting traps the user on a blank screen. Forgetting the replace prop on Navigate pollutes the browser history so the back button cycles through protected routes instead of skipping them. Hardcoding auth checks separately in every page rather than centralizing the pattern also signals weak architectural thinking. Suggesting local storage checks inside the component without lifting state up can create sync bugs between tabs.
What usually comes next
How would you persist the login state across page refreshes? How would you redirect the user back to their originally requested page after they log in? How would you handle role-based access control for nested admin routes? What happens to protected data fetching if an unauthenticated user lands directly on a guarded URL? Would you extract the guard logic into a reusable PrivateRoute wrapper or keep it inline?
A concrete example
Imagine a dashboard route at slash dashboard. The App component holds const user and setUser from useState and passes user into the Dashboard element. The Dashboard component receives user as a prop. If user is falsy, the component returns Navigate to equals slash login replace equals true. Otherwise it renders the dashboard UI. This mirrors the pattern shown in React Router 7 tutorials where Landing stays public but Home and Dashboard require an authenticated user, while Analytics might require an additional permission.
Interview question
In React Router 7, how should a protected component declaratively handle an unauthenticated user?
- a.Render Navigate to login with replace set to trueCorrect
- b.Use useEffect to call history.push and redirect to login
- c.Return null to block rendering until the user authenticates
- d.Read localStorage and assign window.location.href to the login URL
Why? this is the answer
Rendering Navigate with replace is the declarative React Router 7 pattern that redirects unauthenticated users while keeping history clean. Calling history.push imperatively breaks the declarative model and loses router state.
Just read this? Test yourself on what you have been reading.
Read the original → robinwieruch.de
- #react
- #react-router
- #authentication
- #authorization
- #routing
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.
See open roles