-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Describe the bug
Immediately after creating a new route, the Route.id is undefined.
const myRoute = createRoute(opts)
myRoute.id
// ^ undefinedYour Example Website or App
https://stackblitz.com/edit/tanstack-router-n22e5x?file=src%2Fmain.tsx
Steps to Reproduce the Bug or Issue
- Go to the url provided above.
- Open the console.
- Observe the logs that show what the aboutRoute id is. (see image below)
Expected behavior
I expect that when I have created a route, I can immediately use the route's id property.
In the context of my repro, I expect that both logs in the console will say that the route id is /about.
Screenshots or Videos
(Don't come at be for the bad function names! 😜 I refactored and didn't rename, ok?!)
Platform
- OS: macOS
- Browser: Chrome & Safari
- Version:
- Chrome Version 123.0.6312.107 (Official Build) (arm64)
- Safari Version 17.4.1 (19618.1.15.11.14)
Additional context
This is a bit of a tangent, so here we go...
For some of my components, I need to fake out TS to tell the router "you are definitely at one of these routes", so I do stuff like this:
const route = useMatch({ strict: false } as unknown as {
from: typeof someRoute.id | typeof anotherRoute.id
})Works fine, since that is just type information. But if I want to be sure I am safe at runtime, I can do this
const someRoute = createRoute({/* ... route def ...*/})
const anotherRoute = createRoute({/* ... route def ...*/})
const myRouteIds = [someRoute.id, anotherRoute.id] as const
type MyRouteId = typeof myRouteIds[number]
export function useMyRouteId() {
const routeId = useMatch({ strict: false, select: x => x.routeId })
if (!myRouteIds.has(routeId as MyRouteId)) {
throw new Error("You are not on one of my routes!!11!!")
}
return routeId as MyRouteId
}(I don't do it quite like that, but this was simpler for an example.)
Except... I can't. Because myRouteIds captures a bunch of undefined values, since they are not available immediately.
Of course, you can work around this, but it sure was surprising to discover.
