-
Notifications
You must be signed in to change notification settings - Fork 696
/
22-error-boundaries.html
54 lines (50 loc) · 1.58 KB
/
22-error-boundaries.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<body>
<div id="root"></div>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.js"></script>
<script src="https://unpkg.com/[email protected]/dist/umd/react-error-boundary.js"></script>
<script type="text/babel">
const ErrorBoundary = ReactErrorBoundary.ErrorBoundary
// class ErrorBoundary extends React.Component {
// state = {error: null}
// static getDerivedStateFromError(error) {
// return {error}
// }
// render() {
// const {error} = this.state
// if (error) {
// return <this.props.FallbackComponent error={error} />
// }
// return this.props.children
// }
// }
function ErrorFallback({error}) {
return (
<div>
<p>Something went wrong:</p>
<pre>{error.message}</pre>
</div>
)
}
function Bomb() {
throw new Error('💥 CABOOM 💥')
}
function App() {
const [explode, setExplode] = React.useState(false)
return (
<div>
<div>
<button onClick={() => setExplode(true)}>💣</button>
</div>
<div>
<ErrorBoundary FallbackComponent={ErrorFallback}>
{explode ? <Bomb /> : 'Push the button Max!'}
</ErrorBoundary>
</div>
</div>
)
}
ReactDOM.render(<App />, document.getElementById('root'))
</script>
</body>