react-plotly.js icon indicating copy to clipboard operation
react-plotly.js copied to clipboard

Custom ModeBar Button click uses old state values

Open joshuakoh1 opened this issue 2 years ago • 2 comments

modeBarButtonsToAdd: [{ name: 'customDownload', title: 'Download', icon: Plotly.Icons.disk, click: (div) => {console.log(div); console.log(chartState); console.log(chartData); } }

chartState is the Plotly state variable passed to the Plot element (data, layout, frames, config) chartData is the raw data source that is used to generate the data traces for chartState chartData gets updated during app cycle which updates chartState All of the data outputs from click event are logged as point of init which has an empty data array, even the chart div that gets passed from the click function

joshuakoh1 avatar May 22 '22 11:05 joshuakoh1

I have the same issue.

floriancargoet avatar Mar 10 '23 10:03 floriancargoet

My workaround is to use a react ref which I always update. In the click handler, we have access to the latest value with ref.current.

floriancargoet avatar Mar 10 '23 10:03 floriancargoet