Skip to content

Commit 5bb6929

Browse files
authored
Merge pull request x-orpheus#3 from x-orpheus/feat/en-doc
feat: translate doc and README.md
2 parents 7b1c250 + 4dd27fa commit 5bb6929

File tree

5 files changed

+663
-112
lines changed

5 files changed

+663
-112
lines changed

README-zh_CN.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# catch-react-error
2+
3+
[![npm](https://img.shields.io/npm/v/catch-react-error?style=flat-square)](https://www.npmjs.com/package/catch-react-error)
4+
[![Build Status](https://travis-ci.org/x-orpheus/catch-react-error.svg?branch=master)](https://travis-ci.org/x-orpheus/catch-react-error)
5+
[![codecov](https://img.shields.io/codecov/c/gh/x-orpheus/catch-react-error?style=flat-square&logo=codecov)](https://codecov.io/gh/x-orpheus/catch-react-error)
6+
7+
[English](./README.md) | 简体中文
8+
9+
[catch-react-error 由来的故事](./doc/catch-react-error.md)
10+
11+
## 简介
12+
13+
对于 React 生命周期中发生的错误,只需要使用 `ErrorBoundary` 组件包裹可能出现异常的组件即可。
14+
15+
但是这种方案只适用于客户端渲染,然而对于服务端渲染出现的错误,却完全无能为力;现在大型的项目无不采用服务端渲染的方式,所以需要找到一个简单易用的能够应用于**同构应用**的 React 错误处理方案。
16+
17+
`catch-react-error` 正是来解决这个问题的,`catch-react-error` 在客户端渲染的时候正常使用`ErrorBoundary` 包裹组件;在服务端渲染的时候,使用 `try-catch` 包裹 `render` 函数, 这样则可以在同构应用中完美的处理 React 生命周期中发生的错误。
18+
19+
## Demo
20+
21+
<div style="text-align:center" align="center">
22+
<img src="https://p1.music.126.net/6tHW45dHH_qKtCw0rrkJOg==/109951164571395030.gif" />
23+
</div>
24+
25+
### client side render
26+
27+
```sh
28+
cd example-client
29+
npm run dev
30+
```
31+
32+
### server side render
33+
34+
```sh
35+
cd example-server
36+
npm run dev
37+
```
38+
39+
## How to use
40+
41+
### 1.安装 catch-react-error
42+
43+
```sh
44+
npm install catch-react-error --save
45+
```
46+
47+
### 2.安装 ES7 Decorator babel plugin
48+
49+
我们采用 ES7 的 `Decorator` 语法来让代码更简洁,当然也可以采用函数式的写法
50+
51+
```sh
52+
npm install --save-dev @babel/plugin-proposal-decorators
53+
npm install --save-dev @babel/plugin-proposal-class-properties
54+
55+
```
56+
57+
添加 babel plugin
58+
59+
```json
60+
{
61+
"plugins": [
62+
["@babel/plugin-proposal-decorators", { "legacy": true }],
63+
["@babel/plugin-proposal-class-properties", { "loose": true }]
64+
]
65+
}
66+
```
67+
68+
### 3.导入 catch-react-error
69+
70+
```jsx
71+
import catchreacterror from "catch-react-error";
72+
```
73+
74+
### 4.使用@catchreacterror decorator
75+
76+
```jsx
77+
@catchreacterror()
78+
class Count extends React.Component {
79+
render() {
80+
const { count } = this.props;
81+
if (count === 3) {
82+
throw new Error("count is three");
83+
}
84+
return <h1>{count}</h1>;
85+
}
86+
}
87+
```
88+
89+
`catchreacterror` 本质上是一个柯里化的函数,函数签名为:
90+
91+
```
92+
catchreacterror :: ErrorBoundary -> Function -> Component
93+
```
94+
95+
`catchreacterror` 函数接受一个参数: ErrorBoundary (默认采用`DefaultErrorBoundary`),返回一个 High-Order-Function。
96+
97+
其原理为:客户端渲染会用 React 16 的[Error Boundary](https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html)的相关函数来处理错误,服务端用`try catch`来捕获 render 的错误。
98+
99+
### 5.使用@catchreacterror 处理 FunctionComponent
100+
101+
上面是对于 `ClassComponent` 做的处理,但是有些人喜欢用函数组件,这里也提供使用方法,如下。
102+
103+
```js
104+
// 如果没有使用 catchreacterror 包裹 Content 组件,
105+
// 则在 count 为 3 的情况下则必然报错,导致程序崩溃
106+
function Count({ count }) {
107+
if (count === 3) {
108+
throw new Error("count is three");
109+
}
110+
return <h1>{count}</h1>;
111+
}
112+
113+
// 被 catchreacterror 包裹之后会 catch 住错误,
114+
// 仅仅导致当前组件被销毁,其他内容正常展示
115+
const SaleCount = catchreacterror()(Count);
116+
117+
function App() {
118+
return (
119+
<div className="App">
120+
      
121+
<SaleCount count={3} />
122+
<button>I'm OK, click me !</button>
123+
</div>
124+
);
125+
}
126+
```
127+
128+
### 6.如何编写 CustomErrorBoundary
129+
130+
拷贝下面 React 提供的事例,可以添加日志,自定义 fallback 内容
131+
132+
```js
133+
class CustomErrorBoundary extends React.Component {
134+
constructor(props) {
135+
super(props);
136+
this.state = { hasError: false };
137+
}
138+
139+
static getDerivedStateFromError(error) {
140+
// Update state so the next render will show the fallback UI.
141+
return { hasError: true };
142+
}
143+
144+
componentDidCatch(error, errorInfo) {
145+
// You can also log the error to an error reporting service
146+
logErrorToMyService(error, errorInfo);
147+
}
148+
149+
render() {
150+
if (this.state.hasError) {
151+
// You can render any custom fallback UI
152+
return <h1>Something went wrong.</h1>;
153+
}
154+
}
155+
156+
return this.props.children;
157+
}
158+
}
159+
```
160+
161+
## License
162+
163+
MIT.

README.md

Lines changed: 75 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
[![Build Status](https://travis-ci.org/x-orpheus/catch-react-error.svg?branch=master)](https://travis-ci.org/x-orpheus/catch-react-error)
55
[![codecov](https://img.shields.io/codecov/c/gh/x-orpheus/catch-react-error?style=flat-square&logo=codecov)](https://codecov.io/gh/x-orpheus/catch-react-error)
66

7-
## 简介
7+
English | [简体中文](./README-zh_CN.md)
88

9-
对于 React 生命周期中发生的错误,只需要使用 ErrorBoundary 组件包裹可能出现异常的组件即可。
9+
[Why we create catch-react-error](./doc/catch-react-error_EN.md)
1010

11-
但是这种方案只适用于客户端渲染,然而对于服务端渲染出现的错误,却完全无能为力;现在大型的项目无不采用服务端渲染的方式,所以需要找到一个简单易用的能够应用于同构应用的 React 错误处理方案;
11+
## Introduction
1212

13-
catch-react-error 正是来解决这个问题的,catch-react-error 在客户端渲染的时候正常使用`ErrorBoundary` 包裹组件;在服务端渲染的时候,使用 try-catch 包裹 render 函数, 这样则可以在同构应用中完美的处理 React 生命周期中发生的错误
13+
For errors that occur in the React lifecycle, you only need to use the `ErrorBoundary` component to wrap components that may have exceptions.
14+
15+
However, this solution is only suitable for client-side rendering, but it is completely helpless for errors in server-side rendering; nowadays, large-scale projects all adopt server-side rendering, so we need to find an easy-to-use application that can be applied to **isomorphic applications**. React error handling scheme.
16+
17+
`catch-react-error` is exactly to solve this problem, `catch-react-error` normally uses `ErrorBoundary` to wrap components when rendering on the client; when server-side rendering, use `try-catch` to wrap the `render` function, so you can Errors in the React lifecycle.
1418

1519
## Demo
1620

@@ -34,119 +38,125 @@ npm run dev
3438

3539
## How to use
3640

37-
### 1.安装 catch-react-error
41+
### 1.install catch-react-error
3842

3943
```sh
40-
npm install catch-react-error --save
44+
npm install catch-react-error --save
4145
```
4246

43-
### 2.安装 ES7 Decorator babel plugin
47+
### 2. Install ES7 Decorator babel plugin
4448

45-
我们采用 ES7`Decorator` 语法来让代码更简洁,当然也可以采用函数的写法
49+
We use ES7's Decorator syntax to make the code more concise, of course, you can also use the functional style.
4650

4751
```sh
48-
npm install --save-dev @babel/plugin-proposal-decorators
49-
npm install --save-dev @babel/plugin-proposal-class-properties
50-
52+
npm install --save-dev @ babel / plugin-proposal-decorators
53+
npm install --save-dev @ babel / plugin-proposal-class-properties
5154
```
5255

53-
添加 babel plugin
56+
Add babel plugin
5457

5558
```json
5659
{
5760
"plugins": [
58-
["@babel/plugin-proposal-decorators", { "legacy": true }],
59-
["@babel/plugin-proposal-class-properties", { "loose": true }]
61+
["@ babel / plugin-proposal-decorators", { "legacy": true }],
62+
["@ babel / plugin-proposal-class-properties", { "loose": true }]
6063
]
6164
}
6265
```
6366

64-
### 3.导入 catch-react-error
67+
### 3. import catch-react-error
6568

6669
```jsx
6770
import catchreacterror from "catch-react-error";
6871
```
6972

70-
### 4.使用@catchreacterror decorator
73+
### 4. Use @catchreacterror decorator
7174

7275
```jsx
7376
@catchreacterror()
74-
class Test extends React.Component {
77+
class Count extends React.Component {
7578
render() {
76-
return <Button text="click me" />;
79+
const { count } = this.props;
80+
if (count === 3) {
81+
throw new Error("count is three");
82+
}
83+
return <h1>{count}</h1>;
7784
}
7885
}
7986
```
8087

81-
`catchreacterror` 本质上是一个柯里化的函数,函数签名为:
88+
`catchreacterror` is essentially a curried function with a signature:
8289

8390
```
84-
catchreacterror :: ErrorBoundary -> Function -> Component
91+
catchreacterror :: ErrorBoundary-> Function-> Component
8592
```
8693

87-
`catchreacterror` 函数接受一个参数: ErrorBoundary (默认采用`DefaultErrorBoundary`),返回一个 High-Order-Function
94+
The `catchreacterror` function accepts one parameter: ErrorBoundary (`DefaultErrorBoundary` is used by default) and returns a `High-Order-Function`.
8895

89-
其原理为:客户端渲染会用 React 16[Error Boundary](https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html)的相关函数来处理错误,服务端用`try catch`来捕获 render 的错误。
96+
The principle is: client-side rendering will use React 16's [Error Boundary](https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html) related functions to handle errors, and server-side use `try catch` to catch `render` errors.
9097

91-
### 5.使用@catchreacterror 处理 FunctionComponent
98+
### 5. Use @catchreacterror to handle FunctionComponent
9299

93-
上面是对于 `ClassComponent` 做的处理,但是有些人喜欢用函数组件,这里也提供使用方法,如下。
100+
The above is the processing for `ClassComponent`, but some people like to use function components, here are also provided methods of use, as follows.
94101

95102
```js
96-
// 如果没有使用 catchreacterror 包裹 Content 组件,
97-
// 则在 x 为 null 的情况下则必然报错,导致程序崩溃
98-
const Content = (props) => {
99-
return <div>{props.x.length}</div>;
100-
};
103+
// If the Count component is not wrapped with catchreacterror,
104+
// If count is 3, an error will be reported, causing the program to crash
105+
function Count({ count }) {
106+
if (count === 3) {
107+
throw new Error("count is three");
108+
}
109+
return <h1>{count}</h1>;
110+
}
101111

102-
// catchreacterror 包裹之后会 catch 住错误,
103-
// 仅仅导致当前组件被销毁,其他内容正常展示
104-
const SafeContent = catchreacterror(DefaultErrorBoundary)(Content);
112+
// After being wrapped by catchreacterror, the error will be caught,
113+
// Only cause the current component to be destroyed, and other content is displayed normally
114+
const SaleCount = catchreacterror()(Count);
105115

106116
function App() {
107117
return (
108118
<div className="App">
109-
<header className="App-header">
110-
<h1>这是正常展示内容</h1>
111-
</header>
112-
<SafeContent x={null}/>
119+
      
120+
<SaleCount count={3} />
121+
<button>I'm OK, click me !</button>
122+
</div>
113123
);
114124
}
115125
```
116126
117-
### 6.如何编写 CustomErrorBoundary
127+
### 6. How to write a CustomErrorBoundary
118128
119-
拷贝下面 React 提供的事例,可以添加日志,自定义 fallback 内容
129+
Copy the examples provided by React below, you can add logs and customize the fallback content
120130
121-
```js
122-
class CustomErrorBoundary extends React.Component {
123-
constructor(props) {
124-
super(props);
125-
this.state = { hasError: false };
126-
}
127-
128-
static getDerivedStateFromError(error) {
129-
// Update state so the next render will show the fallback UI.
130-
return { hasError: true };
131-
}
132-
133-
componentDidCatch(error, errorInfo) {
134-
// You can also log the error to an error reporting service
135-
logErrorToMyService(error, errorInfo);
136-
}
137-
138-
render() {
139-
if (this.state.hasError) {
140-
// You can render any custom fallback UI
141-
return <h1>Something went wrong.</h1>;
142-
}
143-
}
144-
145-
return this.props.children;
146-
}
131+
```jsx
132+
class CustomErrorBoundary extends React. Component {
133+
  constructor (props) {
134+
    super (props);
135+
    this.state = {hasError: false};
136+
  }
137+
138+
  static getDerivedStateFromError (error) {
139+
    // Update state so the next render will show the fallback UI.
140+
    return {hasError: true};
141+
  }
142+
143+
  componentDidCatch (error, errorInfo) {
144+
    // You can also log the error to an error reporting service
145+
    logErrorToMyService (error, errorInfo);
146+
  }
147+
148+
  render () {
149+
    if (this.state.hasError) {
150+
      // You can render any custom fallback UI
151+
      return <h1> Something went wrong. </ h1>;
152+
    }
153+
  }
154+
155+
    return this.props.children;
156+
  }
147157
}
148158
```
149159
150160
## License
151161
152-
MIT
162+
MIT.

0 commit comments

Comments
 (0)