在react项目中导入富文本编辑器

命令:

npm i react-quill@2.0.0-beta.2 

导入富文本编辑器组件和他的样式文件

import ReactQuill from 'react-quill'
import 'react-quill/dist/quill.snow.css'

在组件当中使用

        <ReactQuill
          className="publish-quill"
          theme="snow"
          placeholder="请输入文章内容"
        />

在视图中查看效果的时候,这个地方会出现两次

是因为react的项目中会有严格模式的导出节点<React.StrictMode> </React.StrictMode>

解决方案,直接把这个严格模式去掉就可以了,效果如下:

支持拖拽 复制 截图 excel ----------------------------------------------------------------------------------------------------------------------------------------------复制代码 /** * Created by zhanglei on 2017/5/23. */ import React, { Component, PropTypes } from 'react'; import { Icon,Modal,message } from 'antd'; import ContentEditable from 'react-contenteditable' import './edit.less' export default class Editor extends Component { static propTypes = { className: PropTypes.string, value:PropTypes.string, editColor:PropTypes.string, }; constructor(props){ super(props); ['inputTextChange','onchangefile','onpaste','ondrop','onParseOrDrop'].map(item=>this[item]=this[item].bind(this)); this.state={ value:null, tableData:[], linkModel:false, visible:false, isColor:false, myDisabled:false, isEdit:true, isFace:false, isBackground:false, linkValue:null, editStatus:[ {label:'加粗',value:'bold',icon:'zitijiacu'}, {label:'斜体',value:'italic',icon:'zitixieti'}, {label:'下划线',value:'underline',icon:'xiahuaxian'}, {label:'链接',value:'createLink',icon:'lianjie'} ], fontSizeData:[ {title:'大号',value:'h1',icon:'H1'}, {title:'号',value:'h2',icon:'h2'}, {title:'正常',value:'h3',icon:'h3-copy-copy'}, {title:'小号',value:'h4',icon:'h4'} ], isSent:true, colorData:[ 'red','orange','yellow','#01FF01','#98F5FF','#8686FF','rgb(216, 154, 255)', '#fff', '#DE1607','#E49402','#E2E205','#04DE04','rgb(71, 237, 255)','#6363F9','rgb(204, 123, 255)', 'rgb(206, 205, 205)', '#C10303','#D08702','#C5C503','#07C307','rgb(0, 221, 245)','#4C4CFB','rgb(184, 70, 255)', 'rgb(183, 183, 183)', '#960505','#AB7005','#ABAB03','#02A902','rgb(6, 171, 189)','#3333FF','rgb(167, 25, 255)', 'rgb(148, 148, 148)', '#710303','#989805','#989805','#059C05','rgb(9, 138, 152)','blue','#A020F0', 'rgb(76, 75, 75)', '#5D0404',' #757504','#757504','green','rgb(2, 99, 109)','blue','#A020F0', '#000','rgb(56, 2, 2)' ], } }; componentDidMount(){ document.addEventListener('click',this.documentClick); }; componentWillReceiveProps(nextProps){ if('value' in nextProps&&this;.state.editValue !== nextProps.value){ this.setState({editValue:nextProps.value}) } } //全局取消隐藏颜色框 documentClick=(e)=>{ const {isColor,isBackground} = this.state; if(isColor||isBackground){ let en = e.srcElement||e.target; const name = '.color-content'; while(en){ if(en.className&&en;.className === name.replace('.','')){ return; } en = en[removed]; } this.setState({isColor:false,isBackground:false}); } }; //卸载颜色框 componentWillUnmount(){ document.removeEventListener('click',this.documentClick) } /* * <粘贴功能> * @param onParseOrDrop 通用方法
当在React应用使用百度富文本编辑器(UEditor)时,遇到`Cannot read properties of undefined (reading 'ready')`错误通常是由于组件未正确初始化或组件的状态管理有问题。这个错误提示表明在尝试访问`ready`属性之前,编辑器实例可能还没有完成初始化。 解决这个问题需要检查以下几个步骤: 1. **确保安装和引入**:确认已经正确安装了`ueditor`库,并在组件的JSX文件导入它。例如: ```jsx import React from 'react'; import EEditor from '@baidublockly/components/ueditor/instance'; // 或者具体的ueditor版本 ``` 2. **初始化编辑器**:在组件的生命周期方法(如`componentDidMount`)初始化UEditor,并给编辑器一个id以便后续引用: ```jsx class MyComponent extends React.Component { componentDidMount() { const ue = new EEditor({ container: this.editorRef.current, // 确保editorRef已经被赋值 initialContent: '', // 初始内容 ...其他配置项 }); ue.ready(() => { // 当编辑器准备好后可以在这里执行操作 }); } render() { return ( <div ref={this.editorRef}> <textarea ref={(ref) => { this.editorRef = ref; }} /> </div> ); } } ``` 3. **处理状态**:确保你在编辑器实例可用前没有依赖它的状态。如果编辑器的状态用于控制组件的行为,可以在`ready`回调更新组件的状态。 4. **检查引用**:确认`editorRef`是否正确引用到元素,如果`this.editorRef.current`在`ready`回调里是undefined,可能是`editorRef`在渲染时还没绑定上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值