Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between onEndEditing and onBlur?

Tags:

react-native

React Native has a TextInput component to accept user inputs and I'm interested in the difference between

onEndEditing

Callback that is called when text input ends.

onBlur

Callback that is called when the text input is blurred.

Is there a scenario where it can't be solved just with onBlur, when is onEndEditing useful?

like image 264
everlasto Avatar asked Dec 02 '15 21:12

everlasto


People also ask

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 you get the onBlur value in react native?

To get the value in TextInput when onBlur is called with React Native, we can get the value from the onEndEditing callback. to set onEndEditing to (e) => console. log(e. nativeEvent.

How do I use onKeyPress in react native?

To handle key presses in React, we use onKeyPress. It is passed as an attribute in <input> elements, and can be used to perform actions for any event involving the keyboard, whether you want to call a function on any key press, or only when a specific key is pressed.


1 Answers

First let me say I was struggling finding the answer to this question!

I believe this is rooted in history since it originates from iOS which has the native event UIControlEventEditingDidEnd. That's probably the naming before blur was introduced as an abstraction.

TL:DR;

onEndEditing should really be deprecated in my opinion, but as of right now you should use it because it is the most platform agnostic version of blur. See below.

Both onBlur and onEndEditing get an event. On iOS both of these seem to do the exact same thing, and the event has the native text. In Android, it is two different events, and only one of them has access to the text. To me this seems like a bug.

Notice the differences between Android onEndEditing and Android onBlur.

// this is undefined on Android onBlur={(e) => alert(e.nativeEvent.text)} 

So if reading the text during a blur, you get cross-platform usability with onEndEditing for now.

See this rnplay to see the differences in OS

like image 193
Gant Laborde Avatar answered Sep 23 '22 22:09

Gant Laborde