Focus breaks when calling setState in onFocus / onBlur
See original GitHub issueDo you want to request a feature or report a bug?
bug
What’s the current behavior?
When you call setState()
from within an onFocus()
or onBlur()
handler the editor can no longer receive / release focus. Here is a JSFiddle illustrating the problem: https://jsfiddle.net/fj9dvhom/2611/. Just try to focus or unfocus the editor.
Chrome: Version 70.0.3538.102 (Official Build) (64-bit) Mac OS: 10.14.1
What’s the expected behavior?
Focusing should work correctly.
I have a suspicion that the setState()
call is causing a render event before the editor can finish handling the original focus / blur event. Wrapping the setState()
in a setTimeout()
seems to alleviate the issue.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:15
- Comments:19 (10 by maintainers)
Top Results From Across the Web
Using setState in onFocus removes the focus - Stack Overflow
I found that in my TextInput wrapper, when it is updated the FIRST TIME using setState from onFocus callback, it always calls onBlur...
Read more >Initializing focus state in React: you might be doing it wrong
When this happens and the element starts off focused before React hydrates, it's too late for React to see the focus event and...
Read more >Accessibility - React
Programmatically managing focus. Our React applications continuously modify the HTML DOM during runtime, sometimes leading to keyboard focus being lost or set ...
Read more >How to fix the ugly focus ring and not break accessibility in React
How to fix the ugly focus ring and not break accessibility in React ... Another approach involves calling onBlur after the event is...
Read more >onblur Event - W3Schools
The onblur event occurs when an element loses focus. The onblur event is often used on input fields. The onblur event is often...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
#2451 is a different issue, this bug occurs even with one editor. For some reason
setTimeout
fixes blurring:which imho this is the proof that it is a race condition issue.
See online demo: https://jsfiddle.net/fj9dvhom/2734/
Yes, this one needs to be fixed on priority as it’s totally a blocker. It took my 2 days to figure out why my editor was not working, as it doesn’t even throw any error.