Skip to main content
add code snippet to allow people to use ScrollView instead
Source Link
Eric Kim
  • 11.1k
  • 4
  • 32
  • 32

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss the keyboard when the tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container{flex: 1}}> <TextInput keyboardType='numeric'/> </View> 

Change it to

<ScrollView contentContainerStyle={{flexGrow: 1}} keyboardShouldPersistTaps='handled' > <TextInput keyboardType='numeric'/> </ScrollView> 

or

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container{flex: 1}}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard, View } from 'react-native'; const DismissKeyboardHOC = (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; const DismissKeyboardView = DismissKeyboardHOC(View) 

Simply use it like this

... render() { <DismissKeyboardView> ...<TextInput keyboardType='numeric'/> </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss the keyboard when the tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard, View } from 'react-native'; const DismissKeyboardHOC = (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; const DismissKeyboardView = DismissKeyboardHOC(View) 

Simply use it like this

... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss the keyboard when the tap is not handled by the children (ie. tapping on other textInputs or buttons)

If you have

<View style={{flex: 1}}> <TextInput keyboardType='numeric'/> </View> 

Change it to

<ScrollView contentContainerStyle={{flexGrow: 1}} keyboardShouldPersistTaps='handled' > <TextInput keyboardType='numeric'/> </ScrollView> 

or

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={{flex: 1}}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard, View } from 'react-native'; const DismissKeyboardHOC = (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; const DismissKeyboardView = DismissKeyboardHOC(View) 

Simply use it like this

... render() { <DismissKeyboardView> <TextInput keyboardType='numeric'/> </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

Added a way to correctly dismiss keyboard while using ScrollView
Source Link
Eric Kim
  • 11.1k
  • 4
  • 32
  • 32

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss the keyboard when the tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard, View } from 'react-native'; const DismissKeyboardHOC = (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; const DismissKeyboardView = DismissKeyboardHOC(View) 

Simply use it like this

const DismissKeyboardView = DismissKeyboardHOC(View) ... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss keyboard when tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard } from 'react-native'; const DismissKeyboardHOC (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; 

Simply use it like this

const DismissKeyboardView = DismissKeyboardHOC(View) ... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss the keyboard when the tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard, View } from 'react-native'; const DismissKeyboardHOC = (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; const DismissKeyboardView = DismissKeyboardHOC(View) 

Simply use it like this

... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

Added a way to correctly dismiss keyboard while using ScrollView
Source Link

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss keyboard when tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard } from 'react-native'; const DismissKeyboardHOC (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; 

Simply use it like this

const DismissKeyboardView = DismissKeyboardHOC(View) ... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard } from 'react-native'; const DismissKeyboardHOC (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; 

Simply use it like this

const DismissKeyboardView = DismissKeyboardHOC(View) ... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss keyboard when tap is not handled by the children (ie. tapping on other textInputs or buttons)

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

If you have

<View style={styles.container}> <TextInput keyboardType='numeric'/> </View> 

Change it to

import {Keyboard} from 'react-native' <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <View style={styles.container}> <TextInput keyboardType='numeric'/> </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard } from 'react-native'; const DismissKeyboardHOC (Comp) => { return ({ children, ...props }) => ( <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}> <Comp {...props}> {children} </Comp> </TouchableWithoutFeedback> ); }; 

Simply use it like this

const DismissKeyboardView = DismissKeyboardHOC(View) ... render() { <DismissKeyboardView> ... </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

typos, code formatting
Source Link
SherylHohman
  • 18.2k
  • 18
  • 94
  • 101
Loading
Added a property that is necessary not to ruin the accessibility system that react-native has for blind people
Source Link
Loading
Removed flowtype
Source Link
Eric Kim
  • 11.1k
  • 4
  • 32
  • 32
Loading
Now using Keyboard exposed by react-native
Source Link
Eric Kim
  • 11.1k
  • 4
  • 32
  • 32
Loading
Updated solution to a better one
Source Link
Eric Kim
  • 11.1k
  • 4
  • 32
  • 32
Loading
Source Link
Eric Kim
  • 11.1k
  • 4
  • 32
  • 32
Loading