React Quill Component
React-Quill is a robust React component that wraps the popular Quill rich-text editor, allowing developers to easily integrate a powerful WYSIWYG editing experience into their React applications. The current stable version is 2.0.0, which introduced a full TypeScript port, React 16+ compatibility, and a refactored build system, aiming for a drop-in upgrade for most users. While major releases, like v2, occur periodically with significant overhauls, the project generally maintains an active development pace with bug fixes and minor improvements. A key differentiator is its seamless integration with React's component lifecycle, abstracting away much of Quill's imperative API. It supports controlled mode with specific caveats, leveraging Quill's Delta format for content management and offering extensive customization options for toolbars and formats. It is designed to be highly extensible and performant within a React ecosystem, making it a go-to choice for rich-text editing needs.
Common errors
-
Module not found: Can't resolve 'react-quill/dist/quill.snow.css' in '...'
cause Webpack or other bundler cannot find the CSS file, typically because a CSS loader (like `css-loader` and `style-loader`) is missing or misconfigured in the project's build setup.fixInstall `css-loader` and `style-loader` (or equivalent) via npm and configure your Webpack (or similar) setup to process `.css` files. -
TypeError: Cannot read properties of undefined (reading 'getEditor')
cause Attempting to call `getEditor()` on a `ReactQuill` instance that hasn't mounted yet, or calling it on a ref that isn't connected to the component.fixEnsure you are accessing the `getEditor()` method via a React ref to the `ReactQuill` component only after the component has mounted. Example: `const quillRef = useRef(null); // ... <ReactQuill ref={quillRef} ... /> // ... quillRef.current?.getEditor();` -
Editor content resets unexpectedly or changes are lost.
cause This often occurs due to the hybrid controlled/uncontrolled nature of `ReactQuill`. If the `value` prop is updated asynchronously or with a value that doesn't reflect Quill's true internal state, Quill's internal state might be overridden, leading to perceived data loss or jumps.fixAlways update the `value` prop synchronously within the `onChange` handler. If external state updates are needed, ensure they are merged carefully with the editor's current Delta state. For complex scenarios, consider using the `key` prop to force a re-render of the editor if external state requires a full reset.
Warnings
- breaking ReactQuill v2 removed support for long-deprecated props, the `ReactQuill Mixin`, and the `Toolbar` component. Applications upgrading from v1 or older might encounter breaking changes if these features were in use.
- gotcha Due to Quill's internal handling of its state, `ReactQuill` operates in a hybrid controlled/uncontrolled mode. While it accepts a `value` prop and `onChange` handler, it cannot prevent internal Quill edits. The `value` prop will override content if it differs from the internal state, potentially causing unexpected behavior or loss of user input if not managed carefully.
- gotcha The `ReactQuill` component requires specific CSS themes (e.g., `quill.snow.css`) to be imported and loaded for proper styling and functionality of the editor and its toolbar. Without these styles, the editor may appear unstyled or non-functional.
- breaking ReactQuill v2 requires React 16, 17, or 18 as peer dependencies. Applications using older React versions (e.g., React 15 or earlier) must upgrade their React installation or use an older `react-quill` version (e.g., v1.x).
Install
-
npm install react-quill -
yarn add react-quill -
pnpm add react-quill
Imports
- ReactQuill
import { ReactQuill } from 'react-quill';import ReactQuill from 'react-quill';
- quill.snow.css
import 'react-quill/dist/quill.snow.css';
- Delta
import { Delta } from 'quill';import type { Delta } from 'react-quill';
Quickstart
import React, { useState } from 'react';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
function MyComponent() {
const [value, setValue] = useState('');
return (
<div style={{ height: '300px', display: 'flex', flexDirection: 'column' }}>
<h3>ReactQuill Editor</h3>
<ReactQuill
theme="snow"
value={value}
onChange={setValue}
style={{ flexGrow: 1, overflowY: 'auto' }}
placeholder="Start typing here..."
/>
<p>Current content length: {value.length}</p>
</div>
);
}