Wondering if there’s a “Redwood” way to get the current route/URL/path using the router? Or should we be using window.location for that?
Thanks in advance!
Wondering if there’s a “Redwood” way to get the current route/URL/path using the router? Or should we be using window.location for that?
Thanks in advance!
I have not yet fully understood the Router’s code, but it looks like it’s missing that feature… we might need a useLocation
hook?
There is a LocationContext
within the Router’s internals, however it’s not exported to the outside… and in the end, what it provides is just what you can find on window.location
.
What do you think @thedavid? Wouldn’t it be nice to have a way to get the current route, path and params form a single hook?
Not sure what your use-case is, but in case it is showing active links; I’ve created a PR that does this! https://github.com/redwoodjs/redwood/pull/416/files
In any case exposing a useLocation
hook could be useful for custome stuff as @olance mentioned.
EDIT: Wait doesn’t useParams
give you the current route?
Hmm I might have looked at it too quickly but I only saw params
route params, right?
Yes, the params for the current route. But only the params!
In the above screenshot I made my layout log two things:
LocationContext
provides, which is basically the contents of window.location
useParams
provides, which is just the list of params for the current route, without any information about the route itselfAh, of course, you are right! Forgot about that part
Nice! That’s actually exactly my use case.
Thanks everyone for the feedback, you all rock!
@olance indeed!
@Robert I took a quick look at your PR, which looks great but doesn’t seem like we could leverage it for Olivier’s idea here, correct?
I’d say open up a new issue or get a PR started if you have a chance. We’re a bit buried this week but will try to loop back around to this if you gents aren’t able. All good either way. And huge thanks for helping out here!
Correct!
I suppose what Olivier would like is exposing useContext(LocationContext)
as something like useLocation
? @olance cmiiw
Yup that’s exactly what I had in mind, but do we want to make it more useful by providing more than just the pathname/Search/hash? (Open question)
That’s what useContext(LocationContext)
would provide