I would like to make an editor from scratch with a contenteditable div in react.js The div is editable, but how can I add a button to make selected text bold? I tried "document.execCommand('bold', false, null);", but cant make it work.
import React from 'react';
const Editor = () => {
return (
<div className="editor">
<div className="toolbar">
<button onclick={(e) => {
document.execCommand('bold', false, null);
e.preventDefault();
}}>b</button>
</div>
<div className="editor-content" contenteditable>
<h1>Test</h1>
<p>Test</p>
</div>
</div >
)
}
export default Editor;
Props are case-sensitive. Refactor onclick
to onClick
and contenteditable
to contentEditable
and this implementation should work