Skip to content

Commit 46944b4

Browse files
authored
Merge pull request #53 from rescript-lang/fix-error-boundary
Fix ErrorBoundary component using external binding
2 parents 9da77c3 + 65ae4b7 commit 46944b4

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
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: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@
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;
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 };
3120
}
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);
3232
`)
3333

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

Comments
 (0)