Skip to content

Commit 4e294c1

Browse files
authored
Call setProps only if Tooltip component is not mounted (#1011)
Previously we were getting errors due to uncancelled timers not getting cleaned up properly when the component unmounted. This resulted in a delayed call to setProps and an error. Unfortunately cancelling the timeouts directly appears to be challenging, so this change introduces a simple `isMountedRef` which is used a guard on calls to setProps.
1 parent 19bbcff commit 4e294c1

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/private/Overlay.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ const Overlay = ({
5050
const targetRef = useRef(null);
5151
const hideTimeout = useRef(null);
5252
const showTimeout = useRef(null);
53+
const isMountedRef = useRef(false);
54+
// TODO: we use this as a guard to make sure we don't call setProps if the component
55+
// has unmounted. cancelling the timeouts directly would be neater but I haven't been
56+
// able to make it work.
57+
useEffect(() => {
58+
isMountedRef.current = true;
59+
return () => {
60+
isMountedRef.current = false;
61+
};
62+
});
5363

5464
// Triggers can be passed using space separated values
5565
const triggers = typeof trigger === 'string' ? trigger.split(' ') : [];
@@ -63,7 +73,7 @@ const Overlay = ({
6373
if (isOpenRef.current) {
6474
hideTimeout.current = clearTimeout(hideTimeout.current);
6575
setIsOpen(false);
66-
if (setProps) {
76+
if (setProps && isMountedRef.current) {
6777
setProps({is_open: false});
6878
}
6979
}
@@ -84,7 +94,7 @@ const Overlay = ({
8494
if (!isOpenRef.current) {
8595
showTimeout.current = clearTimeout(showTimeout.current);
8696
setIsOpen(true);
87-
if (setProps) {
97+
if (setProps && isMountedRef) {
8898
setProps({is_open: true});
8999
}
90100
}
@@ -148,8 +158,8 @@ const Overlay = ({
148158
}
149159
// Focus event trigger
150160
if (triggers.indexOf('focus') > -1) {
151-
t.addEventListener('focusin', show, true);
152-
t.addEventListener('focusout', hide, true);
161+
t.addEventListener('focusin', showWithDelay, true);
162+
t.addEventListener('focusout', hideWithDelay, true);
153163
}
154164
// Click or Legacy event trigger
155165
if (triggers.indexOf('click') > -1 || triggers.indexOf('legacy') > -1) {
@@ -216,7 +226,7 @@ const Overlay = ({
216226
// need to make sure that props stay in sync
217227
onHide={() => {
218228
setIsOpen(false);
219-
if (setProps) {
229+
if (setProps && isMountedRef.current) {
220230
setProps({is_open: false});
221231
}
222232
}}

0 commit comments

Comments
 (0)