react-quill组件在我开始输入内容或在任何其他输入字段中输入内容时消失。

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

react-quill component disappears when I start Typing in it or in any other input field

问题

我有一个React应用,我正在使用Quill编辑器创建帖子。每当我在任何字段上开始输入,包括Quill编辑器时,编辑器完全消失。

这是我的代码:

import React, { useState } from 'react';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';

const CreatePost = () => {

    // ... 其他代码 ...

    return (
        <>
            <div className="p-4 lg:ml-16">
                <div className="p-4 mt-14">
                    <h2 className='text-center font-Play font-semibold text-2xl mb-10'>Create your story...</h2>
                    <div className='grid gap-4 grid-cols-10 h-full'>
                        <div className=' h-fit w-full col-span-10'>
                            <form onSubmit={createNewPost}>
                                <label htmlFor="title" className="block text-sm font-medium text-gray-700">Title</label>
                                <div className="mt-1">
                                    <input value={title} onChange={e => setTitle(e.target.value)} type='text' id="title" name="title" className="resize-none mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" placeholder="Title goes here" required></input>
                                </div>
                                <label htmlFor='image-upload' className="mt-6 mb-2 block text-sm font-medium text-gray-700">Select a banner</label>
                                <input type="file" onChange={e => setBanner(e.target.files)} id='image-upload' name='banner' accept="image/*" className='rounded-md' required></input>
                                <label htmlFor='body' className="mt-6 mb-2 block text-sm font-medium text-gray-700">Body</label>
                                <div>
                                    <ReactQuill value={content} onChange={newValue => setContent(newValue)}  modules={modules} htmlFormats={htmlFormats} id="body" placeholder='Type Content...'></ReactQuill>
                                </div>
                                <button type="submit" className="mt-8 text-white bg-gradient-to-br from-green-400 to-blue-600 hover:bg-gradient-to-bl focus:ring-4 focus:outline-none focus:ring-green-200 dark:focus:ring-green-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2">Create</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </>
    )
}

export default CreatePost;

我尝试了之前关于此问题的答案,但没有一个真正有帮助。

这是在开始输入之前的情况

这是我开始输入后的情况

英文:

I have a react app where I am using Quill editor to create a post. The editor completely disappears whenever I start typing on any of the fields including the quill editor.

