react项目实战学习笔记-学习43-wangeditor

 // 模拟componentDidMount
  useEffect(() => {
    editor = new E('#div1')
    editor.config.onchange = (newHtml) => {
      setContent(newHtml)
    }
    editor.create()

    // 根据地址栏id做请求
    if (params.id) {
      ArticleSearchApi({ id: params.id }).then(res => {
        if (res.errCode === 0) {
          editor.txt.html(res.data.content) // 重新设置编辑器内容
          setTitle(res.data.title)
          setSubTitle(res.data.subTitle)
        }
      })
    }

    return () => {
      // 组件销毁时销毁编辑器  注:class写法需要在componentWillUnmount中调用
      editor.destroy()
    }
  }, [location.pathname])

posted @ 2022-08-26 20:35  前端导师歌谣  阅读(27)  评论(0)    收藏  举报