Permalink
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (41 sloc) 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)