DEV Community

Discussion on: πŸ” Private Route in React Router v6

Collapse
 
stevereid profile image
Steve Reid

I might of missed something, but is there anything wrong with doing something like this?

<Route
  path="/"
  element={user ? <Home /> : <Navigate to="/login" />}
/>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
iamandrewluca profile image
Andrei Luca

πŸ€” looking at this it should work, I don't know if react-router can detect the full tree of routes using this way πŸ€”

Collapse
 
cebkun profile image
James Russel Ceballos

I think you should navigate to "/dashboard" (or wherever you want him to go after logging in) instead of "/login".