1
- import React , { useState , useRef , useImperativeHandle , useEffect } from "react" ;
1
+ import React , {
2
+ useState ,
3
+ useRef ,
4
+ useImperativeHandle ,
5
+ useEffect ,
6
+ useCallback ,
7
+ } from "react" ;
2
8
3
9
import { Line } from "react-chartjs-2" ;
4
10
@@ -124,9 +130,21 @@ function _Chart(
124
130
} ,
125
131
} ) ;
126
132
133
+ const enableTooltips = useCallback (
134
+ ( newState : boolean ) => {
135
+ ( opts . plugins as any ) . tooltip . enabled = newState ;
136
+ opts . datasets ! . line ! . pointHoverRadius = newState ? 3 : 0 ;
137
+ setOpts ( opts ) ;
138
+ chartRef . current ?. update ( ) ;
139
+ } ,
140
+ [ opts ]
141
+ ) ;
142
+
127
143
useEffect ( ( ) => {
128
144
if ( ! config . connected ) {
129
145
setConnected ( false ) ;
146
+ // when disconnected, force tooltips to be enabled
147
+ enableTooltips ( true ) ;
130
148
return ;
131
149
}
132
150
@@ -135,8 +153,11 @@ function _Chart(
135
153
// cleanup buffer state
136
154
worker . postMessage ( { command : "cleanup" } ) ;
137
155
setConnected ( true ) ;
156
+
157
+ // restore the tooltips state (which match the pause state when connected)
158
+ enableTooltips ( pause ) ;
138
159
}
139
- } , [ config . connected , connected ] ) ;
160
+ } , [ config . connected , connected , pause , enableTooltips ] ) ;
140
161
141
162
const togglePause = ( newState : boolean ) => {
142
163
if ( newState === pause ) {
@@ -148,9 +169,6 @@ function _Chart(
148
169
setPause ( newState ) ;
149
170
worker . postMessage ( { command : "cleanup" } ) ;
150
171
enableTooltips ( newState ) ;
151
- ( opts . plugins as any ) . tooltip . enabled = newState ;
152
- opts . datasets ! . line ! . pointHoverRadius = newState ? 3 : 0 ;
153
- setOpts ( opts ) ;
154
172
} ;
155
173
156
174
const setInterpolate = ( interpolate : boolean ) => {
0 commit comments