Using react-quilljs we can add a RichText Editor in to our web application (Reactjs). How do we get the contents/state ?
We can use the quill object return by the hook to access the state.
const { quill, quillRef } = useQuill();
Trying to access the data directly ends up with error. We need useEffect hook to keep track of the content.
React.useEffect(() => {
if (quill) {
quill.on("text-change", (delta, oldDelta, source) => {
console.log("Text change!");
console.log(quill.getText()); // Get text only
console.log(quill.getContents()); // Get delta contents
console.log(quill.root.innerHTML); // Get innerHTML using quill
console.log(quillRef.current.firstChild.innerHTML); // Get innerHTML using quillRef
});
}
}, [quill]);
Now we can us the quill on method for track changes through a function as above. For getting Text only we can use getText() method, for entire object delta we require getContents() and for HTML can rely on innerHTML.
See the CodesandBox for a live example
- Quill without toolbar
- Quilljs mention snippet
- Create Quill RichText editor using react-quill
- React-quill double instance fix
- How to get HTML from Remirror editor in Reactjs
- How to get state as JSON from Remirror editor in Reactjs
- How to get content from Quilljs in React
- Create a TinyMCE Rich Text Editor in React
- Create a Slate Rich Text Editor in React
- Create a Material-UI Rich Text Editor in React