-
-
Couldn't load subscription status.
- Fork 10.7k
feat(usePathIsActive): expose isActive hook
#8723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Hi @nicksrandall, Welcome, and thank you for contributing to React Router! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected]. Thanks! - The Remix team |
|
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
|
@nicksrandall This is similar to #8567 - which was closed since the docs suggest a way to achieve this using existing hooks. Does that satisfy your use case? |
|
Yes, that would work for me. IMO, NavLink should be refactored to use useMatch so things are consistent. |
|
Can't wait for this to get merged. Currently I'm using it to find out an active step in our multi-step form. const steps: { name: string; to: To }[] = [
{ name: "Define the group structure", to: "." },
{ name: "Define the chart of accounts", to: "chart-of-accounts" },
{ name: "Define summary rows & KPI", to: "misc" },
{ name: "Set exchange strategies", to: "exchange" },
{ name: "Validate the data", to: "validation" },
];
const useActiveStep = () => {
const matches: boolean[] = [];
for (const step of steps) {
// eslint-disable-next-line react-hooks/rules-of-hooks
matches.push(usePathIsActive({ to: step.to, end: true }));
}
return matches.findIndex((match) => {
return match !== false;
});
}; |
|
I'd like to get this merged since I have a link component that uses a prop instead of classname or style to determine active or not. This hook would easily allow more flexible versions of the NavLink. #8567 doesn't really work, since NavLink can be a relative path, but |
|
👋 Hey folks - I'm going to close this out for the same reasons discussed in #8567 - we think @hornta have you tried using the @paul-sachs Would you also be able to open a new issue with a simple reproduction where that approach doesn't suffice for you? That way we can see if there are other options. |
|
@brophdawg11 turns out the reason my example wasn't working was due to a bug in userland. |
|
If you need to look for a match on something other than the current location you may be able to just use |
This PR exposes the
isActivelogic fromNavLinkas a hook so that it can be used to create customNavLinkstyle components.Example usage: