Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value in TextInput when onBlur is called?

Tags:

react-native

In React Native, I want to pass the value of the TextInput in the onBlur event handler.

onBlur={(e) => this.validateText(e.target.value)} 

e.target.value works for plain React. But, in react-native, e.target.value is undefined. What is the structure of event args available in React Native?

like image 281
vijayst Avatar asked Aug 23 '16 16:08

vijayst


People also ask

What is TextInput value?

The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button. For "text", "password", and "hidden" - it defines the initial (default) value of the input field.

What is onBlur in React Native?

onBlur. The onBlur event handler is called when focus has left the element (or left some element inside of it). For example, it's called when the user clicks outside of a focused text input.

How do I change TextInput value in React Native?

TextInput needs value that it is the value that is gonna be shown inside the TextInput. And to update that value you use onChangeText that is gonna call whatever function you specify every time the text into the TextInput change.


1 Answers

You should use the 'onEndEditing' method instead of the 'onBlur'

onEndEditing?: function Callback that is called when text input ends.

onBlur is a component function where onEndEditing is specific for TextInput

onEndEditing

This approach works for both multiline and single line.

<TextInput      onEndEditing={(e: any) =>      {         this.setState({textValue: e.nativeEvent.text})     } }/> 
like image 101
Yair Levi Avatar answered Oct 08 '22 08:10

Yair Levi