hustcc / echarts-for-react

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

Avoid data reloading onEvents #285

Closed MarcoCadei closed 5 years ago

MarcoCadei commented 5 years ago

Hi guys, thank you for the amazing library! I just have one problem: when i set an event with the onEvents prop the chart reloads the data! How can i avoid that?

Thank you in advance

alexPang6 commented 5 years ago

Notice how with onClick={() => alert('click')}, we’re passing a function as the onClick prop. React will only call this function after a click. Forgetting () => and writing onClick={alert('click')} is a common mistake, and would fire the alert every time the component re-renders.

alexPang6 commented 5 years ago
onEvents={{
  click: params => {
    console.log(params);
  }
}}

change to:

    if (
      prevProps.theme !== this.props.theme ||
      !isEqual(prevProps.opts, this.props.opts) ||
      !isEqual(prevProps.onEvents, this.props.onEvents)
    ) {
      this.dispose();

      this.rerender(); // 重建
      return;
    }

https://github.com/hustcc/echarts-for-react/issues/286

KennyDivert commented 2 years ago
onEvents={{
  click: params => {
  console.log(params);
  }
}}

change to:

    if (
      prevProps.theme !== this.props.theme ||
      !isEqual(prevProps.opts, this.props.opts) ||
      !isEqual(prevProps.onEvents, this.props.onEvents)
    ) {
      this.dispose();

      this.rerender(); // 重建
      return;
    }

286

I'm sorry, what exactly do you mean by change to?

naman13924 commented 2 years ago

Can some one solve this issue as onEvents is re rendering all the charts in my application and creating a performance issue

ct-lynch commented 4 months ago

I found a possible work around. Don't use onEvents, instead use the ref option, combined with the getEchartsInstance().

const chartRef = useRef(null);

  useEffect(() => {
    let instance = chartRef.current.getEchartsInstance();
    instance.on('datazoom',  () => {console.log('no reload')} )  ;
  }, []);

return (
<ReactEChartsCore
                  echarts={echarts}
                  option={eChartOption}
                  ref={chartRef}>
)