Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Support React v16 when editor is focused after removed from DOM
Browse files Browse the repository at this point in the history
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 #1409

Differential Revision: D5976879

fbshipit-source-id: f8f63525fdb6e76a111ea72edae1a620505f3e93
  • Loading branch information
mattkrick authored and facebook-github-bot committed Oct 10, 2017
1 parent 05881dc commit 8085141
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/component/base/DraftEditor.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
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);

Expand Down

0 comments on commit 8085141

Please sign in to comment.