Skip to content

Instantly share code, notes, and snippets.

@lamualfa
Created August 1, 2023 17:07
Show Gist options
  • Save lamualfa/f0f4a28c4f4f8f4cbfa1e6361b3ba8a3 to your computer and use it in GitHub Desktop.
Save lamualfa/f0f4a28c4f4f8f4cbfa1e6361b3ba8a3 to your computer and use it in GitHub Desktop.
Hook to check is path active or no in React Router v6
import { LinkProps, useMatch, useResolvedPath } from 'react-router-dom'
export function useIsActivePath(path: LinkProps['to'], strict = true) {
const resolvedPath = useResolvedPath(path)
const match = useMatch({ path: resolvedPath.pathname, end: strict })
return match !== null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment