[0.54] [iOS] Wrong 'Backspace' event fired onKeyPress after clearing TextInput
See original GitHub issueIn IOS, after clearing TextInput value (setting it to empty string) and then pressing any key (e.g. ‘a’) two onKeyPress events are fired: A, Backspace. First is for the pressed key and second is a Backspace.
Environment
Environment: OS: macOS Sierra 10.12.6 Node: 8.9.1 Yarn: 1.3.2 npm: 5.5.1 Watchman: 4.9.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 3.0 AI-171.4443003
Packages: (wanted => installed) react: ^16.3.0-alpha.1 => 16.3.0-alpha.1 react-native: 0.54.2 => 0.54.2
Expected Behavior
Only one event for the pressed key should be fired (e. g. ‘A’).
Actual Behavior
An additional ‘Backspace’ event is fired.
Steps to Reproduce
- write anythig in the textfield
- press clear
- press ‘a’ >>> events ‘A’ and ‘Backspace’ will be fired
type Props = {};
type State = {
text: string,
keys: string
};
export default class App extends Component<Props, State> {
state = {text: '', keys: ''}
render() {
return (
<View style={styles.container}>
<TextInput style={styles.textInput} value={this.state.text} onChangeText={this.onChangeText} onKeyPress={this.onKeyPress}/>
<Button title="Clear" onPress={this.onClear}/>
<Text>Text: {this.state.text}</Text>
<Text>Keys: {this.state.keys}</Text>
</View>
);
}
onChangeText = (text: string) => {
this.setState({text})
}
onKeyPress = ({ nativeEvent }: Object) => {
this.setState({keys: this.state.keys + nativeEvent.key + ', '})
}
onClear = () => {
this.setState({text: '', keys: ''})
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:15
- Comments:19 (5 by maintainers)
Top Results From Across the Web
Is there a way to detect in javascript that the Backspace is long ...
Try the following: onKeydown(key, event) { let input = document. ... and no text after the cursor position; focus the next field if...
Read more >Previous Textinput Autofocus On Backspace Press React ...
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character...
Read more >ClientEvents OnKeyPress + Backspace/Delete - Telerik
Hi Mike, Delete, backspace and other special keys do not fire the KeyPress event in IE. What you can do is use the...
Read more >MathLive Changelog - CortexJS
#1572 Keyboard events ( keyup , keydown , keypress ) are now fired more ... Long press on the backspace key is now...
Read more >TextInput - React Native Archive
There are also other events, such as onSubmitEditing and onFocus that can be subscribed to. A minimal example: import React, { Component }...
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
I was able to workaround this bug by adding a check in the
onKeyPress
callback function,I found while debugging that duration between receiving backspace event after key press is less than ~10ms. So I record the non-backspace key event timeStamp and use it to compare with backspace event timeStamp. If the difference between these two is less than 20ms (arbitrary value > 10ms) then I return from the callback. Hope this helps!
I opened a pull request to fix this bug: #18627