| 1 | import {BabelClass, BabelClassWithFields} from './BabelClasses-compiled.js'; |
| 2 | import { |
| 3 | Throw, |
| 4 | Component, |
| 5 | DisplayName, |
| 6 | NativeClass, |
| 7 | FrozenClass, |
| 8 | } from './Components.js'; |
| 9 | |
| 10 | const x = React.createElement; |
| 11 | |
| 12 | class ErrorBoundary extends React.Component { |
| 13 | static getDerivedStateFromError(error) { |
| 14 | return { |
| 15 | error: error, |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | componentDidCatch(error, errorInfo) { |
| 20 | console.log(error.message, errorInfo.componentStack); |
| 21 | this.setState({ |
| 22 | componentStack: errorInfo.componentStack, |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | render() { |
| 27 | if (this.state && this.state.error) { |
| 28 | return x( |
| 29 | 'div', |
| 30 | null, |
| 31 | x('h3', null, this.state.error.message), |
| 32 | x('pre', null, this.state.componentStack) |
| 33 | ); |
| 34 | } |
| 35 | return this.props.children; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export default function Example() { |
| 40 | let state = React.useState(false); |
| 41 | return x( |
| 42 | ErrorBoundary, |
| 43 | null, |
| 44 | x( |
| 45 | DisplayName, |
| 46 | null, |
| 47 | x( |
| 48 | NativeClass, |
| 49 | null, |
| 50 | x( |
| 51 | FrozenClass, |
| 52 | null, |
| 53 | x( |
| 54 | BabelClass, |
| 55 | null, |
| 56 | x( |
| 57 | BabelClassWithFields, |
| 58 | null, |
| 59 | x( |
| 60 | React.Suspense, |
| 61 | null, |
| 62 | x('div', null, x(Component, null, x(Throw))) |
| 63 | ) |
| 64 | ) |
| 65 | ) |
| 66 | ) |
| 67 | ) |
| 68 | ) |
| 69 | ); |
| 70 | } |