Skip to content

Latest commit

 

History

History
58 lines (41 loc) · 1.16 KB

ErrorBoundary.md

File metadata and controls

58 lines (41 loc) · 1.16 KB

<ErrorBoundary>

Creates an error boundary, using React's .componentDidCatch() API.

Usage

import {ErrorBoundary} from 'libreact/lib/ErrorBoundary';

<ErrorBoundary
  renderError={({error, info}) => <div>ERROR!</div>}
  onError={({error, info}) => {/* ... */}}
>
  <div>
    This code is protected by error boundary.
  </div>
</ErrorBoundary>

Props

Signature

interface IErrorBoundaryProps {
  renderError?: (state: IErrorBoundaryState) => React.ReactElement;
  onError?: (error?: Error, info?) => void;
}

interface IErrorBoundaryState {
  error?: Error;
  info?: any;
}

, were

  • renderError — renderer called if error happened in error boundary's children.
  • onError — event called every time error detected.

withErrorBoundary HOC

Wraps your component into an error boundary.

import {withErrorBoundary} from 'libreact/lib/ErrorBoundary';

const SafeComponent = withErrorBoundary(UnsafeComponent, {
  renderError: () => <div>Error happened!</div>
});

Signature

withErrorBoundary(Comp, boundaryProps: IErrorBoundaryProps)