How do you handle active route states?
What are the best practice to check which route is active?
6 Replies
like-gold•3mo ago
depends on what for you need that information
give more context please
conventional-tanOP•3mo ago
I have a sidebar and I want to add some highlighting on the sidebar items.
For example, home, posts, profile
If I navigate to profile/edit I want my profile item to be highlighted
I can do
router.state.location.pathname.startsWith("/profile") and it seem to work
but how do I check dynamic routes like /profile/$username/edit?
Is there a way to do so, or do I have to parse the username in the string before checking?
like: router.state.location.pathname.startsWith(/profile/${usernameVar}/edit)like-gold•3mo ago
router.state.matches is typesafe
conventional-tanOP•3mo ago
is this similar as useMatches?
like-gold•3mo ago
uses same data yes
conventional-tanOP•3mo ago
I see, honestly so many ways to check for active routes, useMatch, useMatches, useRouter 🥲🥲🥲