Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pmantou opened this issue Jun 21, 2024 · 0 comments
Open

Comments

@Pmantou
Copy link

Pmantou commented Jun 21, 2024

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的参数

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant