Перейти к содержанию

ErrorBoundary

{"<errorboundary>"}

Catches uncaught errors and renders fallback content.

1
2
3
4
function ErrorBoundary(props: {
  fallback: JSX.Element | ((err: any, reset: () => void) => JSX.Element);
  children: JSX.Element;
}): () => JSX.Element;

Here's an example of how to use it:

1
2
3
<ErrorBoundary fallback={<div>Something went terribly wrong</div>}>
  <MyComp />
</ErrorBoundary>

If you want to customize the error message, you can pass a function as the fallback prop. The function will be called with the error and a reset function. The reset function will reset the error boundary and re-render the children.

1
2
3
4
5
<ErrorBoundary
  fallback={(err, reset) => <div onClick={reset}>Error: {err.toString()}</div>}
>
  <MyComp />
</ErrorBoundary>

Props

Name Type Description
fallback JSX.Element \| ((err: any, reset: () => void) => JSX.Element) The fallback content to render when an error is caught.

Комментарии