import React, { useState, useEffect } from "react"; import "@wangeditor/editor/dist/css/style.css"; import { Editor, Toolbar } from "@wangeditor/editor-for-react"; function MyEditor(props) { const { value, onChange = (e) => { setHtml(e); }, toolbarConfig = { excludeKeys: [ "insertImage", "viewImageLink", "editImage", "uploadImage", "uploadVideo", ], }, editorConfig = { placeholder: "请输入内容...", }, readonly = false, } = props; const [editor, setEditor] = useState(null); // 存储 editor 实例 const [html, setHtml] = useState(""); console.log(props, "MyEditor"); // 模拟 ajax 请求,异步设置 html useEffect(() => { console.log(editor ); setHtml(value); }, [value]); // 及时销毁 editor useEffect(() => { return () => { if (editor == null) return; editor.destroy(); setEditor(null); }; }, [editor]); function insertText() { if (editor == null) return; editor.insertText(" hello "); } function printHtml() { if (editor == null) return; console.log(editor.getHtml()); } return !readonly ? ( <>
onChange(editor.getHtml())} mode="default" style={{ height: "500px" }} />
) : (
); } export default MyEditor;