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.
The notFound function accepts a single optional argument, the options to create the not-found error object.
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
})
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.