Next.js Highlight Current Page Link – How to Highlight the Active Link in Next.js
Written by Kolade Chris | Aug 17, 2024 |
#Next.js
#React | 7 minute Read
Creating a user-friendly navigation system is essential for any website, and highlighting the current page link in the navbar is an integral aspect of that.
In Next.js, a popular React framework, implementing this feature can improve user experience by visually indicating the current page or section the user is visiting. This not only guides users through your site content, it also adds a touch of elegance to the website design.
There are many ways you can highlight the active navlink in Next.jsand React. The popular way of doing it with React is using the react-router package. In Next JS, we used to do it with the userRouter hook.
This article will take you through the new Next JS way of highlighting the active navlink, particularly in Next 14, and that’s using the usePathname hook.
The Navbar we are Working with
The navbar we are using for this guide has already been prepared for you with Tailwind CSS. You can expand the code to see it in full.
What’s the code above doing?
the use-client directive signifies to React that the component is a client component
useState is imported from React and Link from next link
an isOpen state variable is set and setIsOpen to modify the state
a toggleMenu function is created to handle the opening and closing of the mobile menu
an handleClick function is created to hide and show the nav items when one of them is selected on the mobile menu
This is what the navbar looks like on a desktop screen:
And this is what it looks like on a mobile screen:
How to Use the usePathname Hook to Highlight the Current Page Link
Step 1: Create a new Next JS app with the command below
Step 2: Create a Navbar.jsx component in the root and import the usePathname hook from next/navigation
Step 3: Then you must initialize the usePathname hook
Step 4: After that, you have to make the className of each navlink dynamic with the syntax below
For example, the entire services page link and text will now look like this:
And those of the about page will look like this:
You must also make the classNames of the mobile items dynamic.
For example, this is what the about page item of the mobile menu will look like:
After making all the necessary changes, the entire Navbar component should be this:
Don’t forget to expand the code so you can see the changes I made.
Step 5: You then need to import the Navbar component into your layout file and use it.
The navbar should now look like this on a desktop screen:
And it should look like this on a mobile screen:
If you’re wondering how I got an item to show for each page, I created each page in the app directory. This is what the folder structure looks like:
Wrapping Up
Highlighting the current page link in the navbar is one of the many critical features you should consider implementing if you have user experience in mind.
This feature not only enhances the user experience, it also strengthens visual design and sends to the user a good perception of your website.
To better understand things, you can grab the code for this article from the project GitHub repo.
Share this article by copying the link:
Or share the it with your friends and family on social media: