Open In App

How to Create a Text input with helper text in react-native ?

Improve
Improve
Like Article
Like
Save
Share
Report

React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UI’s for both platforms.

Prerequisites:

Approach: In this article, we will see how to create a text input that shows helper text just below it. We will use a react-native-paper library to show helper texts. Helper text shows the hints. It can be a text that may help the user to fill the input field or it can show error text. We will see the approach step-by-step.

Below is the step by step implementation:

Step 1: Create a project in react-native using the following command:

npx react-native init DemoProject

Step 2: Install react-native paper using the following command:

npm install react-native-paper

Step 3: Create a components folder inside your project. Inside the components folder create a file HelperText.js.

Project Structure: It will look like this.

Example: Write down the code in respective files. In HelperText.js, we have imported HelperText and TextInput from react-native-paper library. HelperText uses props type and visible:

  • type: It can take any of the two values based on the requirement.
  • visible: It can be true or false to show or hide the hints.

HeplerText.js




import React, { useState } from 'react';
import { View, StyleSheet } from 'react-native';
import { HelperText, TextInput } from 'react-native-paper';
  
const HelperTextExample = () => {
  const [emailId, setEmailId] = useState('');
  const [platform, setPlatform] = useState('');
  
  const checkErrors = () => {
    return !emailId.includes('@');
  };
  
  const checkString = () => {
    return platform === 'Geeks';
  };
  
  return (
    <View style={styles.container}>
      <TextInput
        label="Learning Platform"
        value={platform}
        onChangeText={(text) => setPlatform(text)}
      />
      <HelperText visible={checkString()}>Geeks for Geeks</HelperText>
  
      <TextInput label="Email" value={emailId} 
      onChangeText={(text) => setEmailId(text)} />
      <HelperText type="error" visible={checkErrors()}>
        Email Id is invalid
      </HelperText>
    </View>
  );
};
  
export default HelperTextExample;
  
const styles = StyleSheet.create({
  container: {
    padding: 25,
  },
});


App.js




import React from 'react';
import type { Node } from 'react';
import { View } from 'react-native';
  
import HelperTextExample from './components/HelperText';
  
const App: () => Node = () => {
  return (
    <View>
      <HelperTextExample />
    </View>
  );
};
  
export default App;


Step to run the application: Run the application using the following command:

npx react-native run-android

Output:

Reference: https://callstack.github.io/react-native-paper/helper-text.html



Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads