javascript - 在 Draft js 中设置没有应用程序状态的 EditorState
问题描述
我想用一些初始内容更新 Draft js Editor。我知道我们可以EditorState
使用组件状态进行更改。
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';
class MyEditor extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = (editorState) => this.setState({editorState});
}
render() {
return (
<Editor editorState={this.state.editorState} onChange={this.onChange} />
);
}
}
每当我尝试在编辑器中更改某些内容时,它都会重新渲染。这会导致一些性能问题。无论如何我可以在不更改状态的情况下设置或更改编辑器的状态。类似的东西
EditorState.set(editorState); //here editorState is new editor state;
知道如何实现吗?
解决方案
经过两天的奋斗,我发现了一个有用的所见即所得编辑器Draft.js
,它建立在很酷的功能之上。它具有 给出的所有功能Draft.js
。我们可以使用编辑器加载现有内容refs
。
import {EditorState, convertFromRaw} from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
class myEditor extends Component {
componentDidMount(){
//I have stored rawContent in database
let rawContent={"blocks":[{"key":"3ge2q","text":"Hello World!;","type":"header-three","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}],"entityMap":{}};
//Convert Raw to contentState
let content=convertFromRaw(rawContent);
let editorState=EditorState.createWithContent(content);
this.refs.editor.editor.update(editorState); //Update the editor with updated content.
}
render(){
<Editor
ref="editor"
wrapperClassName="demo-wrapper"
editorClassName="template-editor"
spellCheck={true}
placeholder="Type something"
onEditorStateChange={this.onEditorStateChange.bind(this)}
/>
}
}
推荐阅读
- ruby-on-rails - 使用涉及使用 ActiveRecord 连接表的 where 子句选择模型
- css - Tailwind CSS边框问题
- javascript - 点击时显示和隐藏不同的信息
- indexing - 谷歌搜索和我的网页有问题
- python - 使用登录名和每个文件的多个子路由从 Web 自动下载文件
- azure - 通过事件中心在数据块中存储 Blob 事件
- node.js - react-redus post api call err 404 not found
- azure - ##[错误]Bash 以代码“1”退出。使用管道部署时
- php - 在 WooCommerce 中为具有特定产品属性术语的变体制作所需的订单备注字段
- laravel - 带有 Socket.io 和 Redis 的 Laravel Echo 服务器上的 CORS 缺少允许来源