npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-app-error-boundary

v1.0.5

Published

Allows to opt-out of react-error-overlay in your react-app

Downloads

797

Readme

react-app-error-boundary

Allows you to turn react-error-overlay in your react-app from a mandatory-thing-you-never-asked-for into a handy-opt-in-feature.

Inspired by this SO question.

Why ever need this?

See discussion under this answer.

In very short:

When developing error boundary components and styling/testing how they look, this is an extremely annoying feature. It slows down development and adds no value. You should allow developers to decide whether they want this feature turned on or not

Why not just disable overlay?

Meaning, you always can simply do this:

import { stopReportingRuntimeErrors } from "react-error-overlay"
stopReportingRuntimeErrors()

which will disable overlay once and for all.

Well, because overlay is handy for unexpected errors. It works great on it's own. But when you know for sure that in some specific place you don't need overlay (for example, you want error from API to be caught by error boundary, this is an expected flow) – you don't have options for that. All or nothing.

That's where this solution comes into play.

Demo

Here is a repo where you can see this package in action.

Installation

npm install --save react-app-error-boundary
# or
yarn add react-app-error-boundary

Usage

  1. Set up error handler in entry point:
// src/index.jsx
import { setupReactAppOverlayErrorHandler } from 'react-app-error-boundary'

ReactDOM.render(...)

setupReactAppOverlayErrorHandler()
  1. Wrap desired application part into <ErrorBoundary> component:
import { ErrorBoundary } from 'react-app-error-boundary'

<ErrorBoundary>
  <DangerousComponent />
</ErrorBoundary>

That's it. With this configuration, if <DangerousComponent /> will throw, you should observe following:

  • error message, written in red, in place of broken component
  • error log in console, starting with Following error has been caught by <ErrorBoundary> component...
  • no react-error-overlay displayed!

Additional configuration

For general usage of ErrorBoundary component, see documentation in original repo. All original props are available here too. Other stuff from repo also is re-exported, so you may also use extra features like useErrorHandler hook.

setDefaultErrorBoundaryFallback(errorRenderer)

In difference from original ErrorBoundary, here you have a default FallbackComponent provided. So you don't need to specify it explicitly every time.

To customize default fallback, use setDefaultErrorBoundaryFallback method:

// src/index.jsx
import { setDefaultErrorBoundaryFallback } from 'react-app-error-boundary'

setDefaultErrorBoundaryFallback(({ error }) => (
  <MyBeautifulErrorRenderer>{error.message}</MyBeautifulErrorRenderer>
))

allowDevErrorOverlay

If for some reason you want react-error-overlay displayed as it does normally, you may allow it for particular ErrorBoundary:

<ErrorBoundary allowDevErrorOverlay>...</ErrorBoundary>

logCaughtErrors

By default, errors handled by ErrorBoundary will still be logged in console (only in development mode). If you want to absolutely suppress caught errors, you may set it for particular ErrorBoundary:

<ErrorBoundary logCaughtErrors={false}>...</ErrorBoundary>

setDefaultErrorBoundaryOptions(options)

Default behavior of ErrorBoundary can be changed globally:

import { setDefaultErrorBoundaryOptions } from 'react-app-error-boundary'

setDefaultErrorBoundaryOptions({
  logCaughtErrors: false,
  allowDevErrorOverlay: true,
})

craOverlaySelector

This is an emergency option, just in case if styles of react-error-overlay will change – so this package will fail to hide it – and patch won't be released for some reason.

With this, you'll be able to patch this issue yourself:

import { setupReactAppOverlayErrorHandler } from 'react-app-error-boundary'

setupReactAppOverlayErrorHandler({
  craOverlaySelector: 'any-css-selector-here'
})

How it works

In general, it's based on this answer.

It sets up a global error handler – to intercept uncaught errors before react-error-overlay does, and handle them in a special way.

And it provides customized ErrorBoundary component (based on react-error-boundary), which marks errors handled by it as caught, so our global handler can decide whether to show overlay.

Unfortunately, using event.stopImmediatePropagation() to prevent react-error-overlay from handling errors is not appropriate (see comment under the mentioned answer), because it breaks all error-boundaries too. So instead, to "disable" overlay we'll just hide it via display: none.

Credits

Big thanks to react-error-boundary for a super convenient error-boundaries api.

LICENSE

MIT