Get Current Path Template #11266
Unanswered
blakearoberts
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What is the recommended way for a component to get the path template that lead to its rendering?
The hook,
useLocation()
returns the current path, but not the template that lead to the path. For example, if I have a route,document/:id
,useLocation()
will return information such as the templated path, e.g.document/123
. I would like to get the template, not the templated path, i.e.document/:id
.It would seem Next.js supports a hook
useRouter()
, but I am not using Next.js.I could shove all my routes' paths into their handlers. But this isn't DRY and in typescript requires awkward type casting due to the handler's type,
unknown
.I could also create my own router function that maps paths to their template. But I'm not sure it makes sense for me to reimplement routing so I can use a routing library.
I feel like there is some concept I am misunderstanding; though, the fact that Next.js has the feature I'm looking for makes me feel otherwise. So I ask, what is the recommended way for a component to get the path template that lead to its rendering?
Beta Was this translation helpful? Give feedback.
All reactions