Skip to content

React quill in nextjs and typescript custom image handler issue? #849

@siamahnaf

Description

@siamahnaf

I am try to use react-quill in my typescript nextjs project. Here I am finding typing and ref issue that I can't solve. Please help me. Here is code example-

import React, { useState, useRef } from 'react';
import dynamic from 'next/dynamic';
import { Container } from "@mui/material";
const ReactQuill = dynamic(import('react-quill'), {
    ssr: false,
    loading: () => <p>Loading ...</p>,
})
import 'react-quill/dist/quill.snow.css';


const Editor = () => {
    const [value, setValue] = useState('');
    const quillRef = useRef<any>();

    const imageHandler = async () => {
        const input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.setAttribute('accept', 'image/*');
        input.click();
        input.onchange = async () => {
            var file: any = input && input.files ? input.files[0] : null;
            var formData = new FormData();
            formData.append("file", file);
            let quillObj = quillRef.current.getEditor();

        };
    }

    const modules = {
        toolbar: {
            container: [
                [{ font: [] }, { 'size': [] }, { 'header': [1, 2, 3, 4, 5, 6] }],
                ['bold', 'italic', 'underline', 'strike'],
                [{ 'color': [] }, { 'background': [] }],
                [{ 'script': 'sub' }, { 'script': 'super' }],
                [{ 'header': 1 }, { 'header': 2 }, 'blockquote', 'code-block'],
                [
                    { list: 'ordered' },
                    { list: 'bullet' },
                    { indent: '-1' },
                    { indent: '+1' },
                ],
                [{ 'direction': 'rtl' }, { 'align': [] }],
                ['link', 'image', 'clean'],
            ],
            'handlers': {
                image: imageHandler
            }
        }
    }
    return (
        <Container maxWidth="xxxl" disableGutters>
            <ReactQuill
                ref={quillRef} // Here I am finding an issue.
                value={value}
                modules={modules}
                onChange={setValue}
                placeholder="Start typing!"
            />
        </Container>
    );
};

export default Editor;

Screenshot 2022-10-26 133941

I am try to use react-quill in my typescript nextjs project. Here I am finding typing and ref issue that I can't solve. Please help me.

Here is code example-

import React, { useState, useRef } from 'react';
import dynamic from 'next/dynamic';
import { Container } from "@mui/material";
const ReactQuill = dynamic(import('react-quill'), {
ssr: false,
loading: () =>

Loading ...

,
})
import 'react-quill/dist/quill.snow.css';

const Editor = () => {
const [value, setValue] = useState('');
const quillRef = useRef();

const imageHandler = async () => {
    const input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');
    input.click();
    input.onchange = async () => {
        var file: any = input && input.files ? input.files[0] : null;
        var formData = new FormData();
        formData.append("file", file);
        let quillObj = quillRef.current.getEditor();

    };
}

const modules = {
    toolbar: {
        container: [
            [{ font: [] }, { 'size': [] }, { 'header': [1, 2, 3, 4, 5, 6] }],
            ['bold', 'italic', 'underline', 'strike'],
            [{ 'color': [] }, { 'background': [] }],
            [{ 'script': 'sub' }, { 'script': 'super' }],
            [{ 'header': 1 }, { 'header': 2 }, 'blockquote', 'code-block'],
            [
                { list: 'ordered' },
                { list: 'bullet' },
                { indent: '-1' },
                { indent: '+1' },
            ],
            [{ 'direction': 'rtl' }, { 'align': [] }],
            ['link', 'image', 'clean'],
        ],
        'handlers': {
            image: imageHandler
        }
    }
}
return (
    <Container maxWidth="xxxl" disableGutters>
        <ReactQuill
            ref={quillRef} // Here I am finding an issue.
            value={value}
            modules={modules}
            onChange={setValue}
            placeholder="Start typing!"
        />
    </Container>
);

};

export default Editor;

enter image description here

Here is CodeSandBox-

https://codesandbox.io/s/still-hill-xkb1pj

Can any one give me a proper typescript solutions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions