2

I am using [email protected], [email protected] and [email protected].

I am trying to add custom text on button click but I didn't get editor ref. Can you please help me.

import dynamic from 'next/dynamic'
const ReactQuill = dynamic(() => import('react-quill'), {
    ssr: false,
})


const insertText = (quillRef) => () => {
    console.log('Ref', quillRef)
    var range = quillRef.getSelection()
    let position = range ? range.index : 0
    quillRef.insertText(position, 'Hello, World! ')
}

const editorRef = useRef()

<Button onClick={insertText(editorRef.current)}>
    Add Text
</Button>
                                                
<ReactQuill
    ref={editorRef}
    preserveWhitespace={true}
    value={value}
    onChange={handleChange}
/>
2
  • @juliomalves, Sorry, I forgot to add 'editorRef.current' into stackoverflow. I updated my code. in editorRef.current I get retry function instead of editor ref. Commented Jan 11, 2022 at 14:28
  • @juliomalves insertText method is not available. only get. Commented Jan 11, 2022 at 14:40

2 Answers 2

3

In case you haven't solved this issue, I had the same problem but found this issue on GitHub suggesting to replace the dynamic import with the following:

const ReactQuill = typeof window === 'object' ? require('react-quill') : () => false;

1

Here is my solution searching all day on Google. And It works for me. hope it works for others.

import dynamic from "next/dynamic";
import "react-quill/dist/quill.snow.css";
import "react-quill/dist/quill.bubble.css";

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

// inside component
let reactQuillRef = useRef(null);
return (
       <ReactQuill
            forwardedRef={reactQuillRef}
            {...rest}
        />
);
1
  • Very helpful! Saved me loads of time! Thanks. Commented Jul 25, 2024 at 9:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.