nhn / tui.image-editor

🍞🎨 Full-featured photo image editor using canvas. It is really easy, and it comes with great filters.
http://ui.toast.com/tui-image-editor
MIT License
6.83k stars 1.26k forks source link

presumed: Strict mode react issue on load #893

Open marc0n3 opened 8 months ago

marc0n3 commented 8 months ago

Describe the bug I've developed a typescript wrapper for the component. I suspect that, because strict mode react invoke effects twice, the initialization process of TuiImageEditor fails.

I've an error on
this._invoker.on(EXECUTE_COMMAND, function (command) { return _this2.ui.fire(EXECUTE_COMMAND, command); });

when image is first loaded. Ui is null.

I suppose that destroy is resetting it.

To Reproduce `import React, {useEffect, useRef} from "react"; import TuiImageEditor from "tui-image-editor"; import "tui-image-editor/dist/tui-image-editor.css"; import "tui-color-picker/dist/tui-color-picker.css";

import "./imageEditor.scss";

export const ImageEditor = ({imageDataUrl}: { imageDataUrl: string }) => { const containerRef = useRef(null); const imageEditorRef = useRef<TuiImageEditor | null>(null);

useEffect(() => {
    const containerElement = containerRef.current;
    if (!containerElement) throw new Error("invalid ref");
    const editorInstance = imageEditorRef.current = new TuiImageEditor(containerElement, {
        usageStatistics: false,
        includeUI: {
            loadImage: {
                path: imageDataUrl,
                name: "image",
            },
            menu: ["shape", "filter", "text"],
            initMenu: "filter",
            uiSize: {
                width: "100%",
                height: "100%",
            },
            menuBarPosition: "left",

        },
        cssMaxWidth: 700,
        cssMaxHeight: 500,
        selectionStyle: {
            cornerSize: 20,
            rotatingPointOffset: 70
        },

    });
    /*/!**
     * load image
     *!/
    editorInstance.on("ready",()=>editorInstance.loadImageFromURL(imageDataUrl,"Image"));*/

    /*  const resizeObserver = new ResizeObserver(() => {
          const editorInstance = imageEditorRef.current;
          if (editorInstance && containerElement)
              editorInstance.resizeCanvasDimension({
                  width: containerElement.clientWidth,
                  height: containerElement.clientHeight
              });
      });*/

    return () => {
        editorInstance.destroy();
        imageEditorRef.current = null;
    };
}, []);

return <div className={"image-editor"} ref={containerRef}/>;

};`

Expected behavior I expect initialization to stop on destroyed components.

Arvin8613 commented 2 months ago

I also encountered the same bug with strict react mode. :(

socnacom commented 2 months ago

Hi ! did you find a reasonable workaround ? I have to disable StrictMode every time I want to work with image editor, and it's not really satisfying. See you