0

I am making a simple blogging site using react, I am using react-quill and i can't figure out the best way to store the data from react-quill editor to the backend. From my code, the content I get is already formatted with html tags. I would like to store the data in a way that when It is stored efficiently and when retrieved from the server, it is displayed with all the formatting e.g bold text, italized... The sample code is below. Also if there is a way i can improve my code, feel free to tell me :)

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

const Create = () => {
    const [body, setBody] = useState('');
    const handleBody = (e) => {
        console.log(e)
        setBody(e)
    }
    return (
        <main>
            <div className="form-container">
                <form>
                    <h2>create post</h2>
                    <ReactQuill
                        placeholder='spread your message...'
                        modules={Create.modules}
                        formats={Create.formats}
                        onChange={handleBody}
                        value={body}
                    />
                    <button id="submit-btn">post</button>
                </form>
            </div>
        </main>
    )
}

Create.modules = {
    toolbar: [
      [{ 'font': [] }],
      [{ 'size': ['small', false, 'large', 'huge'] }],
      ['bold', 'italic', 'underline'],
      [{'list': 'ordered'}, {'list': 'bullet'}],
      [{ 'align': [] }],
      [{ 'color': [] }, { 'background': [] }],
      ['link', 'image'],
      ['code-block'],
      ['clean']
    ]
};

Create.formats = [
    'font',
    'size',
    'bold', 'italic', 'underline',
    'list', 'bullet',
    'align',
    'color', 'background',
    'link',
    'image',
    'code-block'
  ];

export default Create;
ismail pervez
  • 159
  • 2
  • 10

1 Answers1

0

It would be good if you use the delta property of quill. This helps in saving the text in json object in database. And if you want the text back with all format applied you can use JSON.parse().

Anand
  • 1
  • 1