The Wayback Machine - https://web.archive.org/web/20241008011824/https://www.geeksforgeeks.org/how-to-create-basic-text-input-in-react-native/
Open In App

How to create basic text input in React Native ?

Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Text input is a basic component that allows users to input text through the keyboard. It is widely used in mobile applications for taking usernames, passwords, user details, and much more.

In React Native, we have a TextInput component to achieve this. It comes with many props that help us to modify the component according to our needs.

Some of the essential props are listed below:

  1. placeholder: The string that will be rendered before the user entered any text.
  2. value: The actual value to show for the text input.
  3. defaultValue: Provides an initial value that will change when the user starts typing.
  4. keyboardType: Determines which keyboard to open. e.g.numeric, phone-pad, email-address.
  5. maxLength: Limits the maximum number of characters that can be entered by the user.
  6. multiline: If true, the text input can be multiple lines, the default value is false.
  7. onChangeText: Callback that is called when the text input’s text changes. Changed text is passed as a single string argument to the callback handler.
  8. onSubmitEditing: Callback that is called when the user presses the text input’s submit button.
  9. secureTextEntry: If true, the input string will be encrypted. You can use this for password fields.

Apart from these essential props, React Native provides many other props. You can check that on React Native official documentation. But these are the most important to create a basic text input.

Approach: We will see 2 examples of the TextInput component. In the first example, we create a simple TextInput that takes input text from the user and displays that text in an alert box.

In the second example, we will create a Login Screen in which user can enter their email address and password for logging in.

Creating React Application:

Step 1: Open your Terminal and run the below command. It will install Expo CLI globally in your system.

npm install -g expo-cli

Step 2: Now, create a new React Native Project by running the below command.

expo init "Your_Project_Name"

Step 3:  You’ll be asked to choose a template. Select blank template.

blank template

Step 4: Now go to the project folder and run the below command to start the server.

cd "Your_Project_Name"
npm start

Project Structure: It will look like the following.

Project Structure

Example 1: Open the App.js file and write the below code in that file.

App.js is the main file that renders first when you run your app. Here we will use the TextInput component provided by React Native. We will pass 4 props placeholder, value, onChangeText, and onSubmitEditing in this component.

We will also create a state to store the string inserted by the user. We use the onChangeText prop to set the input string to message state. And when the user presses the submit button on the keyboard, an alert will be invoked with the message.

JavaScript
import { StyleSheet, SafeAreaView, TextInput } from "react-native";
import { useState } from "react";

export default function App() {
    const [message, setMessage] = useState("");

    return (
        <SafeAreaView style={styles.container}>
            <TextInput
                placeholder="enter your message here"
                value={message}
                onChangeText={(text) => setMessage(text)}
                onSubmitEditing={() => alert(`Welcome to ${message}`)}
            />
        </SafeAreaView>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: "#fff",
        alignItems: "center",
        justifyContent: "center",
    },
});

Output:

Example 2: Open the App.js file and write the below code in that file.

in this file, we will create 2 TextInput components, email, and password. Users can enter their email and passwords to log in. We will also use secureTextEntry prop for the password field. And when the user presses the submit button on the keyboard, an alert will show.

We will also apply styling to the TextInput component.

JavaScript
import { StyleSheet, SafeAreaView, TextInput, Text } from "react-native";
import { useState } from "react";

export default function App() {
    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");

    return (
        <SafeAreaView style={styles.container}>
            <Text style={styles.title}>GeeksforGeeks</Text>
            <TextInput
                placeholder="Email"
                value={email}
                onChangeText={(text) => setEmail(text)}
                keyboardType="email-address"
                style={styles.input}
            />
            <TextInput
                placeholder="Password"
                value={password}
                onChangeText={(text) => setPassword(text)}
                keyboardType="visible-password"
                secureTextEntry
                style={styles.input}
                onSubmitEditing={() => alert("Welcome to GeeksforGeeks")}
            />
        </SafeAreaView>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: "#228B22",
        alignItems: "center",
        justifyContent: "center",
    },
    title: {
        fontSize: 40,
        color: "#fff",
        marginBottom: 20,
        fontWeight: "bold",
    },
    input: {
        backgroundColor: "#fff",
        padding: 10,
        width: "80%",
        marginTop: 15,
        color: "#000",
    },
});

