React富文本编辑器wangEditor

1.安装

npm install @wangeditor/editor-for-react --save

或者

yarn add @wangeditor/editor-for-react

2.使用案例

组件内部分:

javascript 复制代码
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import React, { useState, useEffect } from 'react'
import { Editor, Toolbar } from '@wangeditor/editor-for-react'

function MyEditor({defaultHtml,updateHtml}) {
    const [editor, setEditor] = useState(null) // 存储 editor 实例
    const [html, setHtml] = useState(defaultHtml) // 编辑器内容

    useEffect(() => {
			updateHtml(html)
    }, [html])
	
    const toolbarConfig = {}
    const editorConfig = {
        placeholder: '请输入内容...',
    }

    // 及时销毁 editor ,重要!
    useEffect(() => {
        return () => {
            if (editor == null) return
            editor.destroy()
            setEditor(null)
        }
    }, [editor])

    return (
        <>
            <div style={{ border: '1px solid #ccc', zIndex: 100,height:'350px',padding:'1vh 0', }}>
                <Toolbar
                    editor={editor}
                    defaultConfig={toolbarConfig}
                    mode="default"
                    style={{ borderBottom: '1px solid #ccc' }}
                />
                <Editor
                    defaultConfig={editorConfig}
                    value={html}
                    onCreated={setEditor}
                    onChange={editor => setHtml(editor.getHtml())}
                    mode="default"
                    style={{ height: '50%', 'overflow-y': 'hidden' }}
                />
            </div>
        </>
    )
}

export default MyEditor;

(1)传递的一个dufaultHTML作为富文本编辑器的默认值,供编辑的场景使用。

(2)updateHTML作为用于富文本编辑器的值发生变化后向父组件暴漏出来的当前最新的值,父组件使用高阶函数来获取编辑器最新的值。

相关推荐
kyriewen2 小时前
Anthropic 估值逼近万亿美元,Claude Sonnet 5 + Claude Science 一天两连发
前端·ai编程·claude
小徐_23334 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
山河木马5 小时前
矩阵专题3-怎么创建投影矩阵(uProjectionMatrix)
javascript·webgl·计算机图形学
天蓝色的鱼鱼6 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷7 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花7 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷7 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜7 小时前
Spring Boot 核心知识点总结
前端
lichenyang4537 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端