简体   繁体   English

React Quill window 未定义

[英]React Quill window is not defined

I added React Quill to Next.js project and it works fine.我将React Quill添加到Next.js项目,它工作正常。 But the problem begins when I try to add ImageResize to editor.但是当我尝试将ImageResize添加到编辑器时,问题就开始了。

When I add line当我添加行

Quill.register('modules/imageResize', ImageResize)

I get an error我得到一个错误

ReferenceError: window is not defined ReferenceError: window 未定义

I think the problem is with Quill import, but can't find any working solution.我认为问题出在Quill导入上,但找不到任何可行的解决方案。

TesxtEditor.tsx文本编辑器.tsx

import { useState, useEffect } from 'react'
import dynamic from 'next/dynamic'
import 'react-quill/dist/quill.snow.css'
import styles from '../styles/Components/TextEditor.module.scss'
import ImageResize from 'quill-image-resize-module-react'
import { Quill } from 'react-quill'
const ReactQuill = dynamic(() => import('react-quill'), { ssr: false })

Quill.register('modules/imageResize', ImageResize)

interface TextEditorParams {
  onChange: (value: string) => string | void
  placeholder?: string
  error?: boolean
  defaultValue?: string | undefined
  required?: boolean
}

const TextEditor = ({
  onChange,
  placeholder,
  error,
  defaultValue,
  required
}: TextEditorParams) => {
  const [errorState, setErrorState] = useState<boolean | undefined>(false)

  useEffect(() => {
    let shouldUpdate = true
    if (shouldUpdate) {
      setErrorState(error)
    }

    return () => {
      shouldUpdate = false
    }
  }, [error])

  const modules = {
    toolbar: {
      container: [
        [{ header: [1, 2, 3, 4, 5, 6, false] }],
        ['bold', 'italic', 'underline', 'strike'],
        [{ list: 'ordered' }, { list: 'bullet' }],
        [{ align: [] }],
        ['link', 'image'],
        ['clean']
      ]
    },
    imageResize: {
      parchment: Quill.import('parchment'),
      modules: ['Resize', 'DisplaySize', 'Toolbar']
    }
  }

  const changeValue = (value: string) => {
    onChange(value)
  }

  return (
    <div className={styles.editor}>
      <ReactQuill
        modules={modules}
        defaultValue={defaultValue}
        onChange={(value) => changeValue(value)}
        placeholder={`${placeholder} ${required ? '*' : ''}`}
        className={errorState ? 'textEditor__error' : 'textEditor'}
      />
    </div>
  )
}

export default TextEditor

Wrap your component in this hellper component.将您的组件包装在这个 hellper 组件中。

To dynamically load a component on the client side, you can use the ssr option to disable server-rendering.要在客户端动态加载组件,可以使用 ssr 选项禁用服务器渲染。 This is useful if an external dependency or component relies on browser APIs like window.如果外部依赖项或组件依赖浏览器 API (如 window),这将很有用。

docs no ssr import 文档没有 ssr 导入

 import React from 'react'; import dynamic from 'next/dynamic'; const NoSSR = ({ children }) => ( <>{children}</> ); export default dynamic(() => Promise.resolve(NoSSR), { ssr: false, });

Just like that:就像那样:

 import NoSSR from "NoSSR" import {TextEdior as TextEditortQuill} from "./TextEditor" const TextEditor = ({...props}) => { return ( <NoSSR> <TextEditortQuill {...props}> </NoSSR> ) } export default TextEditor;

I am using react-quill (v2.0.0) with quill-image-resize-module-react (v3.0.0) on nextjs & I faced the same problem....我在 nextjs 上使用react-quill quill (v2.0.0) 和 quill quill-image-resize-module-react (v3.0.0) & 我遇到了同样的问题....

  1. First I found out that I need to dynamic import React-Quill or its parent component because this package depends on client side object like window. So here's what I did to import my Editor component which includes react-quill -首先,我发现我需要dynamic import React-Quill 或其父组件,因为这个 package 依赖于客户端 object,例如 window。所以这是我导入包含 react-quill 的编辑器组件的操作 -
import dynamic from 'next/dynamic'
const Editor = dynamic(import('components/Editor'), { ssr: false })
  1. The image resizing worked well until I clicked on any image & pressed DEL or BACKSPACE , at which point I found this window.Quill is undefined error.图像大小调整效果很好,直到我点击任何图像并按下DELBACKSPACE ,此时我发现这个window.Quill is undefined错误。 All I did was add window.Quill = Quill to my code & it was good to go again.我所做的只是将window.Quill = Quill添加到我的代码中,再次添加到 go 很好。
import ImageResize from 'quill-image-resize-module-react'
import ReactQuill, { Quill } from 'react-quill'
import 'react-quill/dist/quill.snow.css'

window.Quill = Quill
Quill.register('modules/imageResize', ImageResize)

Hope this helps someone save some time debugging希望这可以帮助某人节省一些调试时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM