Skip to content

Commit 36c5504

Browse files
committed
docs: add <ErrorBoundary> docs
1 parent 1fc9fd3 commit 36c5504

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const MyComponent = mock();
8080
- [`<FullScreen>`](./docs/en/FullScreen.md)
8181
- [Boundaries](./docs/en/Boundaries.md)
8282
- [`<BrowserOnly>`](./docs/en/BrowserOnly.md), [`<ServerOnly>`](./docs/en/ServerOnly.md), and [`<ElectronOnly>`](./docs/en/ElectronOnly.md)
83-
- `<ErrorBoundary>` and `withErrorBoundary()`
83+
- [`<ErrorBoundary>`](./docs/en/ErrorBoundary.md) and `withErrorBoundary()`
8484
- `<CacheBoundary>`
8585
- [UI](./docs/en/UI.md)
8686
- [`<Slider>`](./docs/en/Slider.md)

docs/en/Boundaries.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ according to some semantic meaning.
66
- [`<BrowserOnly>`](./BrowserOnly.md) &mdash; renders children only in browser.
77
- [`<ServerOnly>`](./ServerOnly.md) &mdash; renders children only on server.
88
- [`<ElectronOnly>`](./ElectronOnly.md) &mdash; renders children only in an Electron app.
9+
- [`<ErrorBoundary>`](./ErrorBoundary.md) &mdash; reports errors in its children.

docs/en/ErrorBoundary.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# `<ErrorBoundary>`
2+
3+
Creates an error boundary, using [React's `.componentDidCatch()` API](https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html).
4+
5+
## Usage
6+
7+
```jsx
8+
import {ErrorBoundary} from 'libreact/lib/ErrorBoundary';
9+
10+
<ErrorBoundary
11+
renderError={({error, info}) => <div>ERROR!</div>}
12+
onError={({error, info}) => {/* ... */}}
13+
>
14+
<div>
15+
This code is protected by error boundary.
16+
</div>
17+
</ErrorBoundary>
18+
```
19+
20+
## Props
21+
22+
Signature
23+
24+
```ts
25+
interface IErrorBoundaryProps {
26+
renderError?: (state: IErrorBoundaryState) => React.ReactElement;
27+
onError?: (error?: Error, info?) => void;
28+
}
29+
30+
interface IErrorBoundaryState {
31+
error?: Error;
32+
info?: any;
33+
}
34+
```
35+
36+
, were
37+
38+
- `renderError` &mdash; renderer called if error happened in error boundary's children.
39+
- `onError` &mdash; event called every time error detected.

docs/en/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
- [`<FullScreen>`](./FullScreen.md)
5050
- [Boundaries](./Boundaries.md)
5151
- [`<BrowserOnly>`](./BrowserOnly.md), [`<ServerOnly>`](./ServerOnly.md), and [`<ElectronOnly>`](./ElectronOnly.md)
52-
- `<ErrorBoundary>` and `withErrorBoundary()`
52+
- [`<ErrorBoundary>`](./ErrorBoundary.md) and `withErrorBoundary()`
5353
- `<CacheBoundary>`
5454
- [UI](./UI.md)
5555
- [`<Slider>`](./Slider.md)

src/ErrorBoundary/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import {noop} from '../util';
44

55
export interface IErrorBoundaryProps {
66
children: any;
7-
render: any;
8-
comp: any;
9-
component: any;
10-
fallback?: (state: IErrorBoundaryState) => React.ReactElement<any>;
7+
renderError?: (state: IErrorBoundaryState) => React.ReactElement<any>;
118
onError?: (error?: Error, info?) => void;
129
}
1310

@@ -30,10 +27,10 @@ export class ErrorBoundary extends Component<any, any> {
3027

3128
render () {
3229
const {props, state} = this;
33-
const {fallback} = props;
30+
const {renderError, children} = props;
3431

3532
return state.error ?
36-
(typeof fallback === 'function' ? fallback(state) : null) :
37-
renderProp(props, state);
33+
(typeof renderError === 'function' ? renderError(state) : null) :
34+
children;
3835
}
3936
}

0 commit comments

Comments
 (0)