Complete the code to import the redirect function from Next.js.
import { [1] } from 'next/navigation';
useRouter instead of redirect.redirect from 'next/router' instead of 'next/navigation'.The redirect function is imported from next/navigation to perform client-side redirects.
Complete the code to redirect the user to the home page inside a server component.
export default function Page() {
[1]('/');
return <p>Redirecting...</p>;
}useRouter() which is a hook for client components.router.push inside a server component.Calling redirect with the target path immediately redirects the user in a server component.
Fix the error in the redirect usage inside a server action.
import { redirect } from 'next/navigation'; export async function submitForm() { // some logic [1]('/success'); }
router.push which is not available in server actions.window.location.href which is client-side only.Inside server actions, you must use the redirect function from next/navigation to perform redirects.
Fill both blanks to create a server component that redirects if a condition is met.
import { [1] } from 'next/navigation'; export default function Page({ isLoggedIn }) { if (!isLoggedIn) { [2]('/login'); } return <p>Welcome back!</p>; }
useRouter instead of redirect.router.push inside a server component.Both the import and the function call use redirect to send users to the login page if not logged in.
Fill all three blanks to create a server action that redirects after form submission.
import { [1] } from 'next/navigation'; export async function submitData(data) { // process data if (data.success) { [2]('/thank-you'); } else { [3]('/error'); } }
router.push which is client-side only.redirect multiple times with different names.The redirect function is imported once and used twice to redirect to different pages based on success.