|
3 | 3 | * As soon as React provides a mechanism for error-catching using functional component,
|
4 | 4 | * this is likely to be deprecated and/or move to user space.
|
5 | 5 | ")
|
6 |
| -type reactComponentClass |
7 |
| - |
8 |
| -@module("react") external component: reactComponentClass = "Component" |
9 |
| - |
10 | 6 | type info = {componentStack: string}
|
11 | 7 |
|
12 | 8 | type params<'error> = {
|
13 | 9 | error: 'error,
|
14 | 10 | info: info,
|
15 | 11 | }
|
16 | 12 |
|
17 |
| -let getErrorBoundary: reactComponentClass => React.element = %raw(` |
18 |
| - function (Component) { |
19 |
| - function ErrorBoundary(props) { |
20 |
| - Component.call(this); |
21 |
| - this.state = {error: undefined}; |
22 |
| - }; |
23 |
| - ErrorBoundary.prototype = Object.create(Component.prototype); |
24 |
| - ErrorBoundary.prototype.componentDidCatch = function(error, info) { |
25 |
| - this.setState({error: {error: error, info: info}}) |
26 |
| - }; |
27 |
| - ErrorBoundary.prototype.render = function() { |
28 |
| - return this.state.error != undefined ? this.props.fallback(this.state.error) : this.props.children |
29 |
| - } |
30 |
| - return ErrorBoundary; |
| 13 | +%%raw(` |
| 14 | +var React = require("react"); |
| 15 | +
|
| 16 | +var ErrorBoundary = (function (Component) { |
| 17 | + function ErrorBoundary(props) { |
| 18 | + Component.call(this); |
| 19 | + this.state = { error: undefined }; |
31 | 20 | }
|
| 21 | + ErrorBoundary.prototype = Object.create(Component.prototype); |
| 22 | + ErrorBoundary.prototype.componentDidCatch = function (error, info) { |
| 23 | + this.setState({ error: { error: error, info: info } }); |
| 24 | + }; |
| 25 | + ErrorBoundary.prototype.render = function () { |
| 26 | + return this.state.error != undefined |
| 27 | + ? this.props.fallback(this.state.error) |
| 28 | + : this.props.children; |
| 29 | + }; |
| 30 | + return ErrorBoundary; |
| 31 | +})(React.Component); |
32 | 32 | `)
|
33 | 33 |
|
34 |
| -@react.component |
35 |
| -let make = (~children as _: React.element, ~fallback as _: params<'error> => React.element) => |
36 |
| - getErrorBoundary(component) |
| 34 | +@react.component @val |
| 35 | +external make: ( |
| 36 | + ~children: React.element, |
| 37 | + ~fallback: params<'error> => React.element, |
| 38 | +) => React.element = "ErrorBoundary" |
0 commit comments