英文:
how to import {Quill} in next js? or how can I add hr tag in Quill editor with Next.js
问题
我的错误代码
----
你好,我想自定义Quill编辑器,就像Divider一样。但是,当我使用{Quill}时,出现了这样的错误。我该如何解决?
----
这是我的代码
```javascript
import { Quill } from 'react-quill';
const quill = Quill;
const modules = useMemo(
() => ({
toolbar: {
container: /**/
handlers: {
hr: () => {
setHtmlContent((prev: string) => {
return prev + '<hr/>';
});
},
},
},
}),
[]
);
const formats = [
/**/
'hr',
];
return (
<ReactQuill
theme="snow"
modules={modules}
formats={formats}
value={htmlContent}
onChange={(content, delta, source, editor) => {
setHtmlContent(editor.getHTML());
}}
/>
);
};
英文:
my error code
hello, I want to custom Quill editor like Divider.<br/>
but, when I use the {Quill}, I have error like that.<br/>
how can I solve that?<br/>
here is my code
import { Quill } from 'react-quill';
const quill = Quill;
const modules = useMemo(
() => ({
toolbar: {
container: /**/
handlers: {
hr: () => {
setHtmlContent((prev: string) => {
return prev + '<hr/>';
});
},
},
},
}),
[]
);
const formats = [
/**/
'hr',
];
return (
<ReactQuill
theme="snow"
modules={modules}
formats={formats}
value={htmlContent}
onChange={(content, delta, source, editor) => {
setHtmlContent(editor.getHTML());
}}
/>
);
};
答案1
得分: 1
// 将`react-quill`包在服务器端渲染页面时出现错误,是因为它使用了一些在服务器上不可用的浏览器API,比如`document`。可以通过在客户端使用`dynamic`导入来解决这个问题。
```js
import dynamic from 'next/dynamic';
const Quill = dynamic(() => import('react-quill'), {
// 将ssr选项设置为`false`,这样它只会在客户端加载。
ssr: false,
// 在组件加载时,您还可以添加一个加载指示器。
loading: () => <p>Loading ...</p>,
})
希望对您有所帮助。
<details>
<summary>英文:</summary>
The `react-quill` package uses some browser APIs like `document` which are not available on the server. The error is caused when Next.js is Server-Side-Rendering the page. You can fix this by loading the package on the client only with a `dynamic` import.
Here, try to import like this instead:
```js
import dynamic from 'next/dynamic';
const Quill = dynamic(() => import('react-quill'), {
// Set this to `false` so it is only loaded on the client.
ssr: false,
// You can also add an indicator while the component is loading.
loading: () => <p>Loading ...</p>,
})
Hope this helps.
答案2
得分: 0
add 'use client' at the first line and use dynamic for you to instantiate your Quill editor
const ReactQuill = useMemo(() => dynamic(() => import('react-quill'), { ssr: false }), []);
英文:
add 'use client' at the first line and use dynamic for you to instantiate your Quill editor
const ReactQuill = useMemo(() => dynamic(() => import('react-quill'), { ssr: false }), []);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论