next js navigation to other page in a function
To navigate to another page in Next.js using a function, you can follow these steps:
- Import the Link component from the next/link module at the top of your file:
import Link from 'next/link';
- Inside your function, use the Link component to create a link to the desired page. The Link component takes a
href
prop which specifies the URL of the page you want to navigate to:
function MyComponent() {
return (
<div>
<Link href="/other-page">
<a>Go to Other Page</a>
</Link>
</div>
);
}
Add any necessary styling or content to the link. In this example, we have used an anchor tag (
<a>
) to create a clickable link. You can customize the link's appearance using CSS or any other styling method of your choice.Save your file and run your Next.js application. You should now see a link on your page that, when clicked, will navigate to the specified page ("/other-page" in this example).
That's it! By following these steps, you can navigate to another page in Next.js using a function. Remember to replace "/other-page" with the actual URL of the page you want to navigate to in your application.