[英]React Quill causes rerender of <input> outside of Quill's editor
I have the following React code in my component:我的组件中有以下 React 代码:
const TextEditor = () => {
const [richTextValue, setRichTextValue] = useState('');
const [storyTitle, setStoryTitle] = useState('');
const handleRichEditorOnChange = (content, delta, source, editor) => {
setRichTextValue(editor.getHTML());
}
const handleTitleChange = event => {
setStoryTitle(event.target.value);
}
return (
<>
<input type="text"
placeholder="Title"
value={storyTitle}
onChange={handleTitleChange}/>
<ReactQuill theme="snow"
value={richTextValue}
onChange={handleRichEditorOnChange}/>
</>
)
}
If i type anything in <input>
element, I lose focus after each letter typed because of Quill causing rerender somehow and get the error "Cannot flush updates when React is already rendering".如果我在
<input>
元素中输入任何内容,我会在输入每个字母后失去焦点,因为 Quill 会以某种方式导致重新渲染,并得到错误“当 React 已经在渲染时无法刷新更新”。 However, if I remove ReactQuill
element, input's behaviour is normal and I don't receive any error in console.但是,如果我删除
ReactQuill
元素,输入的行为是正常的,我不会在控制台中收到任何错误。
What is the reason behind this strange and unexpected behaviour and how can it be fixed?这种奇怪和意想不到的行为背后的原因是什么?如何解决?
Nevermind.没关系。 I had:
我有:
if (richTextEditorRef.current) {
richTextEditorRef.current.focus();
}
in code before adding input and now I figured out that it was forcing React to focus on Quill editor.在添加输入之前的代码中,现在我发现它迫使 React 专注于 Quill 编辑器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.