Skip to content
/ eitherx Public

πŸ’Š Your go-to, prescribed, error-boundary component for React

License

Notifications You must be signed in to change notification settings

mfix22/eitherx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

3b0c738 Β· May 7, 2022

History

59 Commits
Jan 24, 2019
Feb 25, 2019
Oct 15, 2017
Oct 15, 2017
Jan 29, 2019
Feb 25, 2019
Aug 15, 2021
May 7, 2022

Repository files navigation

<Eitherx />

πŸ’Š Super simple, reusable either-or error boundaries for React. Your go-to, prescribed, error-boundary component.

bundle size tested with jest PRs welcome MIT License

Usage

$ npm i --save eitherx
import Either from 'eitherx'

// Wrapper-component Style
<Either>
  {/* Either . . . */}
  <p>"Happy Child πŸ˜„"</p>
  {/* Or . . . */}
  <p>"Error child ☹️ (but at least your UI is happy)"</p>
</Either>

// OR use the `render` and `catchError` props

// Render-prop Style
<Either
  render={() => (<p>"Happy Child πŸ˜„"</p>)}
  catchError={({ error }) => (
    <div>
      <p>{`Error: ${error}`}</p>
    </div>
  )}
/>

Wrapper Component Style

Eitherx either renders the first child component, unless an error occurred while rendering, then the second child is rendered. Easy enough πŸ˜„.

If you do not pass a second child, and an error occurs, null will be return to React to render.

Render-prop Style

Using this style, you must pass a function for both render and catchError. If an error occurs, the component returned from catchError will be rendered. Otherwise Eitherx will render the component returned from render.

The catchError function receives an object with the field error set, both of which are passed directly from React Error Boundaries.

Handling Errors

With either style, you can pass an handleError callback prop to add additional error handling, logging, etc.

Example
<Eitherx
  handleError={({ error, info }) => {
    console.log(info)
    console.error(error)
  }}
  render={() => <Component>"Render Prop"</Component>}
  catchError={() => <p>"Catch Prop"</p>}
/>