From 977ce2e42fdcc04a610fdabfae6e58b27c45e4a3 Mon Sep 17 00:00:00 2001 From: Tomas Milar Date: Tue, 2 Feb 2021 19:25:27 -0300 Subject: [PATCH] fix: properly handle render errors in 'react' driver. --- src/drivers/react.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/drivers/react.js b/src/drivers/react.js index 774cbf6c..7e0941c9 100644 --- a/src/drivers/react.js +++ b/src/drivers/react.js @@ -31,6 +31,21 @@ type ReactLibraryType = {| ReactDOM : ReactDomType |}; + +/** + * Util to check if component is currently mounted + */ +function isMounted(component, ReactDOM) { + try { + return !!ReactDOM.findDOMNode(component); + } + catch (error) { + // Error: Unable to find node on an unmounted component + return false; + } +} + + export const react : ComponentDriverType<*, ReactLibraryType, typeof ReactClassType> = { register: (tag, propsDef, init, { React, ReactDOM }) => { @@ -45,7 +60,18 @@ export const react : ComponentDriverType<*, ReactLibraryType, typeof ReactClassT // $FlowFixMe const el = ReactDOM.findDOMNode(this); const parent = init(extend({}, this.props)); - parent.render(el, CONTEXT.IFRAME); + parent.render(el, CONTEXT.IFRAME) + .catch(error => { + // component failed to render, possibly because it was closed or destroyed. + if(!isMounted(this, ReactDOM)) { + // not mounted anymore, we can safely ignore the error + return + } + // still mounted, throw error inside react to allow a parent component or ErrorBoundary to handle it + this.setState(() => { + throw error + }) + }); this.setState({ parent }); }