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!
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.
To unfocus a TextInput in React Native, we can use the Keyboard. dismiss method. to set the onSubmitEditing prop to Keyboard. dismiss .
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
Just add the following to your scrollview:
keyboardShouldPersistTaps='handled'
This makes the scrollview to hide the keyboard if no editable control is focused.
keyboardShouldPersistTaps={true}
deprecated
false
, deprecated, use'never'
instead
true
, deprecated, use'always'
instead
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With