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

@xmanscript/has-error-boundary

v3.0.2

Published

Heigher order function to catch errors in component level and prevent app crash

Downloads

28

Readme

@xmanscript/has-error-boundary

Commitizen friendly

Installation Guide

Before we dive into using @xmanscript/has-error-boundary, let's get it set up in your project. You can install it using your preferred package manager. Here's how to do it with npm:

npm install @xmanscript/has-error-boundary

Or, with yarn:

yarn add @xmanscript/has-error-boundary

Now, let's explore how this package can help you handle errors more effectively in your React app!

Introduction

In any React application, handling errors is crucial. Typically, you'd have one error boundary throughout your app that catches any errors occurring in any part of the app and displays an error message in the UI. However, what if you make a change or add a new feature, and an error occurs in a specific component, causing the entire app to crash? That's a problem we want to solve.

Enter @xmanscript/has-error-boundary. This library allows you to catch errors at the component level, preventing your entire app from crashing. Here's how to use it:

Usage

  1. Create a React custom component, let's call it Test. This component can contain errors you want to catch.
import { useState } from "react";

function Test() {
  const [count, setCount] = useState(0);

  return (
    <>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count} {cat}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </>
  );
}

In this component, there's an undefined variable cat that we're trying to access in our JSX code.

  1. To catch errors within this component, import the Higher-Order Component (HOC) hasErrorBoundary from @xmanscript/has-error-boundary and wrap your component with it.
import { hasErrorBoundary } from "@xmanscript/has-error-boundary";
import { useState } from "react";

function Test() {
  const [count, setCount] = useState(0);

  return (
    <>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count} {c}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </>
  );
}

export default hasErrorBoundary(Test);

An additional feature is that you can create a custom error component to display error messages in your own way. To do this, wrap your app with ErrorBoundaryProvider, as shown below:

import { ErrorBoundaryProvider } from "@xmanscript/has-error-boundary";
import { App } from "./App"; // Replace with your app import

<ErrorBoundaryProvider
  errorComponent={(params) => {
    return (
      <div>
        <p>{params?.errorLocation}</p>
        <p>{params?.fileName}</p>
        <p>{params?.errorMessage}</p>
      </div>
    );
  }}
>
  <App/>
</ErrorBoundaryProvider>

The errorComponent provided will be rendered whenever there is an error in your components wrapped by hasErrorBoundary.

The default error component looks like this: default error component

Note: For typescript projects if you want to type the return type of hasErrorBoundary, it is typed as GoodComponent. It's a generic type so you might need to give suitable type, else GoodComponent<any> will work perfectly.

Happy Coding 🚀.