Here's my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React, { useState } from &#39;react&#39;;
import ReactQuill from &#39;react-quill&#39;;
import &#39;react-quill/dist/quill.snow.css&#39;;
const CreatePost = () =&gt; {
const modules = {
toolbar: [
[&#39;bold&#39;, &#39;italic&#39;, &#39;underline&#39;, &#39;strike&#39;],          // toggled buttons
[&#39;blockquote&#39;, &#39;code-block&#39;],
[{ &#39;header&#39;: [1, 2, 3, 4, 5, 6, false] }],
[{ &#39;list&#39;: &#39;ordered&#39; }, { &#39;list&#39;: &#39;bullet&#39; }],
[{ &#39;script&#39;: &#39;sub&#39; }, { &#39;script&#39;: &#39;super&#39; }],       // superscript/subscript
[{ &#39;indent&#39;: &#39;-1&#39; }, { &#39;indent&#39;: &#39;+1&#39; }],           // outdent/indent
[{ &#39;direction&#39;: &#39;rtl&#39; }],                           // text direction
[{ &#39;size&#39;: [&#39;small&#39;, false, &#39;large&#39;, &#39;huge&#39;] }],    // custom dropdown
[{ &#39;color&#39;: [] }, { &#39;background&#39;: [] }],            // dropdown with defaults from theme
[{ &#39;font&#39;: [] }],
[{ &#39;align&#39;: [] }],
[&#39;link&#39;],
[&#39;clean&#39;],                                          // remove formatting button
[&#39;video&#39;]                                       
]
};
const htmlFormats = [
&#39;header&#39;,
&#39;bold&#39;, &#39;italic&#39;, &#39;underline&#39;, &#39;strike&#39;, &#39;blockquote&#39;,
&#39;list&#39;, &#39;bullet&#39;, &#39;indent&#39;,
&#39;link&#39;, &#39;image&#39;
];
const [title, setTitle] = useState(&#39;&#39;);
const [content, setContent] = useState(&#39;&#39;);
const [banner, setBanner] = useState(&#39;&#39;);
const createNewPost = (e) =&gt; {
e.preventDefault();
const data = new FormData();
data.set(&#39;title&#39;, title);
data.set(&#39;content&#39;, content);
data.set(&#39;banner&#39;, banner);
console.log(banner)
}
return (
&lt;&gt;
&lt;div className=&quot;p-4 lg:ml-16&quot;&gt;
&lt;div className=&quot;p-4 mt-14&quot;&gt;
&lt;h2 className=&#39;text-center font-Play font-semibold text-2xl mb-10&#39;&gt;Create your story...&lt;/h2&gt;
&lt;div className=&#39;grid gap-4 grid-cols-10 h-full&#39;&gt;
&lt;div className=&#39; h-fit w-full col-span-10&#39;&gt;
&lt;form onSubmit={createNewPost}&gt;
&lt;label htmlFor=&quot;title&quot; className=&quot;block text-sm font-medium text-gray-700&quot;&gt;Ttile&lt;/label&gt;
&lt;div className=&quot;mt-1&quot;&gt;
&lt;input value={title} onChange={e=&gt;setTitle(e.target.value)} type=&#39;text&#39; id=&quot;title&quot; name=&quot;title&quot; className=&quot;resize-none mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm&quot; placeholder=&quot;Title goes here&quot; required&gt;&lt;/input&gt;
&lt;/div&gt;
&lt;label htmlFor=&#39;image-upload&#39; className=&quot;mt-6 mb-2 block text-sm font-medium text-gray-700&quot;&gt;Select a banner&lt;/label&gt;
&lt;input type=&quot;file&quot; onChange={e =&gt; setBanner(e.target.files)} id=&#39;image-upload&#39; name=&#39;banner&#39; accept=&quot;image/*&quot; className=&#39;rounded-md&#39; required&gt;&lt;/input&gt;
&lt;label htmlFor=&#39;body&#39; className=&quot;mt-6 mb-2 block text-sm font-medium text-gray-700&quot;&gt;Body&lt;/label&gt;
&lt;div&gt;
&lt;ReactQuill value={content} onChange={newValue =&gt; setContent(newValue)}  modules={modules} htmlFormats={htmlFormats} id=&quot;body&quot; placeholder=&#39;Type Content...&#39; &gt;&lt;/ReactQuill&gt;
&lt;/div&gt;
&lt;button type=&quot;submit&quot; className=&quot;mt-8 text-white bg-gradient-to-br from-green-400 to-blue-600 hover:bg-gradient-to-bl focus:ring-4 focus:outline-none focus:ring-green-200 dark:focus:ring-green-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2&quot;&gt;Create&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div &gt;
&lt;/&gt;
)
}
export default CreatePost

<!-- end snippet -->

I tried following the previous answers regarding this, but none actually helped.

This is before I start typing

This is when I started typing

答案1

得分: 1

将模块声明移到组件之外解决了我的问题。

import React, { useState } from 'react';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';

const modules = {
  // 您的模块设置
}

const CreatePost = () => {
  // 您的组件
};
英文:

Moving modules declaration outside component resolved this issue for me.

import React, { useState } from &#39;react&#39;;
import ReactQuill from &#39;react-quill&#39;;
import &#39;react-quill/dist/quill.snow.css&#39;;
const modules = {
// Your module settings
}
const CreatePost = () =&gt; {
// your component
};

答案2

得分: 0

将模块移出组件可以解决问题。确保如果您在使用任何处理程序来处理图像或其他内容,也将它们移至组件之外。

英文:

Moving modules outside the component can solve issue. Make sure if you using any handlers for image or anything put them outside as well.

答案3

得分: 0

将模块移出组件,或者如果你仍然想在组件内使用模块,请使用 useMemo。

英文:

moving modules outside the component or if you still want using modules inside the component, use useMemo for it

huangapple
  • 本文由 发表于 2023年2月24日 04:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549826.html
匿名

发表评论

匿名网友

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

确定