Bring back isActive prop in NavLink component #9840
Replies: 10 comments
-
Thanks for bringing this to our attention, @smashercosmo. We should add this in v6, @chaance. |
Beta Was this translation helpful? Give feedback.
-
Actually, I think I may have spoken too soon. Just thinking about this some more... The reason we needed a For example, let's say you're building a product filter UI where you keep the value of the filter in the query string. And let's say you're selling shoes and you want to filter by brand. Your URLs might look something like Our However, v6 does give you the tools to build your own import { useSearchParams, Link } from "react-router-dom";
function BrandLink({ brand, children }) {
let [searchParams] = useSearchParams();
// Use your own logic here to tell if the link is active or not...
let isActive = searchParams.get('brand') === brand;
return (
<Link
className={isActive ? 'active' : ''}
to={`/shoes?brand=${brand}`}
>
{children}
</Link>
);
}
// Then, use it like:
<BrandLink brand="nike">Nike</BrandLink>
<BrandLink brand="puma">Puma</BrandLink> Does that make sense? In general we are trying to steer people more towards using the tools we give them to solve their own problems instead of trying to create abstractions for solving every use case. For this specific case, I think you should already have the tools you need to get it done. |
Beta Was this translation helpful? Give feedback.
-
I see your point. But how about just passing 'match' object to style and className functions along with isActive flag? |
Beta Was this translation helpful? Give feedback.
-
I think the docs may require updating:
|
Beta Was this translation helpful? Give feedback.
-
From what I can see in <NavLink to="about" className={({ isActive }) => isActive ? : 'active' : 'inactive' }>
About
</NavLink> Not 100% sure if it covers your usecase, but seems like it? |
Beta Was this translation helpful? Give feedback.
-
It would be really great if we could abstract the |
Beta Was this translation helpful? Give feedback.
-
How would I access the IsActive state inside a child component/element of the navlink? IOW: Say I want to change the classname of an icon inside of my NavLink based on its active/inactive state. |
Beta Was this translation helpful? Give feedback.
-
I think you should stop making API changes on small versions. This is a pain to keep track of, and looks like it is for those updating docs as well. |
Beta Was this translation helpful? Give feedback.
-
This means that this code snippet doesn't work (gathered from the same page)
Passing it on the children function doesn't work either
|
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion so it can go through our new Open Development process. Thanks! |
Beta Was this translation helpful? Give feedback.
-
What is the new or updated feature that you are suggesting?
NavLink's isActive prop has been removed in v6 either intentionally or accidentally. I suggest to bring it back.
Why should this feature be included?
isActive prop helps with use cases like when you need to highlight a link or a tab depending on url's query parameters
Beta Was this translation helpful? Give feedback.
All reactions