1

This bug only exists on Android.

If I have a textInput on Android and put value equal to state. Then somewhere else i change value, when i use onChange on the textInput it uses the old text value not the new state.

Here is my snack https://snack.expo.io/SyV1mkIc4

And below is the entire code showing it not working

    import * as React from 'react';
import { Text, View, StyleSheet, TextInput, TouchableOpacity } from 'react-native';
import { Constants } from 'expo';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

export default class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      text:  'aaa'
    }
  }
  render() {
    return (
      <View style={styles.container}>
        <TextInput
          value={this.state.text}
          onChange={(event) => this.setState({
      text: event.text,
    })}
        />
      <TouchableOpacity
        onPress={()=>this.setState({
      text: "",
    })}
        style={styles.submit}
      >
      </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  submit: {
    height: 200,
    width: 200,
    backgroundColor: 'blue',
    textAlign: 'center',
  },
});
Adam Katz
  • 6,999
  • 11
  • 42
  • 74

3 Answers3

1

use onChangeText instead,

onChangeText={(text)=>{this.setState({text});}}
Jaydeep Galani
  • 4,842
  • 3
  • 27
  • 47
1

Okay i figured it out this a samsung only issue and has to do with keyboard caching. The solution is to keyboard.dismiss after changing the state of value.

Adam Katz
  • 6,999
  • 11
  • 42
  • 74
0

I am seeing this on Moto phones, too. I dealt with it by updating the state from onKeyPress instead of onChangeText. Annoying because you have to handle the backspace, and also because now it flickers -- the input shows the new keypress value briefly, then hides it, then shows the new state set by my keypress handler. (As seen in this question) Maybe there's a simpler solution I have overlooked, but here is my example:

validateEmail({ nativeEvent: { key } }) {
    if (key == "Enter") return;

    let value = (this.state.email || "");
    if (key == "Backspace") {
      value = value.substring(0, Math.max(0, value.length - 1));
    }
    else {
      value = value.concat(key.replace(/[^a-z0-9.+@_-]+/ig, "").toLowerCase());
    }
    this.setState({ email: value});
  }

...

 const Email = <Input
      value={this.state.email}
      onKeyPress={this.validateEmail}
    />

So now I am making a workaround to let onchangetext handle valid keypresses, and only change it from onkeypress when necessary to minimize flickering:

 isValidEmail(str) {
    return !/[^a-z0-9.+@_-]+/g.test(str);
  }

  validateEmail({ nativeEvent: { key } }) {
    console.log("key: " + key);

    // don't update state here if key is valid:
    if (key == "Enter" || key == "Backspace" || this.isValidEmail(key)) return;

    // update state with corrected key:  
    value = (this.state.email || "").concat(key.replace(/[^a-z0-9.+@_-]+/ig, "").toLowerCase());

    console.log("value: " + value);
    this.setState({ email: value});
  }

...

  const Email = <Input
      value={this.state.email}
      onChangeText={(text) => { if (this.isValidEmail(text)) this.setState({email: text})}}
      onKeyPress={this.validateEmail}
    />
alr3000
  • 83
  • 11