Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access text of a TextInput upon onSubmitEnding

Tags:

react-native

I simply want to call a function with the text of a TextInput once editing of the text is done. Something like below

<TextInput onSubmitEnding={(text) => submitText(text)}>

But obviously the text is not passed as argument to onSubmitEnding, . onChangeText has it. But I really want the text after the user is done editing. So whats the simplest means to do this,

like image 750
Sathyakumar Seshachalam Avatar asked Feb 07 '23 18:02

Sathyakumar Seshachalam


2 Answers

onSubmitEnding is not a valid event, the correct one is onSubmitEditing.

2º You can get the input value using event.nativeEvent.text

Your code should look like this

<TextInput onSubmitEnding={(event) => this.submitText(event.nativeEvent.text)}>
like image 103
QoP Avatar answered Feb 10 '23 22:02

QoP


I'm not forcing you to certain pattern, but you should have your TextInput's value in a state. Then:

...
this.state = {
  textInputValue: ''
}
...
submitText() {
  console.log(this.state.textInputValue)
}
...
<TextInput
  value={this.state.textInputValue}
  onChangeText={(text) => this.setState({textInputValue: text})}
  onSubmitEditing={() => this.submitText()} />

is completely valid. Here's a live example: https://rnplay.org/apps/wirurQ

like image 34
Samuli Hakoniemi Avatar answered Feb 10 '23 23:02

Samuli Hakoniemi