如何在状态更改时更改DraftJS Editor的初始内容?

问题描述

我使用Draft.js启动了一些内容的编辑器。初始内容按以下示例中给出的状态定义。更改状态后,如何用新内容替换整个内容。在下面的示例中,我尝试了state的{​​{1}}形式,当用户单击initData时,状态更改为What is your name?,翻译后的内容应替换编辑器。

input button

解决方法

使用ContentState初始化编辑器并更改初始内容。 要初始化,请使用:

,而不是EditorState.createEmpty()以及构造函数中的所有其他代码行
this.state = {
  editorState: EditorState.createWithContent(
               ContentState.createFromText('What is your name?')
              ),};

要在点击处理程序中更改此数据,请执行以下操作:

      changeContent() {
        this.setState({
             editorState: EditorState.createWithContent(
                          ContentState.createFromText('Quel est votre nom?')
                          )
         });   
       }