From e080f2e534bb48439e828f02c50efebc929c2343 Mon Sep 17 00:00:00 2001 From: Gilad Gray Date: Thu, 13 Dec 2018 14:10:11 -0800 Subject: [PATCH] [ResizeSensor] try/catch findDOMNode to handle possible error state --- .../components/resize-sensor/resizeSensor.tsx | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/core/src/components/resize-sensor/resizeSensor.tsx b/packages/core/src/components/resize-sensor/resizeSensor.tsx index 04f4544d5cd..864b4e1bce2 100644 --- a/packages/core/src/components/resize-sensor/resizeSensor.tsx +++ b/packages/core/src/components/resize-sensor/resizeSensor.tsx @@ -59,14 +59,11 @@ export class ResizeSensor extends React.PureComponent { } public componentDidMount() { - // using findDOMNode for two reasons: - // 1. cloning to insert a ref is unwieldy and not performant. - // 2. ensure that we get an actual DOM node for observing. - this.observeElement(findDOMNode(this)); + this.observeElement(); } public componentDidUpdate(prevProps: IResizeSensorProps) { - this.observeElement(findDOMNode(this), this.props.observeParents !== prevProps.observeParents); + this.observeElement(this.props.observeParents !== prevProps.observeParents); } public componentWillUnmount() { @@ -74,11 +71,12 @@ export class ResizeSensor extends React.PureComponent { } /** - * Observe the given element, if defined and different from the currently + * Observe the DOM element, if defined and different from the currently * observed element. Pass `force` argument to skip element checks and always * re-observe. */ - private observeElement(element: Element | Text | null, force = false) { + private observeElement(force = false) { + const element = this.getElement(); if (!(element instanceof Element)) { // stop everything if not defined this.observer.disconnect(); @@ -106,4 +104,16 @@ export class ResizeSensor extends React.PureComponent { } } } + + private getElement() { + try { + // using findDOMNode for two reasons: + // 1. cloning to insert a ref is unwieldy and not performant. + // 2. ensure that we resolve to an actual DOM node (instead of any JSX ref instance). + return findDOMNode(this); + } catch { + // swallow error if findDOMNode is run on unmounted component. + return null; + } + } }