question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clear function on TextInput does not call onChange

See original GitHub issue

Description

When calling clear function on TextInput, it does not call onChange or onChangeText method.

Reproduction Steps and Sample Code

https://snack.expo.io/ry1B5ZR2g

Solution

What needs to be done to address this issue? Ideally, provide a pull request with a fix.

Additional Information

  • React Native version: 0.42.0
  • Platform: both
  • Development Operating System: Mac

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
hoangpham95commented, Jun 14, 2017

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 }}
        />
4reactions
charlesrollincommented, May 11, 2018

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; }}
      />
    );
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

onChange not firing when deleting the input value
I'm facing an issue where i cannot get the onChange to fire when i delete the input value ...
Read more >
onchange event is not triggered when clear an input field
When you clear the account name the contents changes but focus is still on input and so it does not fire onChange event....
Read more >
On-change not triggered when input cleared - OutSystems
I have an input box which is linked to an autocomplete widget. When the input box is blank, I show a button with-which...
Read more >
TextInput - React Native
onChangeText ​. Callback that is called when the text input's text changes. Changed text is passed as a single string argument to the...
Read more >
Solved: OnChange() of a textInput doesn't work when enter
If I am in the Text Input and at any time I push a non numeric key, then I step out of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found