For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/en/components/error-boundary.md.

ErrorBoundary

Provide a fallback when content fails to render.

Basic usage

Content is displayed normally.

basic.tsx
function Content({ broken }: { broken: boolean }) {
  if (broken) throw new Error('Demo rendering error');
  return <p>Content is displayed normally.</p>;
}

import { Button, ErrorBoundary, Space } from '@ifhover/leaf-ui';

import { useState } from 'react';

export function ErrorBoundaryBasic() {
  const [broken, setBroken] = useState(false);
  return (
    <Space direction="vertical" align="stretch">
      <Button variant="outline" onClick={() => setBroken(true)}>
        Simulate a rendering error
      </Button>
      <ErrorBoundary onReset={() => setBroken(false)}>
        <Content broken={broken} />
      </ErrorBoundary>
    </Space>
  );
}

More examples

The fallback replaces only the failed section.
advanced.tsx
function Content({ broken }: { broken: boolean }) {
  if (broken) throw new Error('Demo rendering error');
  return <span>The fallback replaces only the failed section.</span>;
}

import { Alert, Button, ErrorBoundary, Space } from '@ifhover/leaf-ui';

import { useState } from 'react';

export function ErrorBoundaryCustom() {
  const [broken, setBroken] = useState(false);
  return (
    <Space direction="vertical" align="stretch">
      <Button variant="outline" onClick={() => setBroken(true)}>
        Test fallback
      </Button>
      <ErrorBoundary
        fallback={({ reset }) => (
          <Alert
            title="This section is unavailable"
            description={
              <Button size="sm" variant="soft" onClick={reset}>
                Try again
              </Button>
            }
          />
        )}
        onReset={() => setBroken(false)}
        resetKeys={[broken]}
      >
        <Content broken={broken} />
      </ErrorBoundary>
    </Space>
  );
}

Usage

Catches descendant rendering and lifecycle errors. Handle event, async request and server errors in the operation or framework. Report through onError and avoid exposing internal stacks.

Types

Import these types for data, callbacks and refs.

import type { ErrorBoundaryFallbackProps, ErrorBoundaryProps } from '@ifhover/leaf-ui';

ErrorBoundaryFallbackProps API

PropertyTypeDefaultDescription
errorErrorRequiredError object or notification
reset() => voidRequiredReset or retry

ErrorBoundaryProps API

PropertyTypeDefaultDescription
childrenReactNode—Content or child items
fallbackReactNode | ((props: ErrorBoundaryFallbackProps) => ReactNode)—Loading error content
resetKeysreadonly unknown[]—Reset errors when these values change
onError(error: Error, info: ErrorInfo) => void—Error callback
onReset() => void—Reset callback