@gdo-bzh/error-boundary

React component that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.

Usage no npm install needed!

<script type="module">
  import gdoBzhErrorBoundary from 'https://cdn.skypack.dev/@gdo-bzh/error-boundary';
</script>

README

version

error-boundary

NPM JavaScript Style Guide

React component that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.

more info here

Install

yarn add @gdo-bzh/error-boundary react

Usage

import React from 'react'

const Example = () => (
  <ErrorBoundary Fallback={({error, onRetry}) => (
    <div>
      <pre>{error.message}</pre>
      <button type="button" onClick={onRetry}>retry</button>
    </div>)}>
    <div>somme content ...</div>
  </ErrorBoundary>
)

Types

/**
 * Fallback component Props
 */
export type FallbackProps = {
  /**
   * Error object
   */
  error: Error;
  /**
   * retry callback
   */
  onRetry: () => void;
}

type Props = {
  /**
   * Fallback component
   */
  Fallback: React.ComponentType<FallbackProps>
  /**
   * logError callback to log error to the server for instance
   */
  logError?: (error: Error, errorInfo: React.ErrorInfo) => void
}

License

MIT © gdo-bzh