Replies: 2 comments
|
In code-based routing, the The fix: reference the route object directly instead of passing a string. const params = useParams({ from: groupItemRoute.id })This way TypeScript infers the correct params type from the route definition, and you don't have to guess the internal ID format. |
|
This is intentional and the docs really should call it out — route paths aren't unique (you can have layout routes / parallel routes / pathless routes that share the same The cleaner pattern that sidesteps the whole problem is to call // in your route file
export const groupItemRoute = createRoute({
path: groupItemRoutePath,
getParentRoute: () => groupRoute,
});
// in a child component
import { groupItemRoute } from '../routes/groupItem.route';
const params = groupItemRoute.useParams(); // fully typed, no string at all
If you really do want to pass // inside a component that knows it's mounted under groupItemRoute
useParams({ from: groupItemRoute.id });
// or, if you're in code-based routing, the literal generated id:
useParams({ from: '/id/path' }); // parent id ('id') + child pathThe id-generation rule for code-based routing is: For file-based routing the route IDs are the file paths, which keeps the literal-equals-path intuition. Code-based gives you more flexibility but yes, you pay for it with the id-vs-path divergence. Why I'd push for
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm using code-based routing with nested routes:
Calling useParams in a component using the path will throw an error:
useParams internally uses the route's id, not its path. When using nested routes, the id is generated based on the parent route's id and the child route's path. Because of this, it becomes impossible to reuse route path constants—you have to either import the route and use its id or search through router.routesByPath. Is this the expected behavior? It would be good if this were mentioned in the documentation
All reactions