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.

Removing chart on click event produces errors

See original GitHub issue

I have a click event that causes my page to change the view. The problem is that when the chart is removed it produces errors for various reasons depending on use case.

For instance this fiddle

outputs Cannot read property 'currentStyle' of null when clicking on one of the bars.

I have another use case with custom tooltips that triggers Cannot read property 'removeHoverStyle' of null

I’m sure there just need to be some null/undefined checks but not sure where to start looking and was hoping someone could provide some guidance.

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aemccommented, Aug 1, 2019

My wrapper chart looks like this

function BarChart({ chartId, options }) {
  const classes = useStyles();
  const { t } = useTranslation();

  useEffect(() => {
    const myChart = new Chart(chartId, options); // instantiate a new chart
    return () => {
      console.log('destroying chart...', chartId);
      myChart.destroy(); // destroy chart on component unmount
    };
  });

  return (
    <canvas className={classes.chart} id={chartId} data-testid={chartId}>
      <p>{t('noCanvasSupport')}</p>
    </canvas>
  );
}

In my options object that I pass in, I have an onClick callback function that changes one of the parent’s state variables (a boolean). This boolean renders another chart if true and removes the current chart

Parent.js

...
return (
<>
    {isTrue && <Barchart />} // throws error as soon as isTrue becomes false
    {!isTrue && <Barchart />}
</>

I seem to be getting this error when isTrue is changed to false and the first Barchart is unmounted. The removeHoverStyle gets called for some reason @ryan-morris

I’ve also posted on stackoverflow: https://stackoverflow.com/questions/57314992/removing-chart-on-click-event-produces-removehoverstyle-of-null-error

0reactions
ryan-morriscommented, Jul 31, 2019

@aemc I’m not very familiar with react. Depending on how your chart is removed, you may be able to destroy the chart in the componentWillUnmount of its parent. The overall idea being to tap into an event available in react that lets you know just prior to it being removed from the DOM

Read more comments on GitHub >

github_iconTop Results From Across the Web

Removing chart on click event produces 'removeHoverStyle' of ...
I am on react 16.12 with react-chartjs-2 2.9.0 . I have found that the chart itself has the property "onElementsClick" that you can...
Read more >
Removing chart on click event produces 'removeHoverStyle' of ...
I am on react 16.12 with react-chartjs-2 2.9.0 . I have found that the chart itself has the property "onElementsClick" that you can...
Read more >
Add, change, or remove error bars in a chart - Microsoft Support
Click the Chart Elements button Chart Elements button next to the chart, and then check the Error Bars box. (Clear the box to...
Read more >
How to add error bars in Excel: standard and custom - Ablebits
Add error bars to your chart as usual. · Right-click any vertical error bar and choose Delete from the pop-up menu. Delete vertical...
Read more >
How to Write an Action Listener
In the above example, the event handler class is AL which implements ActionListener. We would like to handle the button-click event, so we...
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