Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when the focus is lost in a react native textInput?

How to know when the focus is lost in a react native textInput?

For example I want to do an action when the user touches outside the textInput and it loses focus.

like image 581
Estebann Aristizabal S Avatar asked Apr 13 '17 18:04

Estebann Aristizabal S


People also ask

How do I know if TextInput is focused?

To check if an input field has focus with JavaScript, we can use the document. activeElement property to get the element in focus. to add an input. to check if the input element is focused.

How do I see focus in React Native?

To check if an element is focused in React:Set the ref prop on the element. After the element is rendered, check if the element is the active element in the document. If it is, the element is focused.

How do you remove TextInput focus in React Native?

To unfocus a TextInput in React Native, we can use the Keyboard. dismiss method. to set the onSubmitEditing prop to Keyboard. dismiss .

Why does the input field lose focus after typing a character?

it is because you are rendering the form in a function inside render(). Every time your state/prop change, the function returns a new form. it caused you to lose focus.


1 Answers

use the onBlur prop on TextInput

example

<TextInput           onFocus={() =>console.log("focus received" ) }           onBlur={() => console.log("focus lost") } /> 

Docs

like image 176
Ahmed Eid Avatar answered Sep 20 '22 18:09

Ahmed Eid