Nextjs 集成富文本编辑器react-quill

目录

一、组件代码

二、参考文档


由于Next与react有些差别,直接调用组件会报无法找到文档的错误,于是我们只有考虑动态导入了解决问题。因为富文本编辑器一般作用与form页面对SEO意义不大,所以这里可以考虑暂时关闭SSR。

一、组件代码

javascript 复制代码
/**
 * @author Dragon Wu
 * @since 2024/6/11 14:36
 */
import React, {useEffect, useState} from 'react';
import dynamic from 'next/dynamic';
import 'react-quill/dist/quill.snow.css';
import styles from "@/styles/publishTender.module.less";

const ReactQuill = dynamic(() => import('react-quill'), {ssr: false});

const TenderEditor: React.FC<{ defaultValue: string, onChange: Function }> = React.memo(({defaultValue, onChange}) => {

    const [editorValue, setEditorValue] = useState(defaultValue);

    const handleChange = (content) => {
        setEditorValue(content);
        onChange(content);
    };

    useEffect(() => {
        handleChange(defaultValue)
    }, [defaultValue]);

    return (<>
        <ReactQuill
            modules={{
                toolbar: [
                    ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                    ['blockquote', 'code-block'],
                    ['link', 'image', 'video', 'formula'],

                    [{ 'header': 1 }, { 'header': 2 }],               // custom button values
                    [{ 'list': 'ordered'}, { 'list': 'bullet' }, { 'list': 'check' }],
                    [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
                    [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
                    [{ 'direction': 'rtl' }],                         // text direction

                    [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
                    [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

                    [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                    // [{ 'font': [] }],
                    [{ 'align': [] }],

                    ['clean']                                         // remove formatting button
                ],
            }}
            value={editorValue}
            onChange={handleChange}
            theme="snow"
            placeholder="输入详情信息"
            className={styles.editor}
        />
    </>)
});

TenderEditor.displayName = "TenderEditor";

export default TenderEditor;

正常渲染,没有再报错:

二、参考文档

next.js - React Quill , unable to access import of Quill.import - Stack Overflow

react富文本编辑器 react-quill简单使用 - 简书

相关推荐
程序员黑豆2 小时前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC3 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
GreenTea3 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
浮生望3 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
万少4 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
波波0074 小时前
C# 15 重磅新特性: 带标签 break 与 continue:重新定义嵌套循环控制流
服务器·前端·c#
Brown.alexis5 小时前
es6知识点1-自备使用
前端·ecmascript·es6
小爬的老粉丝6 小时前
JavaScript 纯前端预览 WPS:先识别容器,再路由解析器
前端·javascript·wps
计算机魔术师6 小时前
新兴多智能体系统的模式与问题
前端
用户059540174467 小时前
AI Agent 上下文污染踩坑实录:用 pytest + Redis 揪出 3 类记忆串号 bug,排查 6 小时
前端·css