I am developing a Nextjs app and would want to direct user to another link using router.push but I do not know how to open the link in new tab. please advice. thank you.
My code is like this right now:
import { useRouter } from "next/navigation";
const router = useRouter();
<Button
onClick={() => {
router.push(`${mylink}`);
}}
>
Order
</Button>
2
Answers
I figured out the issue I had to use the code below and get it working for external links.
Best for internal links:
But for external links it is better to use window.open() like the code below.
What
router.push
does is a redirection to the route you defined. Here what you want is to open a new tab on a different link.You can use NextJS Link and set the target as
_blank
If you want a button you can do it like this