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

问题描述 投票:0回答:2

我的目标是使用从数据库中检索的HTML初始化Draft.js编辑器。每当我尝试初始化编辑器时,都会引发错误:

this.props.editorState.isInCompositionMode is not a function

[我已使用draft-js-export-html来转换Draft.js内容为HTML,并已使用draft-js-import-html来转换回editorState对象。

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

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$1 (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$$1 (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$2 (static/js/0.chunk.js:147574)
    at flushSyncCallbackQueueImpl (static/js/0.chunk.js:147623)
    at flushSyncCallbackQueue (static/js/0.chunk.js:147611)
    at discreteUpdates$1 (static/js/0.chunk.js:159730)
    at discreteUpdates (static/js/0.chunk.js:136807)
    at dispatchDiscreteEvent (static/js/0.chunk.js:141282)

我犯了什么错误?引发该错误的原因是什么?

reactjs draftjs
2个回答
0
投票

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

正如我所看到的,您期望它是具有isInCompositionMode函数的对象,该函数显然不包含此对象。

尝试找到要调用isInCompositionMode的位置。

从命名setEditorState表示它是一个函数,您将其设置为状态“ editorState”(不调用它),因此editorState是一个不能像对象一样访问的函数。

[通常,我会说您提供的数据不足,无法提供答案。


0
投票

只是一件事不见了。需要EditorState.createWithContent()MainBody.js文件代码使用以下代码进行编辑:

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

this.state = {
editorState: EditorState.createWithContent(contentState)
}
© www.soinside.com 2019 - 2024. All rights reserved.