From 808514109c121befc2788bacc7a158fe95630e6f Mon Sep 17 00:00:00 2001 From: Matt Krick Date: Mon, 9 Oct 2017 17:07:58 -0700 Subject: [PATCH] Support React v16 when editor is focused after removed from DOM Summary: **Summary** - calling `Style.getScrollParent(editorNode)` with `null` will result in an uncaught error - `Ref#focus()` is often called within a `setTimeout` (even in some example IIRC). `Ref` doesn't get GCd, but the DOM node it points to may no longer exist. Since React v16 is so blazing fast, it's possible that we're trying to focus something that no longer exists. - Without this bit of code, we'd have to call `findDOMNode` on the ref before calling `focus`. That's not great since putting `findDOMNode` is discouraged in application code. Closes https://github.com/facebook/draft-js/pull/1409 Differential Revision: D5976879 fbshipit-source-id: f8f63525fdb6e76a111ea72edae1a620505f3e93 --- src/component/base/DraftEditor.react.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/component/base/DraftEditor.react.js b/src/component/base/DraftEditor.react.js index 41680dc2f9..430ae311d0 100644 --- a/src/component/base/DraftEditor.react.js +++ b/src/component/base/DraftEditor.react.js @@ -368,6 +368,12 @@ class DraftEditor extends React.Component { const alreadyHasFocus = editorState.getSelection().getHasFocus(); const editorNode = ReactDOM.findDOMNode(this.refs.editor); + if (!editorNode) { + // once in a while people call 'focus' in a setTimeout, and the node has + // been deleted, so it can be null in that case. + return; + } + const scrollParent = Style.getScrollParent(editorNode); const {x, y} = scrollPosition || getScrollPosition(scrollParent);