question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Why the function "cleanup" in useEffect has been called when i init charts?

See original GitHub issue

Version

5.3.2

Link to Minimal Reproduction

No response

Steps to Reproduce

This is my code:

import * as echarts from "echarts";
import { useEffect, useRef } from "react";

function MyChart(props) {
  const chartRef = useRef();

  useEffect(() => {
    if (chartRef.current) {
      let myChart = echarts.getInstanceByDom(chartRef.current);

      const resize = function () {
        console.log("resize -> ", myChart);
        myChart.resize();
      };

      if (!myChart) {
        console.log("init");
        myChart = echarts.init(chartRef.current);
        window.addEventListener("resize", resize);
      }
      myChart.setOption(props.option);

      return function cleanup() {
        console.log("cleanup");
        window.removeEventListener("resize", resize);
      };
    }
  }, [props.option]);

  return <div ref={chartRef} className="chart"></div>;
}

export default MyChart;

And i use this compnent like this:

import MyChart from "../../components/echarts/index";
import { useState } from "react";
import { Row, Col } from "antd";

function Home() {
  const [lineChartData, setLineChartData] = useState({...});
  const [barChartData, setBarChartData] = useState({...});
  const [pieChartData, setPieChartData] = useState({...});

  return (
    <div
      className="test-block"
      style={{
        padding: 24,
        minHeight: 360,
      }}
    >
      <Row>
        <Col span={8} order={1}>
          <MyChart option={lineChartData}></MyChart>
        </Col>
        <Col span={8} order={2}>
          <MyChart option={barChartData}></MyChart>
        </Col>
        <Col span={8} order={3}>
          <MyChart option={pieChartData}></MyChart>
        </Col>
      </Row>
    </div>
  );
}

export default Home;

Current Behavior

When i run my refresh the page, i see “init” and “cleanup” on my console.

And after i set a breakpoint at code let myChart = echarts.getInstanceByDom(chartRef.current) and debug, i find each chart has run this code twice. And in the first step, it print “init” on my “console”, and “cleanup” in the second step.

image

Besides, because the function, “cleanup” has been called, window.removeEventListener("resize", resize) has been called.

So, EventListener has been removed.

Expected Behavior

I expect there has no “cleanup” on my console, because i think the compnent has not been destroyed.

Environment

- OS:Windows10 21H1
- Browser:Chrome 101.0.4951.67
- Framework:React@18.1.0

Any additional comments?

My first issue, i’m sorry for my bad format.

I want to know why things display like this, and what the mistakes i made.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
susiwen8commented, Jun 10, 2022

Please raise your concern to react, this is not echarts bug

0reactions
xingyinsongcommented, Jun 10, 2022

Please raise your concern to react, this is not echarts bug

I got it. Thanks again for your help and patience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the cleanup function from `useEffect` called on every ...
React performs the cleanup when the dependencies to that hook changes and the effect hook needs to run again with new values. This...
Read more >
Understanding React's useEffect cleanup function
React's useEffect cleanup function saves applications from unwanted behaviors like memory leaks by cleaning up effects.
Read more >
React 18 - Avoiding Use Effect Getting Called Twice
This hook works to defeat the hook being called twice, but the cleanup functions will never get called as far as I can...
Read more >
Bug(17.0.0-rc.1): useEffect cleanup functions not running in ...
React version: 17.0.0-rc.1 Steps To Reproduce export default function App() { const [counter, setCounter] = useState(1); return ( {counter} ...
Read more >
Rules of React's useEffect - CoderPad
useEffect is prolific in React apps. Here are four rules associated with the hook and in-depth explanations of why they're important.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found