I have multiple components in my app where I get user inputs using a keyboard. Now I want to dismiss the keyboard whenever user presses outside of the text input field.
I know there is TouchableWithoutFeedback in which I can wrap my component, but whats the best way to do it for multiple screens having this issue.
Shall I create a HOC for this that handles TouchableWithoutFeedback ??
You can do this
<KeyboardAvoidingView behavior="padding">
<ScrollView keyboardShouldPersistTaps="handled">
// Rest of your code
</ScrollView
</KeyboardAvoidingView>
HOC is the best approach imo
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>
);
};
export const DismissKeyboardView = DismissKeyboardHOC(View)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.