'Rich text editor in react js

I have create rich text editor in react and it is working fine too but i want to pass this value in other component and store it in state, can anyone suggest me the right way. This is rich text content component, which is working fine.

import React, { useState } from "react";
import { EditorState } from "draft-js";
import { Editor } from "react-draft-wysiwyg";
import { convertToHTML } from "draft-convert";
import DOMPurify from "dompurify";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
import style from "./RichText.module.css";
const App = () => {
  const [editorState, setEditorState] = useState(() =>
    EditorState.createEmpty()
  );
  const [convertedContent, setConvertedContent] = useState(null);
  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={style.App}>
      <Editor
        editorState={editorState}
        onEditorStateChange={handleEditorChange}
        wrapperClassName={style.wrapperClass}
        editorClassName={style.editorClass}
        toolbarClassName={style.toolbarClass}
      />
      <div
        className={style.preview}
        dangerouslySetInnerHTML={createMarkup(convertedContent)}
      ></div>
    </div>
  );
};
export default App;

and in other component i want to get data in setPostBody useState -

const [postBody, setpostBody] = useState("");
<RichText />

link - https://codesandbox.io/s/compassionate-gauss-pq2wr?file=/src/App.js



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source