Git Product home page Git Product logo

react-badly's Introduction

react-badly

Take hold of your react lifecycle hooks with react-badly

npm version

You can install off of npm with

npm install react-badly

# or yarn

yarn add react-badly

This component is a wrapper for any of your React 16+ plus components that may have an error in them.

How to use

The simplest way is to just wrap any component that you think may error with ReactBadly

This will prevent the component from rendering (also will stop any children in the tree as well). This is to make sure that your whole component tree does not dismount as React 16+ does.

import ReactBadly from 'react-badly';


// some code later on

<ReactBadly>
  <SomeComponentThatMayHaveAnError>
    ...
  </SomeComponentThatMayHaveAnError>
</ReactBadly>

If you want to handle your error with some functionality (like sending to analytics etc) you can pass an onError property which will get the error and any info as parameters from react.

import ReactBadly from 'react-badly';

const errorFunction = (error, info) => {
  // can handle the error here and do what you will with it
};

// some code later on

<ReactBadly onError={errorFunction}>
  <SomeComponentThatMayHaveAnError>
    ...
  </SomeComponentThatMayHaveAnError>
</ReactBadly>

There may also be some cases where you actually want to render something else to display if there was an error instead of just not displaying anything. To do that you can pass the render property which will accept a function that will take in { error, info } as a parameter. This will render instead of the direct child of ReactBadly.

import ReactBadly from 'react-badly';

const renderError = ({ error, info }) => (
  <React.Fragment>
      <h2>You have an error!</h2>
      <pre>{JSON.stringify(error)}'\n'{JSON.stringify(info)}</pre>
  </React.Fragment>
);

// some code later on

<ReactBadly render={renderError}>
  <SomeComponentThatMayHaveAnError>
    ...
  </SomeComponentThatMayHaveAnError>
</ReactBadly>

react-badly's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar maniator avatar snyk-bot avatar timbuckley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

timbuckley

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.