tinymce 不支持 React hook 形式
我在 NextJS typescript 项目中使用 TinyMCE markdown 编辑器和 React Hook Form。如果我在没有 react-hook-form
的情况下使用 TinyMCE,它可以正常工作。
我在将其与 react-hook-form
一起使用时遇到问题。 我可以看到 editorRef.current.getContent()
Html 数据输出。但是 data
输出是空对象。
有什么想法这里可能有什么问题吗?
import { useRef } from 'react';
import { Editor } from '@tinymce/tinymce-react';
import { useForm } from 'react-hook-form';
export default function App() {
const editorRef = useRef<any>(null);
const { register, handleSubmit } = useForm();
const submitData = (data) => {
if (editorRef.current) {
console.log(editorRef.current.getContent());
}
console.log(data);
};
return (
<>
<form onSubmit={handleSubmit(submitData)}>
<Editor
onInit={(evt, editor) => (editorRef.current = editor)}
initialValue=""
init={{
height: 500,
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount',
]
}}
/>
<input type="submit" />
</form>
</>
);
}
I am using TinyMCE markdown editor with react Hook Form inside NextJS typescript project. TinyMCE works fine if I am using it without react-hook-form
.
I am getting issues while using it with react-hook-form
.
I can see editorRef.current.getContent()
Html data output. But data
output is empty object.
Any ideas what might be wrong here?
import { useRef } from 'react';
import { Editor } from '@tinymce/tinymce-react';
import { useForm } from 'react-hook-form';
export default function App() {
const editorRef = useRef<any>(null);
const { register, handleSubmit } = useForm();
const submitData = (data) => {
if (editorRef.current) {
console.log(editorRef.current.getContent());
}
console.log(data);
};
return (
<>
<form onSubmit={handleSubmit(submitData)}>
<Editor
onInit={(evt, editor) => (editorRef.current = editor)}
initialValue=""
init={{
height: 500,
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount',
]
}}
/>
<input type="submit" />
</form>
</>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经成功使用 RHF Controller 围绕tinyMCE 包装器(v7.25)编辑器 (v5.10) 如果有帮助的话
I've had success using RHF Controller wrapper (v7.25) around tinyMCE Editor (v5.10) if that helps
如上所述,使用控制器包装器可以正常工作。我能够成功使用tinymce和react-hook-form,如下所示:
确保您从控制器提供onEditorChange和onChange函数,您应该可以开始了
As Mentioned above using the Controller Wrapper works ok. Im able to successfully use tinymce and react-hook-form like the following:
Ensure you supply the onEditorChange with the onChange function from the controller and you should be good to go