-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Description
After updating from 1.4.0 to the latest version (1.4.2), attaching event handlers directly to the ZRender instance using getZr() throws an error when trying to cleanup on unmount.
Previous Behavior
useEffect(() => {
echartsInstance?.getZr().on('mousedown', () => {
console.log('=> mousedown')
})
return () => {
echartsInstance?.getZr().off('mousedown')
}
}, [echartsInstance])This code worked as expected.
Current Behavior
Now throws error:
Uncaught TypeError: Cannot read properties of null(reading 'off')Root Cause
This might be related to the new event cleanup implementation in event-handlers.ts, causing the ZRender instance to become temporarily invalid during the cleanup phase.
Solution
While the previous implementation worked on the version 1.4.0, a more defensive approach should be used by storing and validating the ZRender reference before using it:
useEffect(() => {
const zr = echartsInstance?.getZr()
if (!zr) return
zr.on('mousedown', () => setShowTooltip(true))
return () => zr.off('mousedown')
}, [echartsInstance])Link to Reproduction
https://stackblitz.com/edit/vitejs-vite-fndic4en?file=src%2Fcomponents%2FChart.tsx
Steps to reproduce
- Navigate to a page with the ECharts component
- Navigate away from this page
- You will see a blank page (Check the console for the error)
JS Framework
React (TS)
Version
1.4.2
Browser
Google Chrome 132.0.6834.160
Operating System
- macOS
- Windows
- Linux
Additional Information
This might not be considered an actual bug but I wanted to share it anyway so that if anybody goes through the same issue they will have a solution.