question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function based className for active route for ''next/link'

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

I see a lot of people using a separate component, just for adding an active class when a route is active. My suggestion is the add functional support for className in the link component.

To give an example:

import Link from "./link";

// Example using classnames package.
import cx from "classnames";

<Link className={active => cx({ "text-gray-200": active })} href="/">
  <a>Home</a>
</Link>;

// Example using pure javascript.
<Link className={active => active && "text-gray-200"} href="/">
  <a>Home</a>
</Link>;

Describe the solution you’d like

My solution is to allow className to be both a string and a function; When then component identifies the className as a function, call the method with the first argument is a boolean if the route is active then true else false.

Describe alternatives you’ve considered

An alternative solution is to implement the component yourself, as I have done so far.

Another way would be creating a component with an activeClassName attribute but this prevents support for class IntelliSense in editors as VSCode.

The reason I suggest this is because as the latest releases show, next.js is all about zero-config and not too much complexity. So why not implement such used feature into the existing Link component.

Additional context

My current solution to this request is using a custom component.

Made a gist to my component and a small example here: https://gist.github.com/ch99q/b417990540d2ef91870e1af99ac6a516

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
webdebcommented, Nov 16, 2020

@agos

I’ve created a custom ActiveLink component:

import React from "react"
import { useRouter } from "next/router"

function ActiveLink({ children, href, as, activeClass }) {
  const router = useRouter()
  const active = router.pathname === href

  const onClick = e => {
    e.preventDefault()
    router.push(href, as)
  }

  if (typeof children === "function") {
    return children({ active, onClick })
  }

  const className = active
    ? children.props.className
      ? `${children.props.className} ${activeClass}`
      : activeClass
    : children.props.className

  return React.cloneElement(children, { className, onClick })
}

export default ActiveLink

haven’t tested it properly with 1.5.3, it is still using as but it works for my purposes right now.

0reactions
balazsorban44commented, Jan 29, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Target Active Link when the route is active in Next.js
A simple solution based on the useRouter hook: import Link from "next/link"; import { useRouter } from "next/router"; export const MyNav ...
Read more >
How To Add Styling To An Active Link In NextJS
I am using the useRouter hook inside my "Header function component" and created a ternary operator to check if the <Link> to style...
Read more >
next/link
Enable client-side transitions between routes with the built-in Link component.
Read more >
How to Highlight Currently Active Link in Next.js - Sling Academy
In order to determine whether a link is active or not, we will get information about the current route and then compare it...
Read more >
Next.js - NavLink Component Example with Active CSS Class
This is a quick post to show how to create a custom NavLink component in Next. js that extends the built-in Link component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found