react-quill的使用

本文详细介绍如何在React项目中安装并配置React Quill富文本编辑器,包括自定义上传图片功能和设置富文本样式。通过具体代码示例,帮助读者快速掌握React Quill的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装
npm i react-quill
引入
import ReactQuill from 'react-quill'; 
import 'quill/dist/quill.snow.css';
state里面写配置 这是我的配置 也可以根据官方配置,这里的图片我用的是自定义上传再插入,特别注意:要改变this指向
this.state={
			text:"",
            modules: {
                toolbar: {
                  container: [
                    [{ header: [1, 2,3, 4, 5, 6, false] }],
                    ['bold', 'italic', 'underline', 'strike', ],
                    [{ list: 'ordered' }, { list: 'bullet' }],
                    [{ 'align': [] }],
                    [{ 'color': [] }, { 'background': [] }],
                    ['link', 'image'],
                    ['clean']
                                  
                  ],
                  //如果不自定义上传下面的可以不要
                  handlers: {
                        image() {
                            imageHandler.call(this, props.action);
                        },
                    }
                },
            },//富文本配置
            
  }
  this.handleChange = this.handleChange.bind(this)
图片上传 message是antd组件 这里我把函数定义到 class 上边了全局函数,如果不写自定义上传图片,他会自动转换成base64 ,
function imageHandler(action){
    const input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');
    input.click();
        input.onchange = () => {
            const file = input.files[0];
            const fd = new FormData();
            fd.append('file', file);
            const hide = message.loading('上传中...', 0);
            //这里写自己的接口
            axios({
                    method: 'post',
                    url: '',
                    headers:{'Content-Type': 'multipart/form-data'},
                    data:fd
                }).then((res) => {
                    const range = this.quill.getSelection();
                    this.quill.insertEmbed(range.index, 'image', res."接口返回的地址");
                    let index = range ? range.index : 0;
                    this.quill.setSelection(index + 1);//光标位置加1
                    hide();
                })
        }
    }
获取富文本内容,这里的this指向富文本不指向class ,要在构造器里改变this指向
 //富文本内容
    handleChange(value) {
        console.log(value);
        this.setState({
            text:value
        })
    }
组件,一定要加上这个class名字,否则空格不回显(className=“ql-editor”)
  <ReactQuill
       theme="snow"
        value={ this.state.text }
        onChange={this.handleChange}
        modules={modules}
        className=" ql-editor"
    />
展示页面也要加上 这个 class名字
<div dangerouslySetInnerHTML={{__html: this.state.text}} className="ql-editor"></div>
### React-Quill 安装 对于React-Quill的安装,在命令行工具中执行如下npm指令来添加依赖到项目里[^2]: ```bash npm install react-quill --save ``` 或者如果更倾向于使用Yarn作为包管理器,则可运行: ```bash yarn add react-quill ``` ### 使用教程 为了能够顺利地在React应用中集成并运用此组件,需先引入`react-quill`模块以及必要的样式文件。下面是一个简单的例子展示如何创建一个基于React-Quill的基础编辑器实例。 ```jsx import React, { useState } from 'react'; import ReactQuill from 'react-quill'; import 'react-quill/dist/quill.snow.css'; function Editor() { const [value, setValue] = useState(''); return ( <div> <h1>Simple Rich Text Editor</h1> <ReactQuill theme="snow" value={value} onChange={setValue} /> </div> ); } export default Editor; ``` 这段代码展示了怎样通过状态变量控制输入的内容,并将其绑定至编辑区域上显示出来。同时设置了主题为`snow`,这是默认的主题之一,提供了简洁美观的操作界面。 ### 示例 除了上述基础功能外,还可以自定义工具栏按钮、设置只读模式或是监听各种事件等高级特性。这里给出一段更加复杂的配置样例,其中包含了更多选项供开发者探索和尝试。 ```jsx <ReactQuill modules={{ toolbar: [ [{ header: [1, 2, false] }], ['bold', 'italic', 'underline'], [{'list': 'ordered'}, {'list': 'bullet'}], ['link'] ] }} formats={['header', 'bold', 'italic', 'underline', 'list']} /> ``` 以上片段说明了如何定制化工具条上的可用操作项及其对应的格式列表。这使得可以根据实际需求灵活调整编辑器的功能集。 ### 问题解决 当遇到与React-Quill有关的问题时,建议首先查阅官方文档获取帮助和支持;其次可以通过搜索引擎查询相似案例的学习经验分享文章;最后考虑向社区寻求协助,比如Stack Overflow论坛上有许多关于该库的技术讨论帖子可供参考学习[^3]。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值