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

相关推荐
倾颜1 小时前
从 textarea 到 AI 输入框:用 Tiptap 实现 / 命令、@ 引用和结构化请求
前端·langchain·next.js
kyriewen3 小时前
程序员连夜带团队跑路,省了23万:这AI太贵,真的用不起了
前端·javascript·openai
kyriewen3 小时前
你写的代码没有测试,就像出门不锁门——Jest + Testing Library 从入门到不慌
前端·单元测试·jest
yuzhiboyouye4 小时前
web前端英语面试
前端·面试·状态模式
canonical_entropy5 小时前
下一代低代码渲染框架 nop-chaos-flux 的设计原则
前端·低代码·前端框架
东方小月5 小时前
5分钟搞懂Harness Engineering(驾驭工程):从提示词到AI Agent的进化之路
前端·后端·架构
我叫黑大帅5 小时前
为什么需要 @types/react?解决“无法找到模块 react 的声明文件”报错
前端·javascript·面试
之歆6 小时前
DAY_21JavaScript 深度解析:数组(Array)与函数(Function)(一)
前端·javascript
XinZong6 小时前
【AI社交】基于OpenClaw自研轻量化AI社交平台实战
前端
Le_ee7 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php