8000 Bug: useEffect() clears timeout immediately after a focus event with StrictMode · Issue #33008 · facebook/react · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Bug: useEffect() clears timeout immediately after a focus event with StrictMode #33008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
layershifter opened this issue Apr 24, 2025 · 1 comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@layershifter
Copy link

React version: 18.3.1

Steps To Reproduce

  1. Navigate with keyboard to "toggle menu", press Enter
  2. 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:

So we are wondering what is a proper solution in case and looking for guidance in this particular case 🙏

@layershifter layershifter added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 24, 2025
@szhsin
Copy link
szhsin commented Apr 26, 2025

Just stumbled across this issue. I think you can drop the clearTimeout in the useEffect entirely, since there's no harm in calling setState on an unmounted component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants
0