Skip to content

React Error Boundary Catch Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about React Error Boundary Catch Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

React error boundaries do not catch errors from event handlers or async code.

Wrong

class ErrorBoundary extends React.Component {
  componentDidCatch(error, info) {
    console.error('Caught:', error)
  }

  render() {
    return this.props.children
  }
}

function App() {
  return (
    <ErrorBoundary>
      <button onClick={() => { throw new Error('Click error') }}>
        Click me
      </button>
    </ErrorBoundary>
  )
}

Output: the error from the click handler is not caught by the error boundary.

Error boundaries only catch errors during rendering, lifecycle methods, and constructors. For event handlers, use try-catch:

function App() {
  const [error, setError] = useState(null)

  const handleClick = () => {
    try {
      throw new Error('Click error')
    } catch (e) {
      setError(e)
    }
  }

  if (error) {
    return <h1>Error: {error.message}</h1>
  }

  return <button onClick={handleClick}>Click me</button>
}

For async errors, use error boundaries with componentDidCatch for render errors and try-catch for async:

function DataFetcher() {
  const [data, setData] = useState(null)
  const [error, setError] = useState(null)

  useEffect(() => {
    fetch('/api/data')
      .then(res => res.json())
      .then(setData)
      .catch(setError)
  }, [])

  if (error) throw error // Re-throw for error boundary
  return <div>{data}</div>
}

Expected output: all error types are handled appropriately.

Prevention

  • Wrap async code in try-catch blocks
  • Throw caught async errors in render for error boundary to catch
  • Use error boundaries only for render and lifecycle errors

Common Mistakes with error boundary catch

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What errors does a React error boundary NOT catch?

Error boundaries do not catch errors in event handlers, async code (setTimeout, Promises), server-side rendering, or errors thrown in the error boundary itself.

How do I catch async errors with error boundaries?

Catch the error in your async function, store it in state, and re-throw it in the render method to trigger the error boundary.

Can I use hooks to create an error boundary?

No. Error boundaries must be class components because they require componentDidCatch or getDerivedStateFromError lifecycle methods.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro