Using the router to detect the active link in Next.js
When working with links in Next.js, it’s important to determine the current URL and assign a class to the active link so that it can be styled differently. This is particularly useful in the site header. Unfortunately, the default Link
component provided by next/link
doesn’t handle this automatically. However, there are two techniques that we can use to achieve this behavior.
The first technique is to add the logic to the children of the Link
component. Here’s a simple example:
1 | import React from 'react' |
This technique is recommended as it is the simplest approach.
The second technique involves creating our own Link
component. We can store it in a file called MyLink.js
inside the /components
folder and import it instead of the default next/link
. Inside the component, we compare the current pathname with the href
prop of the component. If they match, we append the text-blue-500
class to the children. You can use your own classes, but in this example, we use a Tailwind class to make the text blue. Finally, we return the children with the updated class using React.cloneElement()
.
1 | import React from 'react' |
Now, we can use this MyLink
component in other components:
1 | import MyLink from 'components/MyLink' |
Using this technique, the code in your other components becomes simpler compared to the first technique, as the logic is now contained within the MyLink
component.
tags: [“Next.js”, “Router”, “Active Link”, “CSS styling”, “Components”]