Open History to see the last commit
1import React from "react";
2β
3import Button from "./Button";
4β
5export default class ErrorBoundary extends React.Component {
6 state = { error: null };
7 static getDerivedStateFromError(error) {
8 return { error };
9 }
10 componentDidCatch() {
11 // log the error to the server
12 }
13 tryAgain = () => this.setState({ error: null });
14 render() {
15 return this.state.error ? (
16 <div>
17 There was an error. <Button onClick={this.tryAgain}>Try again</Button>
18 <pre style={{ whiteSpace: "normal" }}>{this.state.error.message}</pre>
19 </div>
20 ) : (
21 this.props.children
22 );
23 }
24}