Error Boundaries in React

From the React Router v7 cheat sheet · Error Handling · verified Jul 2026

Error Boundaries

Handling errors in loaders, actions, and components

typescript
import { useRouteError, isRouteErrorResponse } from 'react-router'

// Error boundary component
export function ErrorBoundary() {
  const error = useRouteError()

  if (isRouteErrorResponse(error)) {
    return (
      <div>
        <h1>{error.status} {error.statusText}</h1>
        <p>{error.data}</p>
      </div>
    )
  }

  return (
    <div>
      <h1>Oops!</h1>
      <p>{error?.message || "Unknown error"}</p>
    </div>
  )
}

// Throwing errors in loaders
export async function loader({ params }) {
  const post = await fetchPost(params.id)

  if (!post) {
    throw new Response("Not Found", { status: 404 })
  }

  return { post }
}
💡 Error boundaries catch errors in loaders, actions, and rendering
⚡ Use isRouteErrorResponse to handle thrown Responses
📌 Errors bubble up to nearest errorElement
🟢 Throw Response objects for HTTP-style errors
errorserror-boundaryhandling
Back to the full React Router v7 cheat sheet