【问题标题】:How to solve uncaught typeerror: cannot read property 'getDisplayList' of null如何解决未捕获的类型错误:无法读取 null 的属性“getDisplayList”
【发布时间】:2021-09-28 08:14:29
【问题描述】:

看到与以下相同的错误: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} />

【问题讨论】:

    标签: reactjs typescript charts echarts


    【解决方案1】:

    我假设你正在使用 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
    }
    

    【讨论】:

    • 谢谢!我试过了,它有效。我还刚刚更新了问题,以包括使用useMemo 的可能性(这是我在看到你的答案之前首先尝试过的——尽管我认为你的答案对useCallback 更有意义)。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2022-01-19
    • 2021-12-01
    • 2022-07-02
    • 2022-06-14
    • 2022-10-23
    • 2023-03-05
    相关资源
    最近更新 更多