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

相关推荐
竹林8183 分钟前
OmniGame 技术白皮书:从零依赖到 WebRTC P2P,重新定义网页小游戏的工程上限
前端·浏览器
ss2733 分钟前
AI全栈实战 | 2.1-02 CSS 布局:垂直居中为什么是经典面试题?所有布局在解决什么矛盾
前端·css
律宏阔4 分钟前
Flutter Riverpod 3:统一打印 Provider 错误与自动重试日志
前端·flutter
周洲083010 分钟前
STM32片内Flash读写深度详解|掉电参数保存、底层原理
linux·前端·stm32
data analyse 45624 分钟前
埋点工具的私有化部署成本高吗?
前端·数据分析·github
data analyse 4561 小时前
埋点工具的埋点查询语言难学吗?
前端·数据分析
DianSan_ERP1 小时前
多平台订单自动下载与回传的技术实现:从消息推送到状态闭环引言
java·linux·服务器·前端·网络·架构·自动化
恋猫de小郭1 小时前
Shopify 回应为什么从 RN 回到原生,为什么不用 KMP ?
android·前端·flutter
小蜗 strong2 小时前
和电脑猜拳(随机程序应用)
服务器·前端·python
下家3 小时前
为了脱离前端鄙视链,于是自己写个框架 - React 党看完沉默了
前端·前端框架