|
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 | + |
4 | 4 | import "./index.css"; |
5 | 5 |
|
6 | 6 | function App() { |
| 7 | + const [count, setCount] = useState(0); |
| 8 | + const [content, setContent] = useState([]); |
7 | 9 | return ( |
8 | 10 | <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> |
14 | 38 | </div> |
15 | 39 | ); |
16 | 40 | } |
17 | 41 |
|
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"); |
41 | 45 | } |
| 46 | + return <h1>{count}</h1>; |
42 | 47 | } |
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); |
64 | 48 |
|
65 | | -const Foo = (a, b, c) => { |
66 | | - return <p>Foo</p>; |
67 | | -}; |
| 49 | +const SaleCount = catchreacterror()(Count); |
68 | 50 |
|
69 | 51 | export default App; |
0 commit comments