hustcc / echarts-for-react

⛳️ Apache ECharts components for React wrapper. 一个简单的 Apache echarts 的 React 封装。
https://git.hust.cc/echarts-for-react
MIT License
4.56k stars 633 forks source link

Get `Uncaught TypeError: Cannot read property 'getDisplayList' of null` warning #366

Closed baurine closed 3 years ago

baurine commented 4 years ago

Hi,

After adding the onEvents prop to ReactEchartsCore component, I always get the Uncaught TypeError: Cannot read property 'getDisplayList' of null warning in the browser console when clicking the legend. I tried but failed to find the root cause, do you have any idea? Thanks in advance.

The core code:

  const [selectedLegends, setSelectedLegends] = useState({})

  const handleLegendChange = ({ name, selected }) => {
    setSelectedLegends(selected)
  }

  const opt = useMemo(() => {
    ...
    legend: {
      ...
      selected: selectedLegends
    ...
  )

      <ReactEchartsCore
        echarts={echarts}
        notMerge={true}
        lazyUpdate={true}
        style={{ height: HEIGHT }}
        option={opt}
        theme={'light'}
        onEvents={{ legendselectchanged: handleLegendChange }}
      />

The details are in here: https://github.com/pingcap-incubator/tidb-dashboard/pull/663/commits/886875f3e87b2e2af01dab968c561a0f57c25d13 , related PR: https://github.com/pingcap-incubator/tidb-dashboard/pull/663

The warning screenshot:

image

baurine commented 4 years ago

请问您解决了吗?我也遇到了相同问题

Nope. But I found change notMerge props to its default value false can help me remove the onEvents props in the above case.

xiyanfeixu commented 4 years ago

I have changed notMerge to false ,but still have warnings like the above case.

findxc commented 3 years ago

I find that if you call setState in onEvents listener, the error occurs. Maybe the error has something to do with the React re-render.

const onLegendSelected = () => {
  // the setCount triggers React re-render, when you click lengend, the error occurs
  setCount(c => c + 1);
};

const onEvents = {
  legendselectchanged: onLegendSelected,
};

version: echarts: 4, echarts-for-react: ^2.0.16

dolimovkhumoyun commented 3 years ago

Has anyone found solution for this issue? @hustcc

hustcc commented 3 years ago

Can someone give me a live demo?

findxc commented 3 years ago

@hustcc https://codesandbox.io/s/echarts-for-react-issues-366-ru14m

hustcc commented 3 years ago

@findxc in FC, set onLegendSelected in memo, keep the function with same ref.

findxc commented 3 years ago

I have updated my demo https://codesandbox.io/s/echarts-for-react-issues-366-ru14m , and it can work now ✌️✌️✌️. The key point is to wrap your event listeners with useMemo or useCallback.

const onLegendSelected = useCallback((params) => {
  const { selected } = params
  setSelected(selected)
}, [])

Many thanks to hustcc 👍👍👍