Skip to content

Commit 2761e40

Browse files
authored
Merge pull request #7614 from google/enhancement/7544-follow-up
Fix tooltip view double trigger (Follow Up).
2 parents 7cc244c + a4afa5d commit 2761e40

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

assets/js/components/KeyMetrics/KeyMetricsSetupCTAWidget.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
* External dependencies
2121
*/
2222
import PropTypes from 'prop-types';
23+
import { useMount } from 'react-use';
2324

2425
/**
2526
* WordPress dependencies
2627
*/
2728
import { __ } from '@wordpress/i18n';
28-
import { Fragment, useCallback, useEffect, useState } from '@wordpress/element';
29+
import { Fragment, useCallback } from '@wordpress/element';
2930

3031
/**
3132
* Internal dependencies
@@ -113,14 +114,14 @@ function KeyMetricsSetupCTAWidget( { Widget, WidgetNull } ) {
113114
);
114115
}, [ viewContext ] );
115116

116-
const [ hasBeenInView, setHasBeenInView ] = useState( false );
117-
118-
useEffect( () => {
119-
if ( isTooltipVisible && ! hasBeenInView ) {
117+
useMount( () => {
118+
// Since components are conditionally rendered, when tooltip
119+
// appears, old component will unmount and new componnet will mount,
120+
// with tooltip visible equal to true, so here we ensure event is sent only once when that occurs,
121+
if ( isTooltipVisible ) {
120122
trackEvent( `${ viewContext }_kmw`, 'tooltip_view' );
121-
setHasBeenInView( true );
122123
}
123-
}, [ isTooltipVisible, viewContext, hasBeenInView, setHasBeenInView ] );
124+
} );
124125

125126
if ( isTooltipVisible ) {
126127
return (

0 commit comments

Comments
 (0)