question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Focus breaks when calling setState in onFocus / onBlur

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
klis87commented, Dec 19, 2018

#2451 is a different issue, this bug occurs even with one editor. For some reason setTimeout fixes blurring:

onBlur = (event, editor, next) => {
    next();
    setTimeout(() => this.setState({ hasFocus: false }), 0);
}; 

which imho this is the proof that it is a race condition issue.

See online demo: https://jsfiddle.net/fj9dvhom/2734/

3reactions
rahul1995commented, Jun 13, 2019

I’m quite surprised to find that setState() on a parent’s onBlur/Focus handler also prevents the editor from gaining/losing focus. I’d expect Editor’s inner workings to be immune from external state changes like this?

I’ve forked @ericedem’s fiddle, and simply moved the onFocus/Blur handler to a <div> wrapper to demonstrate the problem here: https://jsfiddle.net/thatmarvin/sLkq06yt/

Wrapping setTimeout also fixes the problem: https://jsfiddle.net/thatmarvin/fa0encyd/

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found