Skip to content

Conversation

@akx
Copy link

@akx akx commented Jan 7, 2022

This PR extracts the path-matching part of NavLink to a pure function isPathActive and a hook useIsPathActive.

This will make it possible to easily create more custom <NavLink> analogues:

function RouterNavLink({to, children}) {
  const active = useIsPathActive(to);
  const navigate = useNavigate();
  return (
    <SomeCustomNavLink active={active} onClick={() => navigate(to)}>{children}</SomeCustomNavLink>
  );
}

I didn't add any new tests for the extracted behavior, since it's exactly the same code used by NavLink, and so is covered by NavLink's tests.

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jan 7, 2022

Hi @akx,

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 CLA Signed label will be added to the pull request.

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

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jan 7, 2022

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@timdorr timdorr requested review from chaance and mjackson January 7, 2022 16:53
@akx akx force-pushed the expose-is-active branch from f6def26 to 1e68808 Compare February 8, 2022 11:39
@akx
Copy link
Author

akx commented Feb 8, 2022

Rebased. Could someone review this? Cheers.

@brophdawg11
Copy link
Contributor

There's an example in the docs to achieve this type of custom active link via useMatch/useResolvedPath, so I don't think we need to expose the internals of NavLink

@akx
Copy link
Author

akx commented Feb 26, 2022

@brophdawg11 Alright, I guess, but why doesn't <NavLink> use useMatch then? Especially since' useMatch's docs say this...

/**
* Returns true if the URL for the given "to" value matches the current URL.
* This is useful for components that need to know "active" state, e.g.
* <NavLink>.
*
* @see https://reactrouter.com/docs/en/v6/api#usematch
*/

@brophdawg11
Copy link
Contributor

@akx Great question - maybe it should! From a quick test it seems to work, but I'll defer to @mjackson for any historical context

@WillSmithTE
Copy link
Contributor

@brophdawg11 I wanted this functionality, and saw a few places linking here, but the docs link seems to have died. Any chance you have a new one, for future people's reference here too?

@brophdawg11
Copy link
Contributor

Oops - yeah here's the link to the example in the repo: https://github.com/remix-run/react-router/tree/main/examples/custom-link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants