How to solve uncaught typeerror: cannot read property 'getDisplayList' of null

How to solve uncaught typeerror: cannot read property 'getDisplayList' of null

看到与以下相同的错误:https://segmentfault.com/q/1010000022589791

使用Echarts的onEvents参数调用外部函数时出现错误。我该如何解决?

interface ParamsInterface {
    type: "datazoom";
    start: number;
    end: number;
}

interface TimeRange {
    start: number;
    end: number;
}

const [time, setTime] = useState<TimeRange>({start: 0, end: 100});

const onEvents = useMemo(
    () => ({
        dataZoom: (params: ParamsInterface) => {
            const newTime: TimeRange = {
                start: params.start,
                end: params.end,
            }
            setTime(newTime);
        }
    }), []
}

return <Echarts option={option} onEvents={onEvents} />

我假设你正在使用 echarts-for-react。

根据 this 的讨论,您应该避免直接从 onEvents 处理程序调用 setState。而是在外部定义处理程序并用 useCallback:

包装它
const onDataZoom = useCallback((params: ParamsInterface) => {
        const newTime: TimeRange = {
            start: params.start,
            end: params.end,
        }
        setTime(newTime);
    }, []);

const onEvents = {
    dataZoom: onDataZoom
}