React Reference ยท Props and Attributes
React contentEditable prop
How to use contentEditable in React: the suppressContentEditableWarning prop, reading edits with onInput, and why React-managed children conflict.
The HTML contenteditable attribute turns any element into a small text editor. In JSX you write it as contentEditable (camelCase). The browser then handles typing, selection and the caret, and the DOM changes underneath React.
That creates a conflict: React believes it owns the element's children. If a component renders children and marks the element editable, React logs a warning, because the next re-render may overwrite what the user typed. The fix is to render the initial text once, read edits from events, and avoid re-rendering the same children from state.
<p contentEditable suppressContentEditableWarning onInput={handleInput}> Edit me </p>
Try it
The state only mirrors the text; it is never passed back in as children. If you need to replace the content from outside (for example a "Reset" button), do it through a ref.
Syntax
<element contentEditable /> <element contentEditable="plaintext-only" /> <element contentEditable suppressContentEditableWarning />
| Name | Description |
|---|---|
contentEditable |
true, false, or the string "plaintext-only" (supported in current browsers; no rich-text formatting). Makes the element editable. |
suppressContentEditableWarning |
Boolean. Hides React's warning about editable elements that also have React-managed children. |
onInput |
Fires after each edit. Read e.currentTarget.textContent or innerHTML. |
Usage notes
- Use
onInput, notonChange. There is novaluefor these elements, and React'sonChangeis meant for form controls. - Do not feed the text back as children.
as a child of the editable element makes React rewrite the node on every keystroke, which resets the caret to the start. - Rich text is hard. Pasting, undo, lists and selection behave differently across browsers. For anything beyond a small text field, use a dedicated editor library.
- Security. If you store
innerHTML, treat it as untrusted and sanitise it before rendering it again; seedangerouslySetInnerHTML. - Accessibility. An editable
divneeds a role and a label (role="textbox",aria-label, andaria-multiline="true"for several lines). Prefer<textarea>or<input>when a plain form control is enough. - Newlines are inserted as
<div>or<br>depending on the browser;textContentdrops them,innerTextkeeps line breaks.