V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
withoutxx

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

  •  
  •   withoutxx · May 14, 2020 · 1433 views
    This topic created in 2185 days ago, the information mentioned may be changed or developed.
    • 目前想把富文本组件的方法暴露给父组件调用,怎么才可以像 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;
    
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1112 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 18:25 · PVG 02:25 · LAX 11:25 · JFK 14:25
    ♥ Do have faith in what you're doing.