Output:



Similar Reads

How to Create a Text input with helper text in react-native?
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: Basi
3 min read
Create a Card View in React native using react-native-paper
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 UIs for both platforms. Approach: In this ar
2 min read
Create an Activity Indicator in react-native using react-native-paper library
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 UIs for both platforms. Prerequisites: Basic
2 min read
How to create Chip in react-native using react-native-paper ?
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: Basi
2 min read
How to create a button in react-native using react-native-paper library ?
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: Basi
2 min read
How to create custom dialog box with text input React Native?
React Native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. In this article, we are going to create a dialog with Text Input. To achieve this we will use React Native's built-in Modal component which we will use as a Dialog. There are two ways to create a React Native
3 min read
How to add a divider in react-native using react-native-paper library ?
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: Basi
2 min read
How to add a Progress Bar in react-native using react-native-paper library ?
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. Approach: In this a
3 min read
React Native Text Input Component
In this article, We are going to see how to create a TextInput in react-native. For this, we are going to use the TextInput component. It is a basic component that is used to collect data from users. For creating a TextInput in react native we have to import the TextInput component from React Native. import { TextInput } from 'react-native'Syntax:
7 min read
How to create a basic button in React Native ?
React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, etc. We’re always looking for shorter development cycles, quicker time to deployment, and better app performance. And there are so many hybrid mobile frameworks such as NativeScript, React Native, Ion
2 min read
Create an Image to Text App using React-Native
In this article, we are going to build a step-by-step Image to Text app using React-Native. This application will allow users to extract the text from the image using OCR (Optical Character Recognition) and later copy it to the clipboard or share it with others. This application is a perfect usage of React Native as the perfect framework for applic
4 min read
Create a Text Editor App using React-Native
In this article, we are going to implement a text editor app using React Native. It will contain multiple text formatting functionalities like bold, italic, underline, etc. We will implement Editor with a library called "react-native-pell-rich-editor." Preview of final output: Let us have a look at how the final output will look like: Prerequisites
3 min read
Create a Text Narrator App using React-Native
In this project, we'll develop a Text Narrator application using React Native. The Text Narrator app is a valuable tool for improving accessibility. It allows users to input text, and the app will convert that text into audible speech. This can be incredibly helpful for individuals with visual impairments or reading difficulties, making it easier f
2 min read
Onsen UI CSS Component Basic Text Input
Onsen UI is a cutting-edge and practical HTML5 framework that is available for usage for free. It simplifies UI development so that app developers may concentrate on the functionality of the software. Because it provides a huge selection of premium UI elements designed especially for mobile apps and packed with attributes that follow native iOS and
3 min read
How to Add Tag Input in React Native ?
Tags are widely used for managing items or categories in UI. Adding a tag input feature in React Native allows users to dynamically add and remove tags, enhancing data organization and filtering in applications. In this article, we will see how we can add tag input in react native application. We are going to create a dynamic tag input component in
4 min read
How to Convert Input Value in Md5 using React Native ?
In this article, we'll explore how to convert an input value to MD5 using React Native. Hashing, a fundamental te­chnique in computer science­, involves converting data of any size into a fixe­d-size value for security or inte­grity purposes. One widely use­d hashing algorithm is MD5 (Message Digest Algorithm 5), although its vulne­rabilities rende
3 min read
How to Add Phone Number Input in React Native ?
React Native is a JavaScript framework for cross-platform mobile app development. In this article, we'll see how to add a phone number input field in a React Native app using Expo CLI. ​Adding a phone number input field in React Native is helpful for collecting user phone numbers during registration or login. It typically consists of a text input c
3 min read
How To Add Strike Through On Text In React Native ?
In this article, we will explore two different approaches to adding a strike-through effect to text in React Native. It is also known as crossed-out text used to display the text as cross-marked. PrerequisitesIntroduction to React NativeReact Native ComponentsReact HooksExpo CLINode.js and npm Syntax<Text style={{ textDecorationLine: 'line-throu
4 min read
How to Copy Text to the Clipboard in React Native ?
Mobile app de­velopment requires enabling text copying to the clipboard for se­amless content sharing and improved use­r experience­. A popular cross-platform framework, React Native, offers a solution to this common requirement. This article will guide develope­rs through implementing clipboard functionality using the @re­act-native-clipboard/clip
3 min read
What is the difference between React Native and React?
In modern web and mobile application development, two technologies have been making waves: React and React Native. Both are open-source frameworks developed by Facebook, designed to streamline and enhance the process of building user interfaces. But what sets them apart? This article will tell the key differences between React, primarily used for w
4 min read
How to Use Routing with React Navigation in React Native ?
Almost every mobile application requires navigating between different screens. React Native provides an elegant and easy-to-use library to add navigation to native applications: react-navigation. It is one of the most popular libraries used for routing and navigating in a React Native application. Transitioning between multiple screens is managed b
4 min read
How to place cursor position at end of text in text input field using JavaScript ?
When working with forms or interactive elements in JavaScript, you might need to control the position of the cursor within a text input field. Specifically, you might want to place the cursor at the end of the current text, allowing users to easily continue typing without disrupting the existing content. In this article, we’ll see how to do this wi
3 min read
How to select all Text in HTML Text Input when clicked using JavaScript?
To select the text in HTML Text Input we can use the JavaScript DOM methods and we will select the text when we click the text box. Using document.getElementById() methodSyntax:<input onClick="this.select();" > or document.getElementById("ID").select();Example 1: We will use "this.select()" in this example to select the text. [GFGTABS] HTML
1 min read
How to create a Snackbar in react native using material design ?
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: Basi
3 min read
How to Create Skeleton Loading in React Native ?
React Native is a popular framework for cross-platform mobile app development. In this ​article we are going to create a skeleton loading effect in React Native, visually indicating content loading to inform users while data is fetched. In mobile app development, it's crucial to ensure a smooth user experience. However, network delays or fetching l
3 min read
Create an OTP Generator and Validator App using React-Native
One-time passwords (OTPs) have become a popular choice for enhancing the security of various online services and applications. In this article, we'll explore how to create an OTP Generator and Validator App using React Native, a popular framework for building cross-platform mobile applications. Preview of final output: Let us have a look at how the
4 min read
Create a Biometric Authentication App using React-Native
Biometric authentication such as fingerprint, facial recognition, and other biometric identifiers, provides a smooth user experience and high-level security. In this tutorial, we will learn to implement a Biometric Authentication App using React-Native. Preview of the final output: Prerequisites & Technolgies Used:Introduction to React NativeRe
3 min read
Create a Music Player using React-Native
React Native is a powerful framework that allows you to build mobile applications using JavaScript and React. In this tutorial, we are going to build a Music Player using React-Native. Here we will load the music files stored in our device and then use the play and pause button to control the music. We can also see the progress of the music played
4 min read
What are the steps to create first React Native App ?
React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, etc. We’re always looking for shorter development cycles, quicker time to deployment, and better app performance. And there are so many hybrid mobile frameworks such as NativeScript, React Native, Ion
2 min read
How to Create Card with Box Shadow in React Native ?
Creating Card with box shadow in react native makes it stand out within the interface and display information in an appealing way. It will be done in React Native by using the expo cli. Expo simplifies cross-platform app development by providing a unified codebase for iOS, Android, and the web. With its tools and services, developers can create hig
4 min read