Next.js · Junior · Question 33 of 75

How do you redirect the user to /login from a server 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.

Further reading

Problem with a question

Next.js · Junior · question 33

Test yourself

Take the Next.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz