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作为用于富文本编辑器的值发生变化后向父组件暴漏出来的当前最新的值,父组件使用高阶函数来获取编辑器最新的值。

相关推荐
Lkstar14 小时前
ES6+ 必备特性复习:解构、展开运算符、Symbol、Proxy
javascript·面试
半兽先生14 小时前
vue高性能下拉组件 支持上万数据不卡顿
前端·javascript·vue.js
invicinble15 小时前
前端框架使用vue-cli( 第二层:工程配置层--路由页面配置)
javascript·vue.js·前端框架
懂懂tty15 小时前
Vue3 架构
前端·vue.js
四岁爱上了她15 小时前
自定义标签切换动画
javascript·css·css3
invicinble15 小时前
前端框架使用vue-cli( 第二层:工程配置层--总览)
前端·vue.js·前端框架
哆啦A梦158815 小时前
01, 前端vue3框架的快速搭建以及项目工程的讲解
前端·vue3·springboot
坤盾科技15 小时前
Docker 离线地图服务器搭建实战:Node.js + OpenLayers + MBTiles
linux·javascript·arcgis·docker·node.js
念你那丝微笑15 小时前
2026年Vue前端面试准备
前端·vue.js·面试
冴羽yayujs15 小时前
GitHub 前端热榜项目 - 日榜(2026-05-09)
前端·github