Open
Description
React version: 18.3.1
Steps To Reproduce
- Navigate with keyboard to "toggle menu", press Enter
- Notice that a menu is opened, but a tooltip does not exist 🚨
Link to code example: https://stackblitz.com/edit/vitejs-vite-nnvwzvcm
The current behavior
2025-04-24.13.50.07.mp4
Menu:useEffect() // moves focus to MenuItem ✅
MenuItem:onFocus() => Tooltip:onFocus() // triggers a setTimeout() to open Tooltip ✅
Tooltip:useEffect:dispose() // calls clearTimeout() 🚨
The expected behavior
Without StrictMode
a dispose function is not called:
2025-04-24.13.51.24.mp4
That's a simplified version of the issue from microsoft/fluentui#34296. I clearly understand that it's not a bug in React. It does what's expected 👍
The correct approach is to fire setTimeout()
& clearTimeout()
in useEffect()
. The problem is that the described pattern is not unique and adopted by component libraries:
- https://github.com/mui/material-ui/blob/ad81de06c384d446d4fc9740a53b692f209165a3/packages/mui-material/src/Tooltip/Tooltip.js#L410-L413
- https://github.com/radix-ui/primitives/blob/e2e0559010cb5e861944ecba840823ae530fa4a8/packages/react/tooltip/src/tooltip.tsx#L205C9-L221
- more...
So we are wondering what is a proper solution in case and looking for guidance in this particular case 🙏