T
TanStack3y ago
sunny-green

Help to understand the need to `getParentRoute`

Why is the need to use getParentRoute when defining the new Route()?
const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<div className="p-2">
<h3>Welcome Home!</h3>
</div>
)
},
});

const routeTree = rootRoute.addChildren([indexRoute])
const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<div className="p-2">
<h3>Welcome Home!</h3>
</div>
)
},
});

const routeTree = rootRoute.addChildren([indexRoute])
Since I'm adding .addChildren could that be inferred, like indexRoute after that point knows that its parent is the rootRoute?
2 Replies
genetic-orange
genetic-orange3y ago
Afaik it's only for TypeScript and typesafety so it can infer parent route context/loader etc
sunny-green
sunny-greenOP3y ago
hum ok, that makes sense. Thank you. Since I'm using a file structure with features, it will be nice if each feature creates its own routes and only the root imports them, without the need to infer the parent, but it's ok, I can use a function creator that receives an argument that is the parent route.

Did you find this page helpful?