Change in v10.11.1 causes inputs to lose focus on DOM changes
See original GitHub issue- Check if updating to the latest Preact version resolves the issue (no)
Describe the bug Up to preact v10.11.0 changes in the DOM did not cause a focused input to lose focus. Starting with v10.11.1 the input loses focus.
To Reproduce
Steps to reproduce the behavior:
- Check out https://github.com/jcestibariz/bug-preact-focus
- Notice preact is locked to v10.11.0 which does not contain the bug
- Run
npm i
and thennpm start
- In the browser click on the input and press backspace
- Notice the last “tag” is removed but the input keeps focus.
- Go back to the console and press Ctrl+C to stop the server
- Run
npm i preact@10.11.1
and thennpm start
- Go back to the browser and repeat step 4
- Notice now the input loses focus
- You can also check with the latest version (v10.11.3 as of this moment) the bug is still present
Expected behavior The input should not lose focus when DOM changes.
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React Routing causes inputs to lose focus - Stack Overflow
This causes the input field to lose focus. Avoid using <Redirect /> and use history.replace after updating the state.
Read more >Issues · preactjs/preact - GitHub
#2618 opened on Jul 11, 2020 by marvinhagemeister. Open 26 · The road to Preact 11 ... Change in v10.11.1 causes inputs to...
Read more >onfocusout Event - W3Schools
The onfocusout event occurs when an element looses focus. The onfocusout event is often used on input fields. The onfocosout event is often...
Read more >Adding Html element makes input-field lose focus - Learn - Elm
With the following code, when input text reaches 5 characters, the input field will lose focus. If I change else branch to [...
Read more >React.js loses input focus on typing
This meant that when the user typed one character the recipients[i] value changed (via recipientEmailChangedHandler ).
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
Reverting it would not be the right course of action as the PR solves an issue with unmounting fragments, dissecting the issue though points at a bigger one 😅 so first off, the reproduction isn’t using keys for a changing list of elements, which does hint that these remounts are intended. Going further, when we add the keys, in this reproduction, we can see that the focus is lost when we remove the first element but not the second time around which does point that we are dealing with a matching issue of or dom or vnodes.
PR for reference https://github.com/preactjs/preact/pull/3738 - we might need to tweak this to better account for the two cases.
I ran a git bisect and looks like the commit that caused this bug is:
I reverted this change on top of the current master (d4089df1263faab9b980a3493a4c7e986f254f8e) and I can confirm that reverting this change fixes the bug. Since I don’t have enough knowledge of preact’s internals to tell @JoviDeCroock 's intention, I won’t submit this revert as the solution.