I'm using Next.js (v14/app router) to create a statically exported website. However, I'm facing an issue with implementing a tab bar in one of my layouts for easy page navigation.
Here is a simple example:
<div className="tabs">
<Link href={`/post/${p.slug}`}>All wins</Link>
<Link href={`/post/${p.slug}/top`}>Top wins</Link>
<Link href={`/post/${p.slug}/stats`}>Statistics</Link>
</div>
The problem arises when setting the active
classname based on the current route. Is there a way to access the current route of the page to dynamically apply the active
class like
className={currentRoute === '/post/${p.slug}/stats' ? 'active' : ''}
?
I am aware of the usePathname
method, but using it on the client-side seems inefficient. Is there a way to achieve this during page rendering?