From 29bcb1c8d6b23eaf1eb00cdf09e984f00a3ea959 Mon Sep 17 00:00:00 2001 From: Charlie Martin Date: Fri, 29 Dec 2017 13:49:54 -0500 Subject: [PATCH] Don't attempt to remove event listeners from non-existent window (#1495) ``` ``` Since react unmounts from top down, `IFrameComponent` will unmount first (destroying its window), then `SlateEditor` will unmount and attempt to access the iframe window (which no longer exists) throwing `Uncaught TypeError: Cannot read property 'document' of undefined`. It should be safe to skip removing event listeners from windows that no longer exist since they will be garbage collected upon destruction of the window anyway --- packages/slate-react/src/components/content.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/slate-react/src/components/content.js b/packages/slate-react/src/components/content.js index 6f7af34c4..261d4cc9b 100644 --- a/packages/slate-react/src/components/content.js +++ b/packages/slate-react/src/components/content.js @@ -117,7 +117,9 @@ class Content extends React.Component { componentWillUnmount() { const window = getWindow(this.element) - window.document.removeEventListener('selectionchange', this.onNativeSelectionChange) + if (window) { + window.document.removeEventListener('selectionchange', this.onNativeSelectionChange) + } // COMPAT: Restrict scope of `beforeinput` to mobile. if ((IS_IOS || IS_ANDROID) && SUPPORTED_EVENTS.beforeinput) {