I have a TextInput component that calls an 'OnChangeText' event :
<TextInput onChangeText={(text) => this.setState({zip:text})}/>
That works fine.
Now I want to change the call event to a function:
 <TextInput onChangeText={_handleTextChange}/>
_handleTextChange(event) {
       this.setState({zip: 
       event.nativeEvent.text});
  }
That doesn't work. I get the following error :
undefined is not an object (evaluating 'event.nativeEvent.text'
How do send the 'Event' object to _handleTextChange function?
Thanks, Yaron
onChangeText passes a string to your handler function instead of an Event.
class YourComponent {
   render () {
     // ...
     return <TextInput onChangeText={this._handleTextChange.bind(this)}/>;
   }
  _handleTextChange(text) {
     this.setState({zip: text});
  }
}
EDIT
If you want an Event instead of text string, you can use onChange instead of onChangeText.
class YourComponent {
   render () {
     // ...
     return <TextInput onChange={this._handleTextChange.bind(this)}/>;
   }
  _handleTextChange(event) {
     this.setState({zip: event.nativeEvent.text});
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With