T
TanStack11mo ago
genetic-orange

notFoundComponent bubbles up regardless, why?

I've tried specifying defaultNotFoundComponent in the router, and also instead defining notFoundComponent in _root.tsx. When I throw notFound()inside a sub-route's beforeLoad, it bubbles up, instead of rendering that sub-route's notFoundComponent, why?
6 Replies
metropolitan-bronze
metropolitan-bronze11mo ago
Matching does not happen when beforeLoad is ran, so it does not know the route you are on, you can
return {
notFound: true
}
return {
notFound: true
}
in beforeLoad and then throw in loader
loader: ({ context }) => {
if (context.notFound) throw notFound({ ... })
}
loader: ({ context }) => {
if (context.notFound) throw notFound({ ... })
}
genetic-orange
genetic-orangeOP11mo ago
Ahh, got it That makes sense, I went down debugging the internals, and but it hard to see that this was the behavior, as a I always saw it recursively matching parent routes
metropolitan-bronze
metropolitan-bronze11mo ago
Yeah, it's somewhat confusing, this could be added to the docs of beforeLoad or/and notFound
genetic-orange
genetic-orangeOP11mo ago
I agree, because I saw it in those docs and thought it had to do with an example that was related to loader The docs are awesome, but there is still a lot of guess work and try/error one has to go thru, just because getting the docs perfect is so damn hard
metropolitan-bronze
metropolitan-bronze11mo ago
You can open PRs yourself to correct/add to the docs when you find something that needs documenting tbh, I am annoyed that to get to the docs for the Router type, I have to go to createRouter and then click on it's return type, as it is not in the list on the API reference page nvm, I am dumb, it is in the "Types" section
genetic-orange
genetic-orangeOP11mo ago
I will open the PR

Did you find this page helpful?