javascriptreactjsnext.jsdraftjs

Document is not defined when I initiate editor with HTML content using convertFromHTML method


I want to initialize my Editor state with a html markup but im getting this error

at renderToString (/home/al/Documents/node/admin-next/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
at render (/home/al/Documents/node/admin-next/node_modules/next-server/dist/server/render.js:86:16)
at renderPage (/home/al/Documents/node/admin-next/node_modules/next-server/dist/server/render.js:211:20)
at Function.value (/home/al/Documents/node/admin-next/.next/server/static/development/pages/_document.js:925:41)
at _callee$ (/home/al/Documents/node/admin-next/.next/server/static/development/pages/_document.js:2334:78)
at tryCatch (/home/al/Documents/node/admin-next/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (/home/al/Documents/node/admin-next/node_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (/home/al/Documents/node/admin-next/node_modules/regenerator-runtime/runtime.js:114:21)
at asyncGeneratorStep (/home/al/Documents/node/admin-next/.next/server/static/development/pages/_document.js:352:24)

This is my code which I copied from this stackoverflow question. It errors when I initialize the editorState with a content.

const blocksFromHTML = convertFromHTML(
    "<p>Hey this <strong>editor</strong> rocks 😀</p>"
  );

  const content = ContentState.createFromBlockArray(
    blocksFromHTML.contentBlocks,
    blocksFromHTML.entityMap
  );

  const [editorState, setEditorState] = useState(
    EditorState.createWithContent(content)
  );

import React, { useState } from "react";
import {
  Editor,
  EditorState,
  RichUtils,
  getDefaultKeyBinding,
  ContentState,
  convertFromHTML
} from "draft-js";
import "./RichTextEditor.css";
import "draft-js/dist/Draft.css";

const MinimumRequirements = () => {
  const blocksFromHTML = convertFromHTML(
    "<p>Hey this <strong>editor</strong> rocks 😀</p>"
  );

  const content = ContentState.createFromBlockArray(
    blocksFromHTML.contentBlocks,
    blocksFromHTML.entityMap
  );

  const [editorState, setEditorState] = useState(
    EditorState.createWithContent(content)
  );

  const onChangeHandler = (editorState) => setEditorState(editorState);

  return (
    <div className="RichEditor-root">
      <Editor
        blockStyleFn={getBlockStyle}
        customStyleMap={styleMap}
        handleKeyCommand={handleKeyCommand}
        keyBindingFn={mapKeyToEditorCommand}
        onChange={onChangeHandler}
        placeholder="Create pc requirements..."
      />
    </div>
  );
};

export default MinimumRequirements;

Still i get this error. Anyone can help me?

Edit: My code works now but i dont know why and i dont have changes on this code? I'm afraid i will get error on production.


Solution

  • See https://github.com/facebook/draft-js/issues/1361:

    convertFromHTML is not expected to be implemented for the server. I copied the information provided in that url to solve this for server side rendering

    The author also includes a solution that involves installing the NPM package jsdom.