Redirect from one page to another in Next.js

6 months ago23k times

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");
  
});


Other answers

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>


Add Answer

Add a codeAdd Code
Remove adsremove

Latest codes

view all

Latest Snippets

view all