react中富文本编译器

285 阅读1分钟

安装和怎么实现:

$ npm install --save react-draft-wysiwyg draft-js(安装)

```
import React, { useState } from 'react'
import { convertToRaw } from 'draft-js';
import { Editor } from "react-draft-wysiwyg";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
import draftToHtml from 'draftjs-to-html';
export default function NewsEditor() {
 const [editorState,setEditorState]  =useState()
  return (
    <div>
      <Editor
        editorState={editorState}
        toolbarClassName="toolbarClassName"
        wrapperClassName="wrapperClassName"
        editorClassName="editorClassName"
        onEditorStateChange={(editorState)=>setEditorState(editorState)}
        onBlur={()=>{
          console.log(draftToHtml(convertToRaw(editorState.getCurrentContent())));
        }}(获取值)
      />;
    </div>
  )
}

html->draft
github:https://jpuri.github.io/react-draft-wysiwyg/#/docs
`  useEffect(()=>{
      console.log(props.content);
      const html = props.content;
      const contentBlock = htmlToDraft(html);
      if (contentBlock) {
        const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
        const editorState = EditorState.createWithContent(contentState);
        setEditorState(editorState)
       }
  },[props.content])`