rwbrockhoff/chartjsdemo

Reusing Chart object

Closed this issue · 1 comments

Hi, I found your article about wrapping ChartJS into a React component and I have a question: why do you recreate Chart object in componentDidUpdate method here instead of caching Chart instance and calling update method on it whenever data changes? Was there any specific reason for that?

Hi there! I apologize for the delay. Yes, the update method is preferred if it works for you. I was bringing in dynamic data and changing my chart based on props. I found that the chart would glitch randomly to my previous dataset, and I was able to resolve the issue by recreating the chart instead. I imagine update is a less expensive operation, so use it if you're not experiencing any glitches in your chart ui on state/prop changes.