wangEditor-for-react icon indicating copy to clipboard operation
wangEditor-for-react copied to clipboard

编辑器onChange方法中无法获取到界面其他state最新值

Open Pmantou opened this issue 1 year ago • 0 comments

src/components/Editor.tsx路径文件内有一段赋值逻辑,只有editor未创建时才会执行,导致onChange函数之后无法更新,使用的一直是初始创建时组件内的值,相当于闭包了。从而在使用时,页面只能拿到初次创建编辑器时页面状态下的state,内部createEditor时能否给onChange包一层ref?

  useEffect(() => {
    if (ref.current == null) return
    if (editor != null) return
    // 防止重复渲染 当编辑器已经创建就不在创建了
    if (ref.current?.getAttribute('data-w-e-textarea')) return

    const newEditor = createEditor({
      selector: ref.current,
      config: {
        ...defaultConfig,
        onCreated: handleCreated,
        onChange: handleChanged,
        onDestroyed: handleDestroyed,
      },
      content: defaultContent,
      html: defaultHtml || value,
      mode,
    })
    setEditor(newEditor)
  }, [editor])

大家有遇到的可以用ahooks中的useMemoizedFn包一下传递给onChange的参数

Pmantou avatar Jun 21 '24 12:06 Pmantou