The following code is easy to redirect from one page to another in the next.js. To immediately redirect from one page to another useEffect
will be used.
import { useRouter } from "next/router"; const router = useRouter(); useEffect(()=>{ router.push("/another-page"); });
To redirect from one page to another within a website use Link
component of next.js
import Link from 'next/link'; <Link href='/another-url'> <a> </a> </Link>
To redirect from one page to another using button click event using arrow function
import { useRouter } from "next/router"; const router = useRouter(); const redirect = () =>{ router.push('another-page'); } <button onclick={redirect}>Click Me</button>