forked from rescript-lang/rescript-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRescriptReactErrorBoundary.res
More file actions
42 lines (36 loc) · 1.19 KB
/
RescriptReactErrorBoundary.res
File metadata and controls
42 lines (36 loc) · 1.19 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
@ocaml.doc("
* 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 reactComponentClass
@bs.module("react") external component: reactComponentClass = "Component"
type info = {componentStack: string}
type params<'error> = {
error: 'error,
info: info,
}
let getErrorBoundary = %bs.raw(`
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;
}
`)
@bs.obj
external makeProps: (
~children: React.element,
~fallback: params<'error> => React.element,
~key: string=?,
unit,
) => {"children": React.element, "fallback": params<'error> => React.element} = ""
let make = getErrorBoundary(component)