Signal a missing resource
A route loader cannot retrieve a post, user, or other resource by identifier.
TanStack · Function
Creates a TanStack Router not-found error for missing resources or route handling. Return it or throw it from beforeLoad or loader callbacks, with options available to select handling and control throwing.
Call notFound to create a TanStack Router not-found error. Throw it from a route loader or beforeLoad callback when a resource is absent; alternatively, configure options such as routeId, data, or throw to control handling and whether the function throws immediately.
notFound(options?)| Name | Type | Description |
|---|---|---|
options | NotFoundErrorOptions | Optional configuration for the not-found error, including throw, routeId, and data. |
A NotFoundError object by default; with options.throw set to true, the function throws the object instead.
import { notFound, createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ params }) => {
const post = await getPost(params.postId)
if (!post) throw notFound()
return post
},
})
async function getPost(id: string) {
return id === '1' ? { id, title: 'Hello' } : undefined
}The loader throws a not-found error when the requested post does not exist.
A route loader cannot retrieve a post, user, or other resource by identifier.
A helper needs to throw immediately rather than return an error object.
A specific route or the root route should render the not-found UI.
notFound creates a NotFoundError that can be returned or thrown from route beforeLoad and loader callbacks. Throwing it signals TanStack Router to render the applicable notFoundComponent.
Follow-up: How can the root route handle the error? Use routeId for the root route, or configure the router's notFoundMode as root so root handling is selected.
Calling notFound() returns the error object by default. Passing { throw: true } makes the function throw the object instead, which is useful when a throwing helper should have a never-like control flow.
Follow-up: What happens when throw is false or omitted? The object is returned and must be thrown by the caller if it should trigger not-found handling.
The optional routeId identifies the route that should attempt to handle the error. If that route has no notFoundComponent, the error can bubble to a parent, eventually reaching the root when needed.
Follow-up: What is the default handling route? TanStack Router normally tries the route that threw the error, while notFoundMode can change route-matching behavior.
NotFoundErrornotFoundComponentNot Found ErrorsRouterOptions