1

I want onTextChange to be triggered when I set text to it through state changes. I am building my on screen number keyboard to enter a pin code, and I want to catch text changes on TextInput when user presses these keys.

this.setState({text})

My TextInput looks like this:

<TextInput
  editable={false}
  onChangeText={text => Alert.alert(text)}
  secureTextEntry={true}
  value={this.state.text}

UPD: I found somehow related question, but it too doesn't have an answer: TextInput not working as expected when programmatically entering text

madim
  • 774
  • 10
  • 22

6 Answers6

2

You set the editable prop to false then TextInput's text will never change and onChangeText will never call...

Farsheel
  • 610
  • 6
  • 17
Ali SabziNezhad
  • 3,050
  • 1
  • 17
  • 31
1

I ended up using onContentSizeChange, which gets called when text changes. I did find that some TextInput methods doesn't work properly when text is set programmatically.

madim
  • 774
  • 10
  • 22
1

I think the event handler function for onChangeText is asynchronous, hence you should use async/await

<Input onChangeText={(val) => { this.changeHandler(val) }} />

and the change handler method should be

changeHandler = async (val) =>{
  await this.setState({
    [someKey]: val
 })
  .
  .
  .
  do other stuff that depends on val
}

it worked for previously to make http request which is fired when input value changes, and uses the input value.

1

Because you didn't bind for it:

onChangeText={text => Alert.alert(text)}

should change:

onChangeText={this.changeFunction.bind(this)}

then add a function:

changeFunction(text){
   alert(text);
}

=> You can test then vote me down if it not work :)

Thang Pham
  • 71
  • 1
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 19:40
0

You may use it like below.

class MyComponent extends Component{
  constructor(props){
    super(props)
    this.state = { text: null }   //By default it will be null.
    this._handleChange = this._handleChange.bind(this)  //You need to bind your function here.
  }

  _handleChange(e) {
        const { name, value } = e.target;  //it will destructure your name and value where ever you are using _handleChange method.
        this.setState({
            [name]: value // here we are setting the state dynamically.
        });
        alert(value)
    }

   render() {
     return(
       <View>
         <TextInput
           editable={true}
           onChangeText={this._handleChange}  // this will call _handleChange method.
           secureTextEntry={true}
           value={this.state.text}/>       
       </View>
     )
   }
}
dhivya s
  • 294
  • 1
  • 7
0

This code maybe help you:

     this.state = {Alert.alert(
                         'Update available //your Title',
                         'Keep your app up to date to enjoy the latest 
                          features //your Message',
                          [
                            {
                             text: 'Cancel',
                             onPress: () => console.log('Cancel Pressed'),
                             style: 'cancel',
                             }
                           ]
                      )
    }
    <TextInput
      editable={false}
      onChangeText={text => Alert.alert(text)}
      secureTextEntry={true}
      value={this.state.text}
    />
Ali Tooshmalani
  • 241
  • 2
  • 7