React-native: Clear function on TextInput does not call onChange

Created on 2 Apr 2017  路  7Comments  路  Source: facebook/react-native

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
Locked

Most helpful comment

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; }}
      />
    );
  }
}

All 7 comments

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:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

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}
      />
    )
  }
}
Was this page helpful?
0 / 5 - 0 ratings