onKeyDown\onChange\onInput on android device for controlled input work incorrectly
See original GitHub issueDo you want to request a feature or report a bug? Bug
What is the current behavior?
Character appears in input before onKeyDown
handler is triggered.
Demo of what happens on android device (actual behavior): https://streamable.com/am8y4
Demo of what happens on PC (expected behavior): http://recordit.co/rX2V365iFI
Description and Example
- Look at codesandbox: https://codesandbox.io/s/0x828l6q2w. See that
input
is controlled - Open https://0x828l6q2w.codesandbox.io/ on Android device.
- Attach Chrome debugger
- Add breakpoint to 5th line
console.log("lalal");
- Input any number to on mobile device after
5
- See that number appeared in input alongside with breakpoint handler triggered
What is the expected behavior? … 6. Inputted number should not appear
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React: 16.6.0 Chrome android: 69.0.3497.100 Android: 9
List of devices where the issue was tested:
Issue Analytics
- State:
- Created 5 years ago
- Comments:7
Top Results From Across the Web
Input not working when using onChange and onKeyDown ...
The problem is that I'm no longer able to type something in the input field, however choosing values from the data list works....
Read more >react input onchange doesn't work - You.com | The AI Search ...
Upon a number being entered, they start to use onChange correctly. What is the current behavior? Character appears in input before onKeyDown handler...
Read more >Input events overview - Android Developers
Even if an input method presents a keyboard-like interface, it will generally not trigger the onKeyDown() family of events. You should never ...
Read more >The attribute for input tag"onchange" will not fire, but ...
When I type something into the input, validateNumberInput will not fire. However if I use onkeydown, it will fire. Am I missing something...
Read more >How to call onChange event after pressing Enter key - Edureka
You can use onKeyPress directly on input field. onChange function changes state value on every input field change and after Enter is pressed...
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 Free
Top 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
You may want to use the
onChange
event oronInput
.The keydown, keypress and keyup are browser/device/os/culture specific. I would use them only for a keyboard navigation for example arrows, delete key etc. mdn is good reference point to start. Too much information to be explained, and this is really not the place for such discussion.
Short answer:
If you have set
value
(thus you are in controlled mode) the react will keep overriding the value of the input element with the value of thevalue
property. So kind of preventing the typing.Long answer, TLDR:
The framework will not prevent the typing directly (like calling .preventDefault() on the native event) since the underlying input event is not cancelable. Instead, the framework restores the values after your code execution finishes. The onInput works pretty much the same on all modern browsers. (and there was not such event on older like IE6), so it is handy event to be used.
The keyDown can be prevented. But here is the issue: you don’t know what is typed on keyDown. Let me illustrate:
So if you want to know what is typed, deleted, dragged, swiped, pasted, autocompleted, dictated, you need to allow it being entered into the input by the browser, and it will be visible at some point of the process (during your onChange event). If you don’t like what is typed, then you override it later with something else. (this is the part that react do automatically for you if you have set
value
) The key part is that the overriding must happen in shortest time possible: under 1/60s, a frame, so the end user not be able to see flickering of the text.At the end: Should you be able to see the new value during onChange/onInput if you place debugger: Yes. Should the end user be able to see it: No
PS: I have simplified most of the stuff above, so it is not 100% technically correct, but short enough to be readable.