Switching between read only modes causes error: Cannot read property 'focus' of null
We use the editor in both read only and edit mode mode. This switch happens by changing the state using a onClick handler on the parent div.
However when going from read only mode to edit mode, the editor is not automatically focussed and the console logs the error: "Cannot read property 'focus' of null at react-draft-wysiwyg.js:6". It seems like this.editor is null at the moment of focussing, but I have no clue why this is the case.
Is this a problem of this component, Draft.js or are we using it wrong?
How can it be read only mode?
Via the the prop readOnly which is passed down to the Draft Editor https://draftjs.org/docs/api-reference-editor.html#readonly according to the docs
Well this is pretty old but this happens for me when the editor is removed from the dom, doesn't matter whether I use readonly or not:
{ !isDescriptionWindowOpen && <Editor ..../> }
The same for me. Has anyone found a solution?
Was chasing this bug as well today, but under a different circumstance. We want to use the editor in an uncontrolled mode via defaultEditorState, but we also have a button outside of the Editor where we can add some text (placeholders) to the editor. To achieve this, we use a key prop (see here if you are unfamiliar with this pattern: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key)
Unmounting the editor by giving it a new key results in this error. It seems similar to the scenario described here: https://github.com/jpuri/react-draft-wysiwyg/issues/712#issuecomment-490843899
Judging from the code, focus is called on the unmonted component instance, because it is done in a setTimeout, see: https://github.com/jpuri/react-draft-wysiwyg/blob/4d0aaf6b6bb28b7c04a46cef384c2594d26daacc/src/Editor/index.js#L348-L352
the focus method of the underlying draftJs editor has a guard, but the react-draft-wysiwyg wrapper has not. They even put this use-case in a comment, see here: https://github.com/facebook/draft-js/blob/6e87246506ca59060cb4c6ebc7a9ac3e97609a2a/src/component/base/DraftEditor.react.js#L494-L498
So I think the easiest "fix" / "workaround" would be to add an if (this.editor) around the focus call - relying on refs in a setTimeout seems a bit brittle anyways. However, then in my case, the editor was not focused. But focusing the editor manually if you click on it, well, that's easy enough to achieve 🤷 .
Happy to do a PR with this, too.
There is a PR https://github.com/jpuri/react-draft-wysiwyg/pull/981 to fix this but seem like it hasn't been reviewed and merged for a long time. This error does not break anything but it's annoying especially when we use error tracking tool, it's captured a lot My workaround for now. // useUnmount is a hook from ahooks lib, run before component is removed Add editorRef.current.focusEditor() into a setTimeout and clear it when this component is re-rendered (return function) OR removed (useUnmount hook above)
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css"
import "./style.less"
import { useUnmount } from "ahooks"
import { ContentState, convertFromRaw, convertToRaw, EditorState } from "draft-js"
import { draftToMarkdown, markdownToDraft } from "markdown-draft-js"
import React, { useEffect, useRef, useState } from "react"
import { Editor as DraftEditor, EditorProps } from "react-draft-wysiwyg"
export const MyEditor = (...) => {
const editorRef = useRef(null)
const setFocusTimeoutId = useRef(null)
const [editorState, setEditorState] = useState(
defaultValue
? EditorState.createWithContent(convertFromRaw(markdownToDraft(defaultValue)))
: EditorState.createEmpty()
)
useUnmount(() => {
if (setFocusTimeoutId?.current) {
clearTimeout(setFocusTimeoutId.current)
}
})
useEffect(() => {
// Editor react-draft-wysiwy having this issue https://github.com/jpuri/react-draft-wysiwyg/issues/712
// Because focusEditor is wrapped by a setTimeout https://github.com/jpuri/react-draft-wysiwyg/blob/master/src/Editor/index.js#L348
// Event our DesktopEditor component is removed but .focus in Editor react-draft-wysiwy still be executed -> error occur
// This error does not break anything but it's annoying in rollbar
// There is a PR created https://github.com/jpuri/react-draft-wysiwyg/pull/981, this is we can remove this adhoc way
// Our solution:
// Add editorRef.current.focusEditor() into a setTimeout and clear it when this component is re-rendered (return function) OR removed (useUnmount hook above)
setFocusTimeoutId.current = setTimeout(() => {
if (editorRef.current) {
editorRef.current.focusEditor()
}
})
return () => {
if (setFocusTimeoutId?.current) {
clearTimeout(setFocusTimeoutId.current)
}
}
})
.....
return (
<div className={`editor__wrapper ${!visible && "editor__wrapper__hidden"}`}>
<div className="editor--input">
<DraftEditor
ref={editorRef}
....
</div>
</div>
)
}