Skip to content

Commit 73f8a24

Browse files
danielbariongabrieljablonski
authored andcommitted
refactor: improve tooltip mounted code check
1 parent c5f0229 commit 73f8a24

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState, useRef } from 'react'
1+
import { useEffect, useState, useRef, useLayoutEffect } from 'react'
22
import classNames from 'classnames'
33
import debounce from 'utils/debounce'
44
import { TooltipContent } from 'components/TooltipContent'
@@ -50,6 +50,19 @@ const Tooltip = ({
5050
const { anchorRefs, setActiveAnchor: setProviderActiveAnchor } = useTooltip(id)
5151
const [activeAnchor, setActiveAnchor] = useState<React.RefObject<HTMLElement>>({ current: null })
5252
const hoveringTooltip = useRef(false)
53+
const mounted = useRef(false)
54+
55+
/**
56+
* useLayoutEffect runs before useEffect,
57+
* but should be used carefully because of caveats
58+
* https://beta.reactjs.org/reference/react/useLayoutEffect#caveats
59+
*/
60+
useLayoutEffect(() => {
61+
mounted.current = true
62+
return () => {
63+
mounted.current = false
64+
}
65+
}, [])
5366

5467
useEffect(() => {
5568
if (!show) {
@@ -58,12 +71,20 @@ const Tooltip = ({
5871
}, [show])
5972

6073
const handleShow = (value: boolean) => {
74+
if (!mounted.current) {
75+
return
76+
}
77+
6178
setRendered(true)
6279
/**
6380
* wait for the component to render and calculate position
6481
* before actually showing
6582
*/
6683
setTimeout(() => {
84+
if (!mounted.current) {
85+
return
86+
}
87+
6788
setIsOpen?.(value)
6889
if (isOpen === undefined) {
6990
setShow(value)
@@ -345,7 +366,7 @@ const Tooltip = ({
345366
if (position) {
346367
// if `position` is set, override regular and `float` positioning
347368
handleTooltipPosition(position)
348-
return () => null
369+
return
349370
}
350371

351372
if (float) {
@@ -360,15 +381,14 @@ const Tooltip = ({
360381
handleTooltipPosition(lastFloatPosition.current)
361382
}
362383
// if `float` is set, override regular positioning
363-
return () => null
384+
return
364385
}
365386

366387
let elementReference = activeAnchor.current
367388
if (anchorId) {
368389
// `anchorId` element takes precedence
369390
elementReference = document.querySelector(`[id='${anchorId}']`) as HTMLElement
370391
}
371-
let mounted = true
372392
computeTooltipPosition({
373393
place,
374394
offset,
@@ -378,7 +398,7 @@ const Tooltip = ({
378398
strategy: positionStrategy,
379399
middlewares,
380400
}).then((computedStylesData) => {
381-
if (!mounted) {
401+
if (!mounted.current) {
382402
// invalidate computed positions after remount
383403
return
384404
}
@@ -389,9 +409,6 @@ const Tooltip = ({
389409
setInlineArrowStyles(computedStylesData.tooltipArrowStyles)
390410
}
391411
})
392-
return () => {
393-
mounted = false
394-
}
395412
}, [show, anchorId, activeAnchor, content, html, place, offset, positionStrategy, position])
396413

397414
useEffect(() => {

0 commit comments

Comments
 (0)