简体   繁体   English

TinyMCE 使用 Save 插件时 React 中出现“错误:未找到表单元素”

[英]TinyMCE 'Error: No form element found' in React when using the Save plugin

I am trying to figure out how the save plugin works in TinyMCE for React.我试图弄清楚保存插件如何在 TinyMCE 中为 React 工作。 I want the user to be able to click the save button and the content is saved to a database (code not included here) but anything I try I just keep getting the error Error: No form element found .我希望用户能够单击保存按钮并将内容保存到数据库(此处未包含代码),但是我尝试的任何方法都只是不断收到错误Error: No form element found For example, please consider this component:例如,请考虑这个组件:

const Component = () => {
  const [editorContent, setEditorContent] = useState('');

  const handleEditorInit = () => {
    setEditorContent(someInitialContent);
  }

  const handleEditorChange = (content, editor) => {
    setEditorContent(content);
    console.log(content); // correctly shows formatted content
  }

  const handleOnSubmit = (content, editor) => {
    console.log(content); // correctly shows formatted content
  }

  return (
    <Fragment>
      <Editor
        apiKey="api-key"
        id="my-editor"
        value={editorContent}
        init={{
          menubar: false,
          inline: true,
          plugins: [
            'advlist lists image charmap print preview anchor',
            'searchreplace visualblocks code fullscreen',
            'insertdatetime media table paste code help wordcount save'
          ],
          toolbar:
            'undo redo | bold italic underline backcolor forecolor | \
            removeformat | save'
        }}
        onEditorChange={handleEditorChange}
        onInit={handleEditorInit}
        onSubmit={handleOnSubmit} // doesn't get rid of the error
      />
    </Fragment>
  );
}

When the above component is rendered, the content is displayed in the editor and it can be formatted (bold/underline etc.) without any issues.渲染上述组件时,内容会显示在编辑器中,并且可以格式化(粗体/下划线等)而不会出现任何问题。 The onInit , onEditorChange and onSubmit event handlers are working as expected (receiving the formatted content) but I still get the error no form element found when I click the save button on the toolbar. onInitonEditorChangeonSubmit事件处理程序按预期工作(接收格式化的内容),但是当我单击工具栏上的保存按钮时,我仍然收到错误no form element found
I have tried wrapping the <Editor> component in a <form> tag and I have also tried using the onSaveContent event handler provided by TinyMCE but I still get the error.我尝试将<Editor>组件包装在<form>标记中,并且我还尝试使用 TinyMCE 提供的onSaveContent事件处理程序,但我仍然收到错误消息。 I don't understand why I keep getting the Error: no form element found error with or without a form element.我不明白为什么我不断收到Error: no form element found错误。 I have looked everywhere for solutions but I can't seem to find one.我到处寻找解决方案,但似乎找不到。 Specially not one for React specifically.特别不是专门针对 React 的。 I have found similar issues here and here but I am still stuck on it and don't know how the solution fits into React.在这里这里发现了类似的问题,但我仍然坚持下去,不知道解决方案如何适合 React。
Am I implementing it wrong or does anyone have any idea where I'm going wrong with my implementation?我是在实施错误还是有人知道我的实施哪里出了问题?
Any help is greatly appreciated.任何帮助是极大的赞赏。 Thank you in advance.先感谢您。

The save plugin is designed to submit the standard HTML form that contains the underlying TinyMCE <textarea> . save插件旨在提交包含底层 TinyMCE <textarea>的标准 HTML 表单。 If you are using React I would suspect that your form submission would be handled via some React-specific JavaScript and not a standard HTML form as a standard form submission would make the entire page refresh which is very much not how React or other SPA frameworks tend to operate.如果您使用 React,我怀疑您的表单提交将通过一些特定于 React 的 JavaScript 而不是标准的 HTML 表单作为标准表单提交来处理,因为标准表单提交会使整个页面刷新,这与 React 或其他 SPA 框架的趋势非常不同操作。

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

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