React Native Text Input Clear Doesn't clear text

Edit: upon further verification, it seems that this only happens in Android 6.0.1. Having tried multiple devices with 6.0, this is not a problem.

I have a very simple action-based code snippet where I want to clear text in TextInput. It looks something like this:

state = {
  v: ""
};

_changeText = v => {
  this.setState({ v });
};

clear = () => {
  this.textInputRef.clear();
}

render() {
  return (
    <View style={styles.container}>
      <TouchableOpacity onPress={this.clear}>
        <Text> Clear </Text>
      </TouchableOpacity>
      <TextInput
        ref={ref => this.textInputRef = ref}
        value={this.state.v}
        onChangeText={this._changeText}
      />

    </View>
  );
}

Now the behavior that I expect is to leave the text in focus and clear the text. This is what happens - however, as soon as I start typing something on the keyboard, the text that I previously cleared appears again in the text input. Obviously, such persistence of the text is not really needed.

Has any of you ever had this problem? Is this an RN bug, or is there a way to avoid this behavior without requiring a keyboard blur?

, , : https://snack.expo.io/H1S9b5Mpe.

, "", , .

+6
2

, RN , , - . , , 6.0.0.

, clear TextInput : this.setState({v: ""});. .

0

! !

( ), TextInput false, , " "

<TextInput 
    autoCorrect={false} 
    ref={component => this.messageInput = component} 
    value={this.state.message} 
    onChangeText={(text) => this.setState({ message: text })}
    placeholder="Type your message here..." />

, , . !

BTW: this.setState({ message: "" }), reset .

0

Source: https://habr.com/ru/post/1016330/


All Articles