Simple Steps to Hide Keyboards in React Native

Simple Steps to Hide Keyboards in React Native

In the React Native, keyboard usage sometimes gives a troublesome experience to the users. Of course, it will cover half of the screen and cannot get enough space to access the react native. Use a tap outside button and consider keyboard.dismiss() command to hide the keyboards in React Native.

There is no need to click on the button or other component to hire a touchpad. It refers to the React Native hide keyboard option. It includes touch outside android iOS with native hide dismiss keyboard or hide keyboard easily.

For hiding keyboards in React Native, the user must include a module have dismiss() method and utilize the keyboard.dismiss() or even consider close keyboard option in react Native.

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

How to hide keyboards in react native?

When you are in the main app.js file and hide keyboard, it includes stylesheet view and text input within a short time. It carry out components in handling touchable without feedback component. User allows you to access custom component and named in hide keyboard with children prop.

Onpress=<> keyboard.dismiss ()

Creating a main export should optimize the functional component. In addition to this, the user has to hide the keyboard and take root parent components in creating 2 text input components. You can hide the keyword and root parent component in the export default function home screen ().

export default function HomeScreen() {
  return (
    <HideKeyboard>
      <View style={styles.MainContainer}>
 
        <Text style={styles.text}>React Native Hide Keyboard on Touch Outside</Text>
 
        <TextInput
          style={styles.textinput}
          placeholder="Enter Your Email"
          keyboardType="numeric"
        />
 
        <TextInput
          style={styles.textinput}
          placeholder="Enter Your Password"
        />
 
      </View>
 
    </HideKeyboard>
  );
}

Using the content with hiding keyboards in react native:

import React from 'react';
import { Keyboard, StyleSheet, View, Text, TextInput, TouchableWithoutFeedback } from 'react-native';
const HideKeyboard = ({ children }) => (
  <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
    {children}
  </TouchableWithoutFeedback>
); 
export default function HomeScreen() {
  return (
    <HideKeyboard>
      <View style={styles.MainContainer}>
 
        <Text style={styles.text}>React Native Hide Keyboard on Touch Outside</Text>
         <TextInput
          style={styles.textinput}
          placeholder="Enter Your Email"
          keyboardType="numeric"
        />
 
        <TextInput
          style={styles.textinput}
          placeholder="Enter Your Password"
        />
       </View> 
    </HideKeyboard>
  );
} 
const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    fontSize: 28,
    textAlign: 'center'
  },
  textinput: {
    width: '80%',
    height: 40,
    paddingVertical: 12,
    margin: 8,
    borderRadius: 7,
    backgroundColor: '#F9FBE7',
    borderWidth: 2,
    borderColor: '#00B8D4',
    textAlign: 'center'
  },
});

Hiding the keyboard in React Native seems to have different forms and inputs. It considers developers to fight with common issues on soft keyboard and cover the bottom parts of screens. It ensures that it is unreachable for the user. It has to hide the keyboard in React Native by focusing on functional options for the users.

const KeyboardAwareScrollViewFormScreen: React.FC = () => {
  const onFocusEffect = useCallback(() => {
    AvoidSoftInput.setAdjustPan();
    return () => {
      AvoidSoftInput.setDefaultAppSoftInputMode();
    };
  }, []);  
  useFocusEffect(onFocusEffect);
  return <SafeAreaView
    edges={[ 'bottom', 'left', 'right' ]}
    style={commonStyles.screenContainer}>
    <KeyboardAwareScrollView
      enableOnAndroid={true}
      enableResetScrollToCoords={false}
      bounces={false}
      contentContainerStyle={commonStyles.scrollContainer}
      contentInsetAdjustmentBehavior="always"
      overScrollMode="always"
      showsVerticalScrollIndicator={true}
      style={commonStyles.scroll}>
      <FormExample />
    </KeyboardAwareScrollView>
  </SafeAreaView>;
};

Ways to hide keyboards in react native using android and iOS projects

While composing React Native applications, moreover engineers need to consider how to determine it such that it will bring about steady conduct on the two stages. There are a couple of arrangements that can be utilized, to accomplish the most ideal impact, starting with worked in, through outsider, and finishing with totally custom rationale composed for explicit use cases.

This article should depict and think about them in various experiments. In any case, before that, we should check how that console inconvenience can be taken care of in local Android and iOS projects.

Custom rationale with manifest record

Android has an underlying android:windowSoftInputMode movement boundary, which controls how the console is shown along with the action’s UI. On iOS, there is no underlying way. Engineers rather depend on some custom rationale or the famous IQKeyboardManager library. These (local) arrangements can be utilized in React Native venture – setting esteem in the manifest record on Android and introducing responsive local console director for iOS, which is a RN covering for IQKeyboardManager.

KeyboardAvoidingView + android:windowSoftInputMode="adjustPan"

KeyboardAvoidingView is a React Native underlying part with full JS execution. It depends on RN’s console occasions (keyboardWillChangeFrame on iOS and keyboardDidHide/Show on Android) and, in view of given conduct prop, applies extra cushioning, interpretation, or changes holder’s level.

respond local console mindful parchment view + android:windowSoftInputMode="adjustPan"

Respond local console mindful parchment view is a library with full JS execution that gives an upgraded ScrollView part that responds on console occasions by applying base cushioning and looking to right now centered input. It likewise gives FlatList and SectionList execution. It likewise uncovered listenToKeyboardEvents HOC, which can be utilized to wrap custom parchment parts.

return <>
    <View style={styles.logoContainer}>
      <Image
        resizeMode="contain"
        source={ { uri: 'https://reactnative.dev/img/tiny_logo.png' } }
        style={styles.logo}
      />
    </View>
    <View style={styles.inputsContainer}>
      <TextInput
        placeholder="Single line input"
        style={styles.input}
      />
      <TextInput
        multiline
        placeholder="Multiline input"
        style={[ styles.input, styles.multilineInput ]}
      />
      <TextInput
        multiline
        placeholder="Large multiline input"
        style={[ 
          styles.input,
          styles.multilineInput,
          styles.largeMultilineInput
        ]}
      />
    </View>
    <View style={styles.submitButtonContainer}>
      <Button
        onPress={() => {
          // On submit ...
        }}
        title="Submit"
      />
    </View>
  </>;
};

On a screen with KeyboardAvoidingView, when the console springs up, the top and base pieces of the screen are cut, so the “Submit” button isn’t open. Furthermore, when multiline input is engaged, it is pushed up in any event, when it won’t be covered by a delicate console.

Pushed over the console

In response to the local console mindful parchment view screen on iOS, in the wake of choosing the main information, it is pushed over the console. While choosing huge information, it is situated somewhat over the top edge of the apparent region; however, the remainder of the substance is effectively available.
On Android, the base piece of the screen is marginally cut, yet when the console is shown, inputs are accurately pushed above and provided that required. Here’s a brief summary on Find out how to Scroll to Part in React that you don’t want to miss out.

Conclusion:

While using React Native, users have to find the basic requirements to hide keyboards professionally. It uses the soft input by taking a console with comparable impact. It gives a single line of information that depends on the pushed-over result. It will center around and take the highest point of the ScrollView top edge. It includes console that is apparent and explores clients to look into the part of screen to avoid keyboard in React Native.

Leave a Comment

Your email address will not be published. Required fields are marked *

Let's Get in Touch

Read our customer feedback

Please fill in the form below.


    To top