Search code examples
reactjstypescriptnext.jsdraftjsuse-ref

What ref tag do I use for draft.js?


I'm using Draft.js for a form I'm building on Next.js with TS, and I'm running into some errors. Here is my code:

import {Editor, EditorState} from 'draft-js';
const [editorState, setEditorState] = useState(
    EditorState.createEmpty()
  );
 
  const editor = useRef<**WhatDoIUseHere**>(null);
 
  function focusEditor() {
    editor.current.focus();
  }
 
  useEffect(() => {
    focusEditor()
  }, []);

return (
        <div onClick={focusEditor}>
      <Editor
        ref={editor}
        editorState={editorState}
        onChange={editorState => setEditorState(editorState)}
      />
    </div>
)

What "tag" do I use for the 6th line? const editor... (null);

Thank you!


Solution

  • According to the source code:

    1. It is a div element, so you should use HTMLDivElement.
    const editor = useRef<HTMLDivElement>(null);
    
    1. You should use editorRef, not ref.
    <Editor
      editorRef={editor}
      // ...
    />