【发布时间】:2020-02-26 02:28:13
【问题描述】:
我正在尝试实现一个按钮,在单击它后将内联样式更改为粗体。我想实现这个 example 但使用 React Hooks。
单击按钮后,RichUtils 正在执行,但并未将文本更改为粗体。我错过了什么?
import React, { useEffect, useRef, useState } from 'react';
import {Editor, EditorState, RichUtils} from 'draft-js';
const MyEditor = () => {
const [editorState, setEditorState] = useState(EditorState.createEmpty());
const editor = useRef(null);
const focusEditor = () => {
editor.current.focus();
}
const boldText = () => {
let nextState = RichUtils.toggleInlineStyle(editorState, 'BOLD');
setEditorState(nextState);
}
return (
<div onClick = {focusEditor}>
<button onClick = { () => boldText()}>Bold</button>
<Editor
ref = {editor}
editorState = {editorState}
onChange = {editorState => setEditorState(editorState)}
/>
</div>
);
}
export default MyEditor;
【问题讨论】:
标签: reactjs react-hooks draftjs