我需要在我的项目中使用Draft-js。我按照官方文档中的说明进行操作。我导入了样式表,但未显示。
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js'
import 'draft-js/dist/Draft.css'
const MyComponent: React.FC<MyComponentProps> = () => {
const [editorState, setEditorState] = React.useState(() => EditorState.createEmpty());
return (
<Editor editorState={editorState} onChange={setEditorState} />
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
这是它的渲染方式(我自己在检查工具中添加了背景色)。
有人有什么解决方法吗?
请尝试以下方式:
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';
import 'draft-js/dist/Draft.css';
function MyEditor() {
const [editorState, setEditorState] = React.useState(
() => EditorState.createEmpty(),
);
return <Editor editorState={editorState} onChange={setEditorState} />;
}
ReactDOM.render(<MyEditor />, document.getElementById('container'));
此外,HTML文件的<head></head>
块中必须具有以下元标记:
<meta charset="utf-8" />