V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
withoutxx
V2EX  ›  前端开发

怎么使用 React hook 二次封装一个富文本组件

  •  
  •   withoutxx · 2020-05-14 15:35:12 +08:00 · 1017 次点击
    这是一个创建于 1459 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 目前想把富文本组件的方法暴露给父组件调用,怎么才可以像 antd Form 组件那样直接暴露出去一个 useForm

    • 另外想找一款粘贴时支持保留大部分样式的富文本编辑器 (从 word 复制或者从 135 类似的编辑器复制过来保留样式),目前测试下来只有 wangeditor 设置 pasteFilterStyle = false 可以支持

    //想实现的调用方式
    
    const {setContent} = Editor.useEditor()
    
    <Editor options={} />
    
    
    //当前的写法,父组件只能通过 ref 调用
    
    const Editor = React.forwardRef((props, ref) => {
    
      const eleRef = React.createRef();
      const editorRef = React.createRef();
    
      useEffect(() => {
        editorRef.current = createEditor(eleRef.current);
      }, [eleRef.current]);
    
      useImperativeHandle(ref, () => {
        return {
          setContent: editorRef.current.setContent,
        };
      });
    
      return (
        <div ref={eleRef}>
    
        </div>
      );
    });
    
    export default Editor;
    
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2751 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:18 · PVG 19:18 · LAX 04:18 · JFK 07:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.