Skip to content

Commit c59016d

Browse files
committed
fix ErrorBoundary comp by external binding
This reverts commit 8704f78.
1 parent 0f74265 commit c59016d

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ _build
1717

1818
# Editor
1919
/.idea/
20+
21+
# React
22+
!/src/react/*.js

src/RescriptReactErrorBoundary.res

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,15 @@
33
* As soon as React provides a mechanism for error-catching using functional component,
44
* this is likely to be deprecated and/or move to user space.
55
")
6-
type reactComponentClass
7-
8-
@module("react") external component: reactComponentClass = "Component"
9-
106
type info = {componentStack: string}
117

128
type params<'error> = {
139
error: 'error,
1410
info: info,
1511
}
1612

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;
31-
}
32-
`)
33-
34-
@react.component
35-
let make = (~children as _: React.element, ~fallback as _: params<'error> => React.element) =>
36-
getErrorBoundary(component)
13+
@react.component @module("./react/ErrorBoundary.js")
14+
external make: (
15+
~children: React.element,
16+
~fallback: params<'error> => React.element,
17+
) => React.element = "default"

src/react/ErrorBoundary.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var React = require("react");
2+
3+
export default (function (Component) {
4+
function ErrorBoundary(props) {
5+
Component.call(this);
6+
this.state = { error: undefined };
7+
}
8+
ErrorBoundary.prototype = Object.create(Component.prototype);
9+
ErrorBoundary.prototype.componentDidCatch = function (error, info) {
10+
this.setState({ error: { error: error, info: info } });
11+
};
12+
ErrorBoundary.prototype.render = function () {
13+
return this.state.error != undefined
14+
? this.props.fallback(this.state.error)
15+
: this.props.children;
16+
};
17+
return ErrorBoundary;
18+
})(React.Component);

0 commit comments

Comments
 (0)