From 65ae4b7c237b57a85656786e936c9dfd1ed1eab0 Mon Sep 17 00:00:00 2001 From: Woonki Moon Date: Sun, 24 Jul 2022 17:42:43 +0900 Subject: [PATCH] fix ErrorBoundary runtime error This reverts commit 8704f7850ae6e4844e5380f524ebc2d51acabcd3. --- .gitignore | 3 ++ src/RescriptReactErrorBoundary.res | 44 ++++++++++++++++-------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index dbba277..78129ca 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ _build # Editor /.idea/ + +# React +!/src/react/*.js diff --git a/src/RescriptReactErrorBoundary.res b/src/RescriptReactErrorBoundary.res index af733e0..7f7019c 100644 --- a/src/RescriptReactErrorBoundary.res +++ b/src/RescriptReactErrorBoundary.res @@ -3,10 +3,6 @@ * 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 - -@module("react") external component: reactComponentClass = "Component" - type info = {componentStack: string} type params<'error> = { @@ -14,23 +10,29 @@ type params<'error> = { info: info, } -let getErrorBoundary: reactComponentClass => React.element = %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; +%%raw(` +var React = require("react"); + +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; +})(React.Component); `) -@react.component -let make = (~children as _: React.element, ~fallback as _: params<'error> => React.element) => - getErrorBoundary(component) +@react.component @val +external make: ( + ~children: React.element, + ~fallback: params<'error> => React.element, +) => React.element = "ErrorBoundary"