Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: Keyboard dismiss when changing focus in ScrollView

In my React Native 0.22 iOS app, I have a ScrollView with multiple TextInput element in it.

I noticed that when I change focus from one TextInput to another by tapping on the next TextInput, the keyboard will dismiss and the next TextInput won't get focused immediately. It only get focus on the second time I tap on it (and then keyboard comes back again, really bad experience).

This behavior only happen on TextInput in ScrollView, but not View. I am wondering if there's any way to work around it?

Thank you!

like image 321
Allan Jiang Avatar asked Apr 11 '16 09:04

Allan Jiang


People also ask

How do you dismiss a keyboard in modal react native?

Method 1: Using TouchableWithoutFeedback Component: We simply encapsulate the outermost View component of our App inside the TouchableWithoutFeedback component and set the onPress value of this component to Keyboard. dismiss.

How do you Unfocus a keyboard 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 .


4 Answers

Just provide keyboardShouldPersistTaps="always" prop to your scrollview.

From the docs -

  • 'never' (the default), tapping outside of the focused text input when the keyboard is up dismisses the keyboard. When this happens, children won't receive the tap.
  • 'always', the keyboard will not dismiss automatically, and the scroll view will not catch taps, but children of the scroll view can catch taps.
  • 'handled', the keyboard will not dismiss automatically when the tap was handled by a children, (or captured by an ancestor). false, deprecated, use 'never' instead true, deprecated, use 'always' instead

Docs: https://facebook.github.io/react-native/docs/scrollview#keyboardshouldpersisttaps

like image 131
Mihir Avatar answered Oct 17 '22 23:10

Mihir


Just add the following to your scrollview:

keyboardShouldPersistTaps='handled'

This makes the scrollview to hide the keyboard if no editable control is focused.

like image 30
Ali Avatar answered Oct 17 '22 22:10

Ali


keyboardShouldPersistTaps={true} deprecated

false, deprecated, use 'never' instead

true, deprecated, use 'always' instead

like image 12
thuyiya Avatar answered Oct 17 '22 23:10

thuyiya


RN 40+

ScrollView

keyboardShouldPersistTaps :

Determines when the keyboard should stay visible after a tap.

keyboardDismissMode

Determines whether the keyboard gets dismissed in response to a drag

<ScrollView keyboardShouldPersistTaps={true} keyboardDismissMode="on-drag">
    <TextInput>
</ScrollView>

https://github.com/facebook/react-native/issues/8234

like image 3
Mahesh K Avatar answered Oct 17 '22 22:10

Mahesh K