[Bug] Why the function "cleanup" in useEffect has been called when i init charts?
See original GitHub issueVersion
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.
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:
- Created a year ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Please raise your concern to react, this is not echarts bug
I got it. Thanks again for your help and patience.