this.props.editorState.isInCompositionMode 不是 Draft.js 中的函数错误

this.props.editorState.isInCompositionMode is not a function error in Draft.js of react

我的 objective 是用从数据库中检索到的 HTML 初始化 Draft.js 编辑器。每当我尝试初始化编辑器时,都会抛出错误:

this.props.editorState.isInCompositionMode is not a function

为了将Draft.js内容转换成HTML,我使用了draft-js-export-html and for converting back to editorState object I have used draft-js-import-html

我在下面提到了我使用过的代码:

Editor.js

    constructor(props) {
        super(props);

        this.state = {
            editorState: this.props.setEditorState,
         }
    }
    render(
       <Editor
            placeholder={"Please give details of news..."}
            editorState={this.state.editorState}
            handleKeyCommand={this.handleKeyCommand}
            keyBindingFn={this.keyBindingFunction}
            onChange={this.onChange} />
       );

MainBody.js

<EditComp setEditorState={stateFromHTML(response.data[i].htmlDesc)} />

我在控制台中得到的错误如下:

Uncaught TypeError: this.props.editorState.isInCompositionMode is not a function
    at DraftEditor._showPlaceholder (static/js/0.chunk.js:80878)
    at DraftEditor._renderPlaceholder (static/js/0.chunk.js:80882)
    at DraftEditor.render (static/js/0.chunk.js:80943)
    at finishClassComponent (static/js/0.chunk.js:153826)
    at updateClassComponent (static/js/0.chunk.js:153781)
    at beginWork (static/js/0.chunk.js:155510)
    at HTMLUnknownElement.callCallback (static/js/0.chunk.js:135702)
    at Object.invokeGuardedCallbackDev (static/js/0.chunk.js:135751)
    at invokeGuardedCallback (static/js/0.chunk.js:135804)
    at beginWork$ (static/js/0.chunk.js:161088)
    at performUnitOfWork (static/js/0.chunk.js:160014)
    at workLoopSync (static/js/0.chunk.js:159987)
    at performSyncWorkOnRoot (static/js/0.chunk.js:159576)
    at static/js/0.chunk.js:147628
    at unstable_runWithPriority (static/js/0.chunk.js:187261)
    at runWithPriority (static/js/0.chunk.js:147574)
    at flushSyncCallbackQueueImpl (static/js/0.chunk.js:147623)
    at flushSyncCallbackQueue (static/js/0.chunk.js:147611)
    at discreteUpdates (static/js/0.chunk.js:159730)
    at discreteUpdates (static/js/0.chunk.js:136807)
    at dispatchDiscreteEvent (static/js/0.chunk.js:141282)

我有没有犯错?抛出该错误的原因是什么?

尝试使用控制台日志查看 stateFromHTML(response.data[i].htmlDesc).

的结果是什么

如我所见,您期望它是一个具有函数 isInCompositionMode 的对象,但它显然不包含。

尝试找到调用 isInCompositionMode 的位置。

从命名 setEditorState 表明它是一个函数,你将它设置为你的状态 "editorState"(而不是调用它)因此 editorState 是一个不能像对象一样访问的函数。

一般来说,我会说您提供的数据不足,无法提供答案。

只有一件事不见了。 EditorState.createWithContent() 需要。 MainBody.js 文件代码编辑如下:

var contentState = stateFromHTML(this.props.setEditorState);

this.state = {
editorState: EditorState.createWithContent(contentState)
}