Deepak Kumar
Deepak Kumar

Reputation: 1

TypeError: editorState.getCurrentContent is not a function while using Draft.js Editor

I am trying to add react-draft-wysiwyg editor. I am trying to get the current content of the editor but it's showing error as getCurrentContent() is not a function. Can anyone help please?

import React, { useState } from 'react';

import { Editor } from "react-draft-wysiwyg";
import { EditorState } from "draft-js";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
import { convertToHTML } from 'draft-convert';
import DOMPurify from 'dompurify';


const NoteEditor = () => {

    const [editorState, setEditorState] = useState(
        () => EditorState.createEmpty(),
    );
    const [convertedContent, setConvertedContent] = useState("");

    const handleEditorChange = (state) => {
        setEditorState(state);
        convertContentToHTML();
      }
    const convertContentToHTML = () => {
        let currentContentAsHTML = convertToHTML(editorState.getCurrentContent());
        setConvertedContent(currentContentAsHTML);
    }
    const createMarkup = (html) => {
        return  {
          __html: DOMPurify.sanitize(html)
        }
      }
      
    return(
        <div className="note-editor">
            <Editor 
                defaultEditorState={editorState}
                onChange={handleEditorChange}
                wrapperClassName="wrapper-class"
                editorClassName="editor-class"
                toolbarClassName="toolbar-class"
            />
            <div className="preview" dangerouslySetInnerHTML={createMarkup(convertedContent)}></div>
        </div>
    )
};

export default NoteEditor;

Upvotes: 0

Views: 1478

Answers (1)

Ashish Kamble
Ashish Kamble

Reputation: 2627

you can use like this, sometime useState took time as its async,

const handleEditorChange = (state) => {
    setEditorState(state);
    convertContentToHTML(state);
  }
const convertContentToHTML = (editorState) => {
    let currentContentAsHTML = convertToHTML(editorState.getCurrentContent());
    setConvertedContent(currentContentAsHTML);
}

or do useEffect with editorState,

useEffect(()=>{},[editorState])

Upvotes: 1

Related Questions