When calling clear function on TextInput, it does not call onChange or onChangeText method.
https://snack.expo.io/ry1B5ZR2g
What needs to be done to address this issue? Ideally, provide a pull request with a fix.
I would suggest you change your code to
<Button title="button" onPress={this.clearInput.bind(this)} />
<TextInput
ref={(ref) => {this.textInputRef = ref}}
value={this.state.inputValue}
onChangeText={this._handleTextChange.bind(this)}
style={{ width: 200, height: 44, padding: 8 }}
/>
@hoangpham95 I changed the code based on your recommendation but I don't see the even still firing. Here is the updated sketch https://snack.expo.io/BJDhWjJJ-
Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!
If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:
If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.
ok, this is still a problem as of 0.49.2.
This is also a problem when you have clearButtonMode set to anything but 'never'
After reviewing the TextInput code I figured out this "hack":
change:
onChangeText={(s)=>this.searchUpdate(s)}
to:
onChange={(e)=>this.searchUpdate(e.nativeEvent.text)}
Then you'll get the clear button touch
If anyone is still looking for an answer to this, putting the onChangeText function outside of the JSX (eg. this.onChangeText), then calling it with an empty string after a clear seems to work (RN 0.54.1).
class MyComponent extends React.Component {
textInputRef;
onChangeText = (newText) => {
// insert your logic here
}
clearInput = () => {
this.textInputRef.clear();
this.onChangeText('');
}
render = () => {
return (
<TextInput
onChangeText={this.onChangeText}
ref={comp => { this.textInputRef = comp; }}
/>
);
}
}
how to fix This issue, this is still a problem as of 0.55.4
You should use value property of TextInput, below is the snippet code that I use for my projects
class YourComponent extends React.Component {
clearInput = () => {
this.setState({ input: '' })
}
onChangeText = (input) => {
this.setState({ input })
}
render() {
return (
<TextInput
value={this.state.input}
onChangeText={this.onChangeText}
/>
)
}
}
Most helpful comment
If anyone is still looking for an answer to this, putting the
onChangeTextfunction outside of the JSX (eg.this.onChangeText), then calling it with an empty string after a clear seems to work (RN 0.54.1).