TanStackRouter v1
Auto
Framework
Version

notFound function

The notFound function returns a new NotFoundError object that can be either returned or thrown from places like a Route's beforeLoad or loader callbacks to trigger the notFoundComponent.

notFound options

The notFound function accepts a single optional argument, the options to create the not-found error object.

notFound returns

  • If the throw property is true in the options object, the NotFoundError object will be thrown from within the function call.
  • If the throw property is false | undefined in the options object, the NotFoundError object will be returned.

Examples

tsx
import { notFound, createFileRoute } from '@tanstack/react-router'

const Route = new createFileRoute('/posts/$postId')({
  // throwing a not-found object
  loader: ({ context: { post } }) => {
    if (!post) {
      throw notFound()
    }
  },
  // or if you want to show a not-found on the whole page
  loader: ({ context: { team } }) => {
    if (!team) {
      throw notFound({ global: true })
    }
  },
  // ... other route options
})
import { notFound, createFileRoute } from '@tanstack/react-router'

const Route = new createFileRoute('/posts/$postId')({
  // throwing a not-found object
  loader: ({ context: { post } }) => {
    if (!post) {
      throw notFound()
    }
  },
  // or if you want to show a not-found on the whole page
  loader: ({ context: { team } }) => {
    if (!team) {
      throw notFound({ global: true })
    }
  },
  // ... other route options
})
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.