next js link
To create a link in Next.js, you can use the Link
component provided by the Next.js framework. Here are the steps to create a link in Next.js:
- Import the
Link
component from thenext/link
module:
import Link from 'next/link';
- Use the
Link
component in your JSX code to create a link:
<Link href="/path/to/destination">
<a>Link Text</a>
</Link>
Replace /path/to/destination
with the actual path or route you want the link to point to. The Link
component wraps around the <a>
tag, which serves as the clickable link text.
- Optionally, you can add additional props to the
Link
component for advanced functionality. For example, you can use theas
prop to define a custom URL for server-side rendering:
<Link href="/path/to/destination" as="/custom-url">
<a>Link Text</a>
</Link>
- You can also use the
Link
component for dynamic routing by including dynamic route parameters in the path:
<Link href="/users/[id]" as="/users/123">
<a>User Profile</a>
</Link>
In this example, the [id]
part in the path indicates a dynamic route parameter. The as
prop specifies the actual URL for the link.
By following these steps, you can create links in Next.js using the Link
component. This component is optimized for client-side navigation and provides a seamless user experience when navigating between pages in your Next.js application.