Skip to content

Commit

Permalink
feat: implement withErrorBoundary()
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 13, 2018
1 parent 2ce4951 commit cb88c75
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ const MyComponent = mock();
- [`<ViewportScrollSensor>`](./docs/en/ViewportSensor.md#viewportscrollsensor) and [`<ViewportObserverSensor>`](./docs/en/ViewportSensor.md#viewportobserversensor)
- [`<WindowScrollSensor>`](./docs/en/WindowScrollSensor.md), [`withWindowScroll()`](./docs/en/WindowScrollSensor.md#withwindowscroll-hoc), and [`@withWindowScroll`](./docs/en/WindowScrollSensor.md#withwindowscroll-decorator)
- [`<WindowSizeSensor>`](./docs/en/WindowSizeSensor.md), [`withWindowSize()`](./docs/en/WindowSizeSensor.md#withwindowsize-hoc), and [`@withWindowSize`](./docs/en/WindowSizeSensor.md#withwindowsize-decorator)
- `ActiveSensor`, `withActive()`, and `@withActive`
- `FocusSensor`, `withFocus()`, and `@withFocus`
- [Context](./docs/en/Context.md)
- [`<Provider>`](./docs/en/Provider.md#provider), [`<Consumer>`](./docs/en/Provider.md#consumer), [`withContext()`](./docs/en/Provider.md#withcontext-hoc), and [`@withContext`](./docs/en/Provider.md#withcontext-decorator)
- [`<Theme>`](./docs/en/theme.md#theme), [`<Themed>`](./docs/en/theme.md#themed), [`withTheme()`](./docs/en/theme.md#withtheme-hoc), and [`@withTheme`](./docs/en/theme.md#withtheme-decorator)
Expand All @@ -81,7 +79,7 @@ const MyComponent = mock();
- [`go()`](./docs/en/routing.md#go), `<Redirect>`, `<Link>`, [`<Sms>`](./docs/en/Sms.md), [`<Mailto>`](./docs/en/Mailto.md), and `<Tel>`
- [Boundaries](./docs/en/Boundaries.md)
- [`<BrowserOnly>`](./docs/en/BrowserOnly.md), [`<ServerOnly>`](./docs/en/ServerOnly.md), and [`<ElectronOnly>`](./docs/en/ElectronOnly.md)
- [`<ErrorBoundary>`](./docs/en/ErrorBoundary.md) and `withErrorBoundary()`
- [`<ErrorBoundary>`](./docs/en/ErrorBoundary.md) and [`withErrorBoundary()`](./docs/en/ErrorBoundary.md#witherrorboundary-hoc)
- `<CacheBoundary>`
- [UI](./docs/en/UI.md)
- [`<Portal>`](./docs/en/Portal.md) and [`<Overlay>`](./docs/en/Overlay.md)
Expand Down
19 changes: 19 additions & 0 deletions docs/en/ErrorBoundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ interface IErrorBoundaryState {

- `renderError` &mdash; renderer called if error happened in error boundary's children.
- `onError` &mdash; event called every time error detected.


## `withErrorBoundary` HOC

Wraps your component into an error boundary.

```jsx
import {withErrorBoundary} from 'libreact/lib/ErrorBoundary';

const SafeComponent = withErrorBoundary(UnsafeComponent, {
renderError: () => <div>Error happened!</div>
});
```

Signature

```ts
withErrorBoundary(Comp, boundaryProps: IErrorBoundaryProps)
```
2 changes: 1 addition & 1 deletion docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
- [`go()`](./routing.md#go), `<Redirect>`, `<Link>`, [`<Sms>`](./Sms.md), [`<Mailto>`](./Mailto.md), and `<Tel>`
- [Boundaries](./Boundaries.md)
- [`<BrowserOnly>`](./BrowserOnly.md), [`<ServerOnly>`](./ServerOnly.md), and [`<ElectronOnly>`](./ElectronOnly.md)
- [`<ErrorBoundary>`](./ErrorBoundary.md) and `withErrorBoundary()`
- [`<ErrorBoundary>`](./ErrorBoundary.md) and [`withErrorBoundary()`](./ErrorBoundary.md#witherrorboundary-hoc)
- `<CacheBoundary>`
- [UI](./UI.md)
- [`<Portal>`](./Portal.md) and [`<Overlay>`](./Overlay.md)
Expand Down
9 changes: 7 additions & 2 deletions src/ErrorBoundary/__story__/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Component, createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {linkTo} from '@storybook/addon-links';
import {ErrorBoundary} from '..';
import {ErrorBoundary, withErrorBoundary} from '..';
import ShowDocs from '../../../.storybook/ShowDocs'

class BuggyCounter extends Component<any, any> {
Expand All @@ -29,6 +29,10 @@ class BuggyCounter extends Component<any, any> {
}
}

const Hoc1 = withErrorBoundary(BuggyCounter, {
renderError: () => <div>Errror!</div>
});

storiesOf('Boundaries/ErrorBoundary', module)
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/ErrorBoundary.md')}))
.add('Renders children', () =>
Expand All @@ -42,4 +46,5 @@ storiesOf('Boundaries/ErrorBoundary', module)
<ErrorBoundary renderError={() => <div>Caught error!</div>}>
<BuggyCounter />
</ErrorBoundary>
);
)
.add('HOC', () => <Hoc1 />);
9 changes: 6 additions & 3 deletions src/ErrorBoundary/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Component} from 'react';
import faccToHoc from '../util/faccToHoc';
import {noop} from '../util';
import {h, noop} from '../util';

export interface IErrorBoundaryProps {
children: any;
Expand Down Expand Up @@ -36,4 +35,8 @@ export class ErrorBoundary extends Component<any, any> {
}
}

export const withErrorBoundary = faccToHoc(ErrorBoundary, 'error');
export const withErrorBoundary = (Comp, boundaryProps) => (props) => {
return h(ErrorBoundary, boundaryProps,
h(Comp, props)
);
};
2 changes: 1 addition & 1 deletion src/util/faccToHoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const faccToHoc = (Facc, prop?: string, wrapper = noWrap) => {
const Enhanced = (props) =>
h(Facc, faccProps, (state) => wrapper(Comp, propName, props, state));

return addClassDecoratorSupport(Enhanced);
return isClassDecoratorMethodCall ? addClassDecoratorSupport(Enhanced) : Enhanced;
};

return hoc;
Expand Down

0 comments on commit cb88c75

Please sign in to comment.