React中遇到htmlToDraft is not a function的解决方案

报错具体代码如下

useEffect(()=>{
  const html = props.content
  const contentBlock = htmlToDraft(html);
  if(html === undefined) return;
  if (contentBlock) {
    const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
    const editorState = EditorState.createWithContent(contentState);
    seteditorState(editorState)
  }
},[props.content])

报错如下

Uncaught (in promise) TypeError: (0 , html_to_draftjs__WEBPACK_IMPORTED_MODULE_4__.htmlToDraft) is not a function

1、这是因为htmlToDraft版本兼容问题,把版本降为1.4.0 就可以了
2、加入一行代码htmlToDraft = require('html-to-draftjs').default就可以了

posted @ 2022-04-05 12:02  吉吉王  阅读(212)  评论(0)    收藏  举报