G icon indicating copy to clipboard operation
G copied to clipboard

react严格模式下webglRender初始化时会报错

Open zhe-he opened this issue 5 months ago • 1 comments

Steps To Reproduce

  1. 初始化一个画布
  2. 创建一个圆
  3. 把圆添加进画布
import { Canvas, CanvasEvent, Circle } from "@antv/g";
import { Renderer } from "@antv/g-webgl";
import { useEffect, useRef } from "react";

export function Test() {
    const ref = useRef<HTMLDivElement>(null);
    useEffect(() => {
        if (!ref.current) return;
        const renderer = new Renderer();
        const canvas = new Canvas({
            container: ref.current,
            width: 600,
            height: 500,
            renderer,
        });
        const circle = new Circle({
            style: {
                r: 50,
                fill: "#1890FF",
                stroke: "#F04864",
                lineWidth: 4,
                cursor: "pointer",
            },
        });

        const fn = () => {
            canvas.appendChild(circle);
        };
        canvas.addEventListener(CanvasEvent.READY, fn);
        return () => {
            canvas.removeEventListener(CanvasEvent.READY, fn);
            canvas.destroy();
        };
    }, []);

    return <div ref={ref}></div>;
}

如果关闭react的严格模式不会报错,打开会报错, 报错信息如下: Error handled by React Router default ErrorBoundary: TypeError: Cannot read properties of undefined (reading 'destroy') at RenderGraphPlugin.ts:277:19 at SyncHook.ts:15:16 at Array.forEach () at SyncHook2.call (SyncHook.ts:13:20) at RenderingService2.destroy (RenderingService.ts:332:24) at Canvas2.destroy (Canvas.ts:387:32) at cleanExistedCanvas (canvas.ts:18:52) at new Canvas2 (Canvas.ts:198:7) at index.tsx?t=1761104888817:12:24 at Object.react_stack_bottom_frame (react-dom-client.development.js:25989:20)

zhe-he avatar Oct 22 '25 03:10 zhe-he

Based on the provided bug report, here's the analysis:

Title: react严格模式下webglRender初始化时会报错

Body:

Steps To Reproduce:

  1. Initialize a canvas.
  2. Create a circle.
  3. Add the circle to the canvas.
import { Canvas, CanvasEvent, Circle } from "@antv/g";
import { Renderer } from "@antv/g-webgl";
import { useEffect, useRef } from "react";

export function Test() {
    const ref = useRef<HTMLDivElement>(null);
    useEffect(() => {
        if (!ref.current) return;
        const renderer = new Renderer();
        const

github-actions[bot] avatar Oct 22 '25 03:10 github-actions[bot]