-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathRescriptReactErrorBoundary.res
More file actions
44 lines (39 loc) · 1.28 KB
/
RescriptReactErrorBoundary.res
File metadata and controls
44 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/***
* Important note on this module:
* As soon as React provides a mechanism for error-catching using functional component,
* this is likely to be deprecated and/or move to user space.
*/
type info = {componentStack: string}
type params<'error> = {
error: 'error,
info: info,
}
type reactComponentClass
@module("react") external component: reactComponentClass = "Component"
let noOp: reactComponentClass => unit = %raw(`function (_x) {}`)
let reactComponentClass = component
// this is so that the compiler doesn't optimize away the previous line
noOp(reactComponentClass)
%%raw(`
var ErrorBoundary = (function (Component) {
function ErrorBoundary(props) {
Component.call(this);
this.state = { error: undefined };
}
ErrorBoundary.prototype = Object.create(Component.prototype);
ErrorBoundary.prototype.componentDidCatch = function (error, info) {
this.setState({ error: { error: error, info: info } });
};
ErrorBoundary.prototype.render = function () {
return this.state.error != undefined
? this.props.fallback(this.state.error)
: this.props.children;
};
return ErrorBoundary;
})(reactComponentClass);
`)
@react.component @val
external make: (
~children: React.element,
~fallback: params<'error> => React.element,
) => React.element = "ErrorBoundary"