Is it a good idea to pass the route to a Component?
Hey ๐๐ฝ ,
I want to have access to the
route.fullPath or the route.id and the numerous hooks at a child component.
Here's my code:
Can you tell me whether this is a good idea or how you solve it? I want to navigate in the child component and I need the route.id for this.
Thanks12 Replies
wise-whiteโข2y ago
There's a possibility of this creating a circular dependency with your types.
If you need the
routeId it can get gotten using the getRouteApi helper.
The route's full-path needs to be defined since that is specific to each navigation.correct-apricotOPโข2y ago
Thanks @Sean Cassiere, I'm on plain JavaScript.
OK, what do you think about the following code?
Here I keep the router logic away from the child component.
wise-whiteโข2y ago
Yea that'd work.
correct-apricotOPโข2y ago
Thanks
absent-sapphireโข2y ago
Out of Curiosity: why wouldn't you use
Route.useNavigate inside CreateComponent? (assuming your whole code snippet is from one file)wise-whiteโข2y ago
The Route object doeesn't have a typed
useNavigate hook attached to it.
It only has useLoaderData, useLoaderDeps, useMatch, useParams, useSearch, and useRouteContextdependent-tanโข2y ago
we could add this, if it helps?
absent-sapphireโข2y ago
Thanks for your response. I wrongly assumed that
useNavigate is part of the Route object. Don't know if it's need on Route but would make its API maybe more consistent?dependent-tanโข2y ago
PRs are always welcome if you want to do it
optimistic-goldโข2y ago
that sounds like a good idea
it's actually pretty easy. I'll try to PR it
optimistic-goldโข2y ago
absent-sapphireโข2y ago
That was fast! ๐ณ