Skip to content

Commit f9fd24e

Browse files
author
Sylvenas
committed
feat: 添加demo
1 parent 16c5985 commit f9fd24e

File tree

6 files changed

+99
-112
lines changed

6 files changed

+99
-112
lines changed

example-client/src/App.css

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
1+
/* 仅仅是为了,隐藏框架自带的开发环境error overlay */
2+
iframe {
3+
display: none !important;
4+
}
5+
16
.App {
2-
text-align: center;
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;
347
}
448

5-
.App-logo {
6-
height: 40vmin;
49+
.ok-btn {
50+
width: 200px;
751
}
852

9-
.App-header {
10-
background-color: #282c34;
11-
min-height: 100vh;
12-
display: flex;
13-
flex-direction: column;
14-
align-items: center;
15-
justify-content: center;
16-
font-size: calc(10px + 2vmin);
17-
color: white;
53+
.ok-desc {
54+
line-height: 20px;
55+
text-align: left;
56+
font-size: 16px;
1857
}
1958

20-
.App-link {
21-
color: #09d3ac;
59+
.ok-content {
60+
line-height: 20px;
2261
}

example-client/src/App.js

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,51 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import catchreacterror, { DefaultErrorBoundary } from "./dist";
33

4+
import "./App.css";
5+
46
function App() {
7+
const [count, setCount] = useState(0);
8+
const [content, setContent] = useState([]);
59
return (
610
<div className="App">
7-
<header className="App-header">
8-
<h1>这是正常展示内容</h1>
9-
</header>
10-
<Test></Test>
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>
1138
</div>
1239
);
1340
}
1441

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

64-
const SafeContent = catchreacterror(DefaultErrorBoundary)(Content);
49+
const SaleCount = catchreacterror()(Count);
6550

6651
export default App;

example-client/src/dist/catch-react-error.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ exports["default"] = void 0;
77

88
var React = _interopRequireWildcard(require("react"));
99

10-
var _Errorboundary = _interopRequireDefault(
11-
require("./components/Errorboundary")
10+
var _DefaultErrorBoundary = _interopRequireDefault(
11+
require("./components/DefaultErrorBoundary")
1212
);
1313

1414
var _index = require("./util/index");
@@ -164,7 +164,7 @@ var catchreacterror = function catchreacterror() {
164164
var Boundary =
165165
arguments.length > 0 && arguments[0] !== undefined
166166
? arguments[0]
167-
: _Errorboundary["default"];
167+
: _DefaultErrorBoundary["default"];
168168
return function(InnerComponent) {
169169
if (
170170
Boundary &&
@@ -173,14 +173,14 @@ var catchreacterror = function catchreacterror() {
173173
console.warn(
174174
"Catch-React-Error: The <ErrorBoundary /> component doesn't extend React.Component. ErrorBoundary must extends React.Component"
175175
);
176-
Boundary = _Errorboundary["default"];
176+
Boundary = _DefaultErrorBoundary["default"];
177177
}
178178

179179
if (Boundary && !Boundary.prototype.componentDidCatch) {
180180
console.warn(
181181
"Catch-React-Error: The <ErrorBoundary /> component doesn't have componentDidCatch lifecycle. ErrorBoundary must have componentDidCatch lifecycle"
182182
);
183-
Boundary = _Errorboundary["default"];
183+
Boundary = _DefaultErrorBoundary["default"];
184184
}
185185

186186
if ((0, _index.isComponentClass)(InnerComponent)) {

example-client/src/dist/components/Errorboundary.js renamed to example-client/src/dist/components/DefaultErrorBoundary.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ var DefaultErrorBoundary =
194194
{
195195
key: "render",
196196
value: function render() {
197-
// if (is_server()) {
198-
// return serverMarkup(this.props);
199-
// }
200197
if (this.state.hasError) {
201198
return React.createElement("div", null, "Something is Wrong");
202199
}

example-client/src/dist/foo.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

example-client/src/dist/index.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,15 @@ exports["default"] = void 0;
88

99
var _catchReactError = _interopRequireDefault(require("./catch-react-error"));
1010

11-
var _Errorboundary = require("./components/Errorboundary");
11+
var _DefaultErrorBoundary = require("./components/DefaultErrorBoundary");
1212

13-
Object.keys(_Errorboundary).forEach(function(key) {
13+
Object.keys(_DefaultErrorBoundary).forEach(function(key) {
1414
if (key === "default" || key === "__esModule") return;
1515
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
1616
Object.defineProperty(exports, key, {
1717
enumerable: true,
1818
get: function get() {
19-
return _Errorboundary[key];
20-
}
21-
});
22-
});
23-
24-
var _propsInterface = require("./interface/propsInterface");
25-
26-
Object.keys(_propsInterface).forEach(function(key) {
27-
if (key === "default" || key === "__esModule") return;
28-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
29-
Object.defineProperty(exports, key, {
30-
enumerable: true,
31-
get: function get() {
32-
return _propsInterface[key];
19+
return _DefaultErrorBoundary[key];
3320
}
3421
});
3522
});
@@ -38,5 +25,6 @@ function _interopRequireDefault(obj) {
3825
return obj && obj.__esModule ? obj : { default: obj };
3926
}
4027

28+
// export * from './interface/propsInterface'
4129
var _default = _catchReactError["default"];
4230
exports["default"] = _default;

0 commit comments

Comments
 (0)