ReferenceError: 在使用ReactQuill时,出现 “document is not defined” 错误。

huangapple go评论83阅读模式
英文:

ReferenceError: document is not defined when using ReactQuill

问题

It looks like you're encountering an issue when building your Next.js project with ReactQuill. The error message suggests that there's a problem with the document object being undefined, which usually happens when trying to use browser-specific code on the server-side.

To fix this issue, you should wrap the code that relies on the document object inside a condition that checks if it's running on the client-side. You can do this using the "typeof window !== 'undefined'" check. Here's an example of how you can modify your code:

import ReactQuill from 'react-quill';
import React, { useState, useEffect } from 'react';

export default function Articles() {
    const [content, setContent] = useState<ReactQuill.Value>('');

    useEffect(() => {
        // Check if we're running on the client-side before using the document object
        if (typeof window !== 'undefined') {
            // Your code that uses the document object goes here
        }
    }, []);

    const handleChange = (value: ReactQuill.Value) => {
        setContent(value);
    };

    return (
        <ReactQuill theme="snow" />
    );
}

This change should help resolve the "document is not defined" error when building your Next.js project with ReactQuill. Make sure to replace "// Your code that uses the document object goes here" with the relevant code that requires the document object.

英文:

i'm getting start with both next.js and ReactQuill. But when i went run yarn build, this is what I get:

 info Creating an optimized production build
- info Compiled successfully
- info Linting and checking validity of types
- info Collecting page data ..ReferenceError: document is not defined
    at Object.&lt;anonymous&gt; (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:7661:12)
    at __webpack_require__ (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:36:30)
    at Object.&lt;anonymous&gt; (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:1030:1)
    at __webpack_require__ (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:36:30)
    at Object.&lt;anonymous&gt; (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:5655:14)
    at __webpack_require__ (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:36:30)
    at Object.&lt;anonymous&gt; (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:10045:13)
    at __webpack_require__ (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:36:30)
    at Object.&lt;anonymous&gt; (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:11557:18)
    at __webpack_require__ (C:\Users\joao\Desktop\liceu\node_modules\quill\dist\quill.js:36:30)

&gt; Build error occurred
Error: Failed to collect page data for /Articles
    at C:\Users\joao\Desktop\liceu\node_modules\next\dist\build\utils.js:1152:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  type: &#39;Error&#39;
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

How am I supposed to use that? I installed the packages, create the file under the pages folder. The file goes like this:

import ReactQuill from &#39;react-quill&#39;;
import React, { useState, useEffect  } from &#39;react&#39;;


export default function Articles() {

    const [content, setContent] = useState&lt;ReactQuill.Value&gt;(&#39;&#39;);

    const handleChange = (value: ReactQuill.Value) =&gt; {
      setContent(value);
    };

    return (
      &lt;ReactQuill theme=&quot;snow&quot; /&gt;
    );
  }

What am I imissing? How do I fix that?

答案1

得分: 1

你可以使用 dynamic。请参见此示例

看起来 ReactQuill 只能在浏览器上运行。你只能在客户端上渲染这个元素。我不确定你使用的是哪个版本的 Next.js。
你可以添加 useIsMounted 钩子:

function useIsMounted() {
  const isMounted = useRef(false)
  useEffect(() => {
    isMounted.current = true
    return () => {
      isMounted.current = false
    }
  }, [])

  return useCallback(() => isMounted.current, [])
}

示例链接

然后你可以像这样使用它:

import ReactQuill from 'react-quill'
import React, { useState, useEffect } from 'react'

export default function Articles() {
  const [content, setContent] = useState<ReactQuill.Value>('')
  const isMounted = useIsMounted()

  const handleChange = (value: ReactQuill.Value) => {
    setContent(value)
  }

  return isMounted() ? <ReactQuill theme="snow" /> : null
}
英文:

You can use dynamic. Please, see this example

Looks like ReactQuill runs only on browser. You can render this element only on client side. I'm not sure what version of Next.js do you use.
You may add useIsMounted hook:

function useIsMounted() {
  const isMounted = useRef(false)
  useEffect(() =&gt; {
    isMounted.current = true
    return () =&gt; {
      isMounted.current = false
    }
  }, [])

  return useCallback(() =&gt; isMounted.current, [])
}

Link to example

Then you it like this:

import ReactQuill from &#39;react-quill&#39;
import React, { useState, useEffect } from &#39;react&#39;

export default function Articles() {
  const [content, setContent] = useState&lt;ReactQuill.Value&gt;(&#39;&#39;)
  const isMounted = useIsMounted()

  const handleChange = (value: ReactQuill.Value) =&gt; {
    setContent(value)
  }

  return isMounted() ? &lt;ReactQuill theme=&quot;snow&quot; /&gt; : null
}

huangapple
  • 本文由 发表于 2023年5月30日 01:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定