Skip to content

Commit 65ff208

Browse files
author
Sylvenas
committed
feat: readme.md
1 parent f9fd24e commit 65ff208

File tree

5 files changed

+297
-69
lines changed

5 files changed

+297
-69
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
catch-react-error 正是来解决这个问题的,catch-react-error 在客户端渲染的时候正常使用`ErrorBoundary` 包裹组件;在服务端渲染的时候,使用 try-catch 包裹 render 函数, 这样则可以在同构应用中完美的处理 React 生命周期中发生的错误
1414

15-
## example
15+
## Demo
16+
17+
<div style="text-align:center" align="center">
18+
<img src="https://p1.music.126.net/6tHW45dHH_qKtCw0rrkJOg==/109951164571395030.gif" />
19+
</div>
1620

1721
### client side render
1822

example-server/pages/index.css

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,60 @@
22
iframe {
33
display: none !important;
44
}
5+
6+
.App {
7+
/* background: rgb(241, 243, 244); */
8+
}
9+
10+
.btns {
11+
display: flex;
12+
justify-content: space-around;
13+
margin-bottom: 30px;
14+
}
15+
16+
.btn {
17+
width: 100px;
18+
height: 30px;
19+
border-radius: 75px;
20+
background: rgb(241, 243, 244);
21+
color: #217ce8;
22+
font-size: 16px;
23+
/* outline: none; */
24+
border: none;
25+
}
26+
27+
.btn:active {
28+
transform: scale(1.2);
29+
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
30+
}
31+
32+
/* .btn:hover {
33+
background: gainsboro;
34+
35+
} */
36+
37+
h1 {
38+
text-align: center;
39+
margin: 0;
40+
height: 45px;
41+
}
42+
43+
div {
44+
line-height: 45px;
45+
font-size: 20px;
46+
text-align: center;
47+
}
48+
49+
.ok-btn {
50+
width: 200px;
51+
}
52+
53+
.ok-desc {
54+
line-height: 20px;
55+
text-align: left;
56+
font-size: 16px;
57+
}
58+
59+
.ok-content {
60+
line-height: 20px;
61+
}

example-server/pages/index.js

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,51 @@
1-
import React from "react";
2-
// import catchreacterror, { DefaultErrorBoundary } from "./dist";
3-
import catchreacterror, { DefaultErrorBoundary } from "catch-react-error";
1+
import React, { useState } from "react";
2+
import catchreacterror, { DefaultErrorBoundary } from "./dist";
3+
44
import "./index.css";
55

66
function App() {
7+
const [count, setCount] = useState(0);
8+
const [content, setContent] = useState([]);
79
return (
810
<div className="App">
9-
<header className="App-header">
10-
<h1>这是正常展示内容</h1>
11-
</header>
12-
<Test></Test>
13-
<SafeContent />
11+
<SaleCount count={count} />
12+
<section className="btns">
13+
<button className="btn" onClick={() => setCount(count => count + 1)}>
14+
+
15+
</button>
16+
<button className="btn" onClick={() => setCount(count => count - 1)}>
17+
-
18+
</button>
19+
</section>
20+
<hr />
21+
<section>
22+
<p className="ok-desc">
23+
Although the Count Component was crashed, but this part was not
24+
affected
25+
</p>
26+
<button
27+
className="btn ok-btn"
28+
onClick={() => {
29+
setContent(content => ["clicked!", ...content]);
30+
}}
31+
>
32+
I'm OK, click me !
33+
</button>
34+
{content.map(x => (
35+
<p className="ok-content">{x}</p>
36+
))}
37+
</section>
1438
</div>
1539
);
1640
}
1741

18-
class Test extends React.Component {
19-
constructor() {
20-
super();
21-
this.state = {
22-
foo: 1
23-
};
24-
this.buttonRef = React.createRef();
25-
}
26-
27-
render() {
28-
const { foo } = this.state;
29-
console.log(foo);
30-
return (
31-
<div>
32-
<Foo test="foo" />
33-
<Button text="click me" ref={this.buttonRef} />
34-
<Label list={["a", "abc", null, "abcd"]} />
35-
</div>
36-
);
37-
}
38-
39-
componentDidMount() {
40-
console.log(this.buttonRef.current);
42+
function Count({ count }) {
43+
if (count === 3) {
44+
throw new Error("count is three");
4145
}
46+
return <h1>{count}</h1>;
4247
}
43-
@catchreacterror()
44-
class Button extends React.Component {
45-
click() {
46-
console.log("button click");
47-
}
48-
render() {
49-
const emptyObj = {};
50-
console.log(emptyObj.a.b);
51-
return <button onClick={this.click}>click me</button>;
52-
}
53-
}
54-
55-
const Label = ({ list }, b, c) => {
56-
return list.map(x => <SafeContent x={x} kye={x} />);
57-
};
58-
59-
const Content = (props, b, c) => {
60-
return <div>{props.x.length}</div>;
61-
};
62-
63-
const SafeContent = catchreacterror(DefaultErrorBoundary)(Content);
6448

65-
const Foo = (a, b, c) => {
66-
return <p>Foo</p>;
67-
};
49+
const SaleCount = catchreacterror()(Count);
6850

6951
export default App;

0 commit comments

Comments
 (0)