我应该将 react-quilljs 对象的哪一部分保存到后端的数据库中?

问题描述

我正在尝试使用 react-quilljs 在完整的 mern 堆栈应用程序中实现文本编辑器,但我不确定应该将 quill 对象的哪一部分保存到后端的数据库中。我有以下编辑器代码

import React,{ useState } from 'react';

import { usequill } from 'react-quilljs';
// or const { usequill } = require('react-quilljs');

import 'quill/dist/quill.sNow.css'; // Add css for sNow theme
// or import 'quill/dist/quill.bubble.css'; // Add css for bubble theme

export default () => {
  const { quill,quillRef } = usequill();

  const [content,setContent] = useState('');

  console.log(quill); // undefined > quill Object
  console.log(quillRef); // { current: undefined } > { current: quill Editor Reference }

  React.useEffect(() => {
    if (quill) {
      quill.on('text-change',() => {
        setContent(?) // What part of the quill object I need to use here to set the content before sending it to the backend <-----------------
        console.log(quill.getFormat());
        console.log('Text change!');
      });
    }
  },[quill]);

  const submitHandler = (e) => {//here I will send the editor content to the bakcend};

  return (
    <div style={{ width: 500,height: 300 }}>
      <div ref={quillRef} />
      <form onSubmit={submitHandler}>
        <button type='submit'>Submit</button>
      </form>
    </div>
  );
};

解决方法

你应该分别保存两件事,一个是羽毛笔的原始数据,另一个是从中提取文本

参考下面的沙盒,需要保存内容

https://codesandbox.io/s/react-quill-demo-forked-0qr5f?file=/src/editor.js