簡體   English   中英

在 NextJS 中使用動態導入時如何訪問 ReactQuill Ref?

[英]How to access ReactQuill Ref when using dynamic import in NextJS?

我遇到了一個有趣的問題。 我將 NextJS 用於其服務器端渲染功能,並使用 ReactQuill 作為我的富文本編輯器。 為了繞過 ReactQuill 與 DOM 的聯系,我動態地導入了它。 但是,這帶來了另一個問題,即當我嘗試將 ref 附加到 ReactQuill 組件時,它被視為可加載組件而不是 ReactQuill 組件。 我需要 ref 來自定義上傳到富文本編輯器時如何處理圖像。 現在,ref 返回 current:null 而不是我可以使用 .getEditor() on 自定義圖像處理的函數。

有人對我如何解決這個問題有任何想法嗎? 我嘗試了引用轉發,但它仍然將引用應用於可加載組件,而不是 React-Quill 組件。 這是我的代碼的快照。

const ReactQuill = dynamic(import('react-quill'), { ssr: false, loading: () => <p>Loading ...</p> }
);

const ForwardedRefComponent = React.forwardRef((props, ref) => {return (
    <ReactQuill {...props} forwardedRef={(el) => {ref = el;}} />
)})

class Create extends Component {
    constructor() {
        super();
        this.reactQuillRef = React.createRef();
    }

    imageHandler = () => {
         console.log(this.reactQuillRef); //this returns current:null, can't use getEditor() on it.
    }
    render() {
    const modules = {
      toolbar: {
          container:  [[{ 'header': [ 2, 3, false] }],
            ['bold', 'italic', 'underline', 'strike'],
            [{ 'list': 'ordered'}, { 'list': 'bullet' }],
            [{ 'script': 'sub'}, { 'script': 'super' }],
            ['link', 'image'],
            [{ 'indent': '-1'}, { 'indent': '+1' }],    
            [{ 'align': [] }],
            ['blockquote', 'code-block'],],
          handlers: {
             'image': this.imageHandler
          }
        }
     };
         return(
             <ForwardedRefComponent 
                value={this.state.text}
                onChange={this.handleChange}
                modules={modules}
                ref={this.reactQuillRef}/> //this.reactQuillRef is returning current:null instead of the ReactQuill function for me to use .getEditor() on
         )
    }
}

const mapStateToProps = state => ({
    tutorial: state.tutorial,
});

export default connect(
    mapStateToProps, {createTutorial}
)(Create);

在 NextJS 中,React.useRef 或 React.createRef 不適用於動態導入。

你應該更換

const ReactQuill = dynamic(import('react-quill'), { ssr: false, loading: () => <p>Loading ...</p> }
);

import ReactQuill from 'react-quill';

並在加載window后渲染。

import ReactQuill from 'react-quill';
class Create extends Component {
    constructor() {
        super();
        this.reactQuillRef = React.createRef();
        this.state = {isWindowLoaded: false};
    }
    componentDidMount() {
        this.setState({...this.state, isWindowLoaded: true});
    }

    .........
    .........

   render(){
     return (
       <div>
         {this.isWindowLoaded && <ReactQuil {...this.props}/>}
       </div>
     )
   }

}

使用 onChange 並傳遞所有參數,這里有一個使用 editor.getHTML() 的示例


import React, { Component } from 'react'
import dynamic from 'next/dynamic'
import { render } from 'react-dom'

const QuillNoSSRWrapper = dynamic(import('react-quill'), {
  ssr: false,
  loading: () => <p>Loading ...</p>,
})

const modules = {
  toolbar: [
    [{ header: '1' }, { header: '2' }, { font: [] }],
    [{ size: [] }],
    ['bold', 'italic', 'underline', 'strike', 'blockquote'],
    [
      { list: 'ordered' },
      { list: 'bullet' },
      { indent: '-1' },
      { indent: '+1' },
    ],
    ['link', 'image', 'video'],
    ['clean'],
  ],
  clipboard: {
    // toggle to add extra line breaks when pasting HTML:
    matchVisual: false,
  },
}
/*
 * Quill editor formats
 * See https://quilljs.com/docs/formats/
 */
const formats = [
  'header',
  'font',
  'size',
  'bold',
  'italic',
  'underline',
  'strike',
  'blockquote',
  'list',
  'bullet',
  'indent',
  'link',
  'image',
  'video',
]

class BlogEditor extends Component {
  constructor(props) {
    super(props)
    this.state = { value: null } // You can also pass a Quill Delta here
    this.handleChange = this.handleChange.bind(this)
    this.editor = React.createRef()
  }

  handleChange = (content, delta, source, editor) => {
    this.setState({ value: editor.getHTML() })
  }

  render() {
    return (
      <>
        <div dangerouslySetInnerHTML={{ __html: this.state.value }} />
        <QuillNoSSRWrapper ref={this.editor} onChange={this.handleChange} modules={modules} formats={formats} theme="snow" />
        <QuillNoSSRWrapper value={this.state.value} modules={modules} formats={formats} theme="snow" />
      </>
    )
  }
}
export default BlogEditor

如果你想在 Next.js 中使用 ref 和動態導入

你可以使用React.forwardRef API

更多信息

我分享我的解決方案,希望它對你也有幫助。

來自https://github.com/zenomaro/react-quill/issues/642#issuecomment-717661518的幫助

 const ReactQuill = dynamic( async () => { const { default: RQ } = await import("react-quill"); return ({ forwardedRef, ...props }) => <RQ ref={forwardedRef} {...props} />; }, { ssr: false } ); export default function QuillWrapper() { const quillRef = React.useRef(false) return <> <ReactQuill forwardedRef={quillRef} /> </> }

例如,您可以使用 ref 使用自定義處理程序上傳圖像

 const ReactQuill = dynamic( async () => { const { default: RQ } = await import("react-quill"); return ({ forwardedRef, ...props }) => <RQ ref={forwardedRef} {...props} />; }, { ssr: false } ); export default function QuillWrapper({ value, onChange, ...props }) { const quillRef = React.useRef(false) // Custom image upload handler function imgHandler() { // from https://github.com/quilljs/quill/issues/1089#issuecomment-318066471 const quill = quillRef.current.getEditor() let fileInput = quill.root.querySelector('input.ql-image[type=file]'); // to prevent duplicate initialization I guess if (fileInput === null) { fileInput = document.createElement('input'); fileInput.setAttribute('type', 'file'); fileInput.setAttribute('accept', 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon'); fileInput.classList.add('ql-image'); fileInput.addEventListener('change', () => { const files = fileInput.files; const range = quill.getSelection(true); if (!files || !files.length) { console.log('No files selected'); return; } const formData = new FormData(); formData.append('file', files[0]); formData.append('uid', uid) formData.append('img_type', 'detail') quill.enable(false); axios .post('the/url/for/handle/uploading', formData) .then(response => { // after uploading succeed add img tag in the editor. // for detail visit https://quilljs.com/docs/api/#editor quill.enable(true); quill.insertEmbed(range.index, 'image', response.data.url); quill.setSelection(range.index + 1); fileInput.value = ''; }) .catch(error => { console.log('quill image upload failed'); console.log(error); quill.enable(true); }); }); quill.root.appendChild(fileInput); } fileInput.click(); } // I don't know much about useMemo // but if i don't use the hook, // the editor keeps rerendered resulting in losing focus and I guess perfomance trouble too. const modules = useMemo(() => ({ toolbar: { container: [ [{ font: [] }], [{ size: ["small", false, "large", "huge"] }], // custom dropdown ["bold", "italic", "underline", "strike"], // toggled buttons [{ color: [] }, { background: [] }], // dropdown with defaults from theme [{ script: "sub" }, { script: "super" }], // superscript/subscript [{ header: 1 }, { header: 2 }], // custom button values ["blockquote", "code-block"], [{ list: "ordered" }, { list: "bullet" }], [{ indent: "-1" }, { indent: "+1" }], // outdent/indent [{ direction: "rtl" }], // text direction [{ align: [] }], ["link", "image"], ["clean"], // remove formatting button ], handlers: { image: imgHandler } // Custom image handler } }), []) return <ReactQuill forwardedRef={quillRef} modules={modules} value={value} onChange={onChange} {...props}/> }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM