Next.js · Junior · Question 33 of 75
How do you redirect the user to /login from a server component?
- ✓Correct answer: Call
redirect('/login')fromnext/navigation - 2Call
router.push('/login')fromuseRouter - 3Return
<Navigate to="/login" />from the component
Explanation
redirect() works in server components, Server Functions and Route Handlers. It throws a special exception, so code after it doesn't run, and you must not wrap it in try/catch. useRouter is available only in client components, and <Navigate> is from React Router.