首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react-hook-form中使用draft js-plugins

如何在react-hook-form中使用draft js-plugins
EN

Stack Overflow用户
提问于 2021-09-15 08:11:48
回答 1查看 424关注 0票数 1

我使用react-hook-form和draft.js作为所见即所得。我的代码中,我使用了draft js-plugins:

代码语言:javascript
复制
import React, {useRef} from "react";
import { Controller } from "./src";
import Editor from "@draft-js-plugins/editor";
import createToolbarPlugin from "@draft-js-plugins/static-toolbar";
import "draft-js/dist/Draft.css";
import "@draft-js-plugins/static-toolbar/lib/plugin.css";

const staticToolbarPlugin = createToolbarPlugin();
const { Toolbar } = staticToolbarPlugin;
const plugins = [staticToolbarPlugin];

function RichText({ control }) {
  const editor = useRef(null);
  return (
    <div
      style={{
        border: "1px solid #ccc",
        minHeight: 30,
        padding: 10
      }}
    >
      <Toolbar />
      <Controller
        ref={editor}
        name="DraftJS"
        control={control}
        plugins={plugins}
        render={({ value, onChange }) => {
          return <Editor editorState={value} onChange={onChange} />;
        }}
      />
    </div>
  );
}

export default RichText;

一切都好,我看到了插件。但是当我想要选择word并点击插件(I,B,U)按钮时,会显示错误

代码语言:javascript
复制
props.getEditorState is not a function

我不明白怎么解决这个问题?我发现了一些https://www.draft-js-plugins.com/plugin/static-toolbar .but没有帮助的例子

codesandbox上的实时示例

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-15 11:00:42

我认为您必须将<Toolbar />组件作为<Editor />组件的同级组件,并将ref传递给它。

代码语言:javascript
复制
<Controller
  name="DraftJS"
  control={control}
  render={({ value, onChange, ref }) => {
    return (
      <>
        <Toolbar />
        <Editor
          ref={ref}
          editorState={value}
          onChange={onChange}
          plugins={plugins}
        />
      </>
    );
  }}
/>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69189352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档