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

相关推荐
垆边人似月.5 小时前
Python中如何使用get函数实现频次查询
服务器·前端·python
moshuying5 小时前
DeepSeek开源生产级推理框架DeepInfer:单集群一天处理155万亿token,这意味着什么?
前端
子兮曰6 小时前
PHP漏洞危机,你的网站升级了吗?
前端·后端·php
jerrywus6 小时前
给 Pi 装上生图插件:从登录报错到画出一只 200×200 的猫
前端·agent·claude
陈随易6 小时前
搞钱点子之生态鱼缸
前端·后端·程序员
PBitW7 小时前
从空目录到上线:我全程用 TRAE 做了一款微信小游戏合集(含一次完整架构重构)
前端·trae
不爱学英文的码字机器7 小时前
从Q1迁到T1,同一个编号已经换了含义
前端·数据库·microsoft
云和数据.ChenGuang7 小时前
光哥说Python 循环导入(Circular Import)深度解析:成因、排查与解决方案
开发语言·前端·人工智能·python·langchain
avi91117 小时前
【AI教人】在Inspector显示所有Delegate-Odin Inspector之三
java·前端·算法·delegate·inspector·editor扩展·unity editor
fengci.7 小时前
BUUCTF(困难部分)
android·开发语言·前端·学习·php