-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't modify text in TextInput onChangeText callback on Android #23578
Can't modify text in TextInput onChangeText callback on Android #23578
Comments
It looks like you are using an older version of React Native. Please update to the latest release, v0.58 and verify if the issue still exists. The "Resolution: Old Version" label will be removed automatically once you edit your original post with the results of running |
Shouting into the wind! Hope the next person can use this as a basis for reporting the issue. |
Sorry about that, but it's really important to make sure the issue is present in the latest release. We've had plenty of issues reported for bugs that have been fixed already. For bug reports that have a minimal repro example, verifying on the latest version using a brand new project should not take a lot of effort. |
Unable to replicate: https://snack.expo.io/@jkcooper/rn-issue-#23578---textinput-on-change |
Still happening on latest release. I've updated the |
@rogerbright is this happening in emulator, or on device? |
Tested just now on both an emulator (Nexus 5X API 28) and an actual device (Galaxy Tab A SM-T580). Same results on both. |
Would you be able to setup a snack demonstrating this issue?
…On Mar 2, 2019, 8:46 AM -0500, rogerbright ***@***.***>, wrote:
Tested just now on both an emulator (Nexus 5X API 28) and an actual device (Galaxy Tab A SM-T580). Same results on both.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Sure. https://snack.expo.io/By1LO7uUV In the snack interface, the bug appears only on Android. Works correctly on iOS. Make sure you enter only lowercase letters. |
@rogerbright ty I will take a look at this! |
I am facing the same issue. |
@umair-khanzada your issue seems different. In my case, the callbacks are being called just fine... it's what happens when I modify the text that makes things screwy. |
Thanks @ericlewis for volunteering to fix it! I think this is really annoying and severe issue, but fortunately, it's not affecting many of our developers. I am going to label this "mid-pri" while we are waiting for the PR. |
this happens with setNativeProps directly as well: #24409 |
Can confirm this also happens on RN 0.59 as well. Looks like I didn't need to modify controlled text input in a while, but when I did I remember using an overlay over transparent uncontrolled text input that would display the transformed text. I'd say it's pretty serious bug (and pretty old, happens at least since 0.57). |
here's a video of a repro: https://streamable.com/j4s4r and the code (i tested this on 0.57 and 0.59): import * as React from 'react';
import { Text, TextInput, View, StyleSheet } from 'react-native';
class ControlledInput extends React.Component {
state = {
value: '',
};
_handleChangeText = ({ nativeEvent: { text } }) => {
this.setState({ value: text.toUpperCase() });
};
render() {
return (
<TextInput
style={{
width: 300,
height: 50,
padding: 10,
backgroundColor: '#fff',
borderWidth: 1,
borderColor: '#eee',
borderRadius: 2,
}}
onChange={this._handleChangeText}
value={this.state.value}
/>
);
}
}
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<ControlledInput />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
}); and a snack: https://snack.expo.io/@notbrent/ranting-sandwich |
FWIW, I did end up solving this issue and many others. The solution starts with an "F" and rhymes with "butter." I realize that's not an option for everyone. |
|
Correct. I'm only six months in but so far my impression is that Flutter/Dart is mature, well thought-out, concise, and free of sanctimony. |
Hey guys, any news on this one? |
I guess not. |
+1 |
I'm seeing this on iOS but I suspect it's because of one of the packages I have installed.
|
@chancegraff I have the same issue on iOS, were you able to fix this? |
The same issue on Android in Emulator. It works fine for the Expo playground so don't use it to test. Simple reproduction: const ControlledComponent = () => {
const [state, setState] = useState('0.00');
const cursor = 1;
return (
<TextInput
ref={ref}
autoFocus
onKeyPress={() => { /* empty */ }}
selection={{start: cursor, end: cursor}}
value={state}
keyboardType="numeric"
/>
);
}; There is no way to prevent default behavior, |
The issue still persists on both IOS and Android I try to limit decimal points to 8 length. The user sees 9th character shortly before removal. It causes flickery. |
Do you still experience this issue? I have four years of experience maintaining facebook/react-native and I specialize in the Text and TextInput components. I currently have 58 facebook/react-native PRs. If you still experience this issue, I will prepare a patched release with the fix. Thanks a lot |
Yes, this bug still occurs. Please take care of it |
@fabOnReact |
Kind of a hacky work around (and doesn't always work quite as expected): const onChange = (e: string) => {
if (isValidInput(e)) {
textInputRef.current?.setNativeProps({ text: e });
}
} |
Thank you! I already tried that, but It's still flickering on device... It works fine on Simulator. |
Can you try something like this? . . .
const previousValueRef = React.useRef('');
. . .
const onChange = (e: string) => {
if (textInputRef.current) {
const textInput = textInputRef.current;
textInput.setNativeProps({ text: previousValueRef.current });
if (isValidInput(e)) {
textInput.setNativeProps({ text: e });
previousValueRef.current = e;
}
}
}
. . . If you need to accept only integers and know your max length, there was a suggestion from this SO post: <TextInput
maxLength={MAX_LENGTH}
keyboardType="numeric"
value={value}
onChangeText={(e) => {
if (/^\d+$/.test(e)) setValue(e);
}}
placeholder="Type only numbers..."
/> |
This PR is included in the react-native-improved library: react-native-improved
Set-upIn package.json "scripts": {
+ "postinstall": "yarn run react-native-patch"
} Then npmnpm install react-native-improved --save-dev yarn v1yarn add react-native-improved --dev |
Thank you!
For my case, I've decided to use a custom Native Module, but I'll give your solution a try another time. |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
A PR with a fix was created a year ago, I don't know what else I can do. |
What's the PR? |
It was linked: |
That's an issue, not a PR |
Ah sorry my mistake, I thought it was the issue where I created a PR for. |
🐛 Bug Report
On Android, modifying the text within the onChange (or onChangeText) callback causes corruption of the text in the TextInput. (Not tested on iOS.)
For example, I'm trying to force all caps in my TextInput field. (This is to work around the react native autoCapitalize issue described here: #8932). So if a lowercase letter is entered, I change it to uppercase in the callback. Unfortunately, alternate keystrokes cause the entire previous text to be duplicated, but only if the entered keystroke was lowercase.
So, when forcing all caps, entering
1234
results in1234
showing up; enteringABCD
results inABCD
showing up; but enteringabcd
results inAABCAABCD
.This issue disappears if assigning a Math.random() key to the TextInput; but then of course so does the keyboard focus, making this an unacceptable workaround.
To Reproduce
See "Bug Report" and "Code Example" sections.
Expected Behavior
One should be able to modify the value inside TextInput's change callbacks, without the text becoming corrupted on the subsequent redisplay.
Code Example
Environment
React Native Environment Info:
System:
OS: Linux 3.19 Ubuntu 14.04.3 LTS, Trusty Tahr
CPU: (4) x64 Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz
Memory: 626.14 MB / 15.38 GB
Shell: 6.18.01 - /bin/tcsh
Binaries:
Node: 8.11.3 - /usr/bin/node
npm: 5.6.0 - /usr/bin/npm
SDKs:
Android SDK:
API Levels: 10, 16, 23, 26, 27, 28
Build Tools: 19.1.0, 20.0.0, 21.1.2, 22.0.1, 23.0.1, 23.0.2, 26.0.3, 27.0.3, 28.0.2, 28.0.3
System Images: android-16 | ARM EABI v7a, android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom
npmPackages:
react: 16.6.3 => 16.6.3
react-native: 0.58.6 => 0.58.6
npmGlobalPackages:
create-react-native-app: 1.0.0
react-native-cli: 2.0.1
The text was updated successfully, but these errors were encountered: