2

I'm currently creating a blog dashboard, I'm beginner on web dev. and for the editor I'm using the React quill.. in the documentation there is a tutorial with on change handler. but it just console log "text change", and I'm trying to change the console log "text-change" with anything that I type on the quill editor.

My question is. How to do it? here my code for the useEffect:

   const { quill, quillRef } = useQuill();
    const [isi, setisi] = useState('')
    
   useEffect(() => {
      if (quill) {
        quill.on('text-change', () => { 
            console.log('teks-changed! ')
        })
        
        
        
      }
    }, [quill]);
Or Assayag
  • 5,662
  • 13
  • 57
  • 93

3 Answers3

3

You need to use quill.getText() to get the text. here is the full working code below.

const { quill, quillRef } = useQuill();
  React.useEffect(() => {
    if (quill) {
      quill.on('text-change', (e) => {
        const text = quill.getText();
        console.log(text);
      });
    }
  }, [quill]);
Sam
  • 1,040
  • 1
  • 7
  • 11
3

You've two options for getting quill data:

  1. Delta
  2. innerHTML

in the next example I've logged both:

import React, { useEffect } from "react";
import { useQuill } from "react-quilljs";
import "quill/dist/quill.snow.css"; // Add css for snow theme

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

  useEffect(() => {
    if (quill) {
      quill.on("text-change", (delta) => {
        console.log("delta", delta);
        console.log("innerHTML", quill.root.innerHTML);
      });
    }
  }, [quill]);

  return (
    <div style={{ width: 500, height: 300 }}>
      <div ref={quillRef} />
    </div>
  );
};

Abbas Hosseini
  • 1,545
  • 8
  • 21
0

inside useEffect you'd have to quill.on() and inside that use quill.getText() to get the data.

like this >>

useEffect(() => {
    if (quill) {
      quill.on('text-change', (e) => {
        const data = quill.getText();
        console.log(data);
      });
    }
  }, [quill])
Suraj
  • 802
  • 9
  • 7