Lesson 19 of 60 – TextInput Component in React Native
32%

TextInput Component in React Native

The TextInput component is used to allow users to enter text into a React Native application. It is one of the most important components for creating forms such as login forms, registration forms, search boxes, feedback forms and profile screens.

Note: TextInput can be controlled using React state. This allows your application to read, validate, update and submit the value entered by the user.

1. What is TextInput?

TextInput is a built-in React Native component used to accept text entered by the user.

import { TextInput } from "react-native";

It can be used for names, emails, passwords, search queries and many other types of user input.

2. Importing TextInput

TextInput is imported from the react-native package.

import {
    TextInput
} from "react-native";

It can also be imported with View and Text.

import {
    View,
    Text,
    TextInput
} from "react-native";

3. Basic TextInput Syntax

<TextInput
    placeholder="Enter your name"
/>

The placeholder gives the user an idea of what should be entered.

4. placeholder Prop

The placeholder prop displays temporary hint text when the input is empty.

<TextInput
    placeholder="Enter your name"
/>

The placeholder disappears when the user starts entering text.

5. Styling TextInput

The style prop is used to control the appearance of TextInput.

const styles = StyleSheet.create({

    input: {
        borderWidth: 1,
        borderColor: "gray",
        padding: 12,
        borderRadius: 8
    }

});
<TextInput
    placeholder="Enter your name"
    style={styles.input}
/>

6. value Prop

The value prop specifies the current value displayed in the input.

const name = "Rahul";

<TextInput
    value={name}
/>

When using React state, the value can be controlled dynamically.

7. onChangeText

The onChangeText prop runs whenever the text changes.

<TextInput
    placeholder="Enter name"
    onChangeText={(text) => {
        console.log(text);
    }}
/>

The updated text is passed to the callback function.

8. TextInput with useState

React state is commonly used to store the value entered by the user.

import { useState } from "react";

const [name, setName] =
    useState("");

<TextInput
    value={name}
    onChangeText={setName}
    placeholder="Enter name"
/>

9. Displaying Input Value

The value entered into TextInput can be displayed using the state variable.

const [name, setName] =
    useState("");

<TextInput
    value={name}
    onChangeText={setName}
    placeholder="Enter name"
/>

<Text>
    Name: {name}
</Text>

10. Keyboard Type

The keyboardType prop can suggest an appropriate keyboard for the type of data being entered.

<TextInput
    keyboardType="numeric"
    placeholder="Enter age"
/>

Other useful keyboard types are available depending on the platform and input requirement.

11. Numeric Input

For numeric input, you can use a numeric keyboard type.

<TextInput
    keyboardType="numeric"
    placeholder="Enter mobile number"
/>

Remember that the value received from TextInput is still text unless you explicitly convert it to a number.

12. Email Input

For email fields, an email-friendly keyboard can be requested.

<TextInput
    keyboardType="email-address"
    placeholder="Enter email"
/>

This provides a keyboard configuration appropriate for entering an email address.

13. Secure Text Entry

The secureTextEntry prop is commonly used for passwords.

<TextInput
    placeholder="Enter password"
    secureTextEntry={true}
/>

The entered characters are visually obscured.

14. Password Input

const [password, setPassword] =
    useState("");

<TextInput
    value={password}
    onChangeText={setPassword}
    placeholder="Enter password"
    secureTextEntry
/>

This pattern is commonly used in login and registration screens.

15. maxLength

The maxLength prop can limit the number of characters that the user can enter.

<TextInput
    placeholder="Enter PIN"
    maxLength={6}
/>

This can be useful for PINs, codes and other fields with fixed length requirements.

16. multiline

The multiline prop allows the user to enter multiple lines of text.

<TextInput
    placeholder="Enter your message"
    multiline
    style={{
        height: 120
    }}
/>

This is useful for comments, descriptions and messages.

17. Number of Lines

For multiline input, numberOfLines can be used to provide an initial visual size.

<TextInput
    multiline
    numberOfLines={4}
    placeholder="Enter description"
/>

The exact behavior can vary between platforms.

18. autoCapitalize

The autoCapitalize prop controls automatic capitalization behavior for text input.

<TextInput
    autoCapitalize="words"
    placeholder="Enter your name"
/>

Common values include none, sentences, words and characters.

19. autoCorrect

The autoCorrect prop controls whether automatic text correction is enabled.

<TextInput
    autoCorrect={false}
    placeholder="Enter username"
/>

Disabling correction can be useful for usernames, codes and other text where automatic corrections are undesirable.

20. Editable Prop

The editable prop controls whether the user can modify the text.

<TextInput
    value="Read only information"
    editable={false}
/>

When editable is false, the user cannot change the input value.

21. onFocus and onBlur

TextInput provides events for detecting when the input receives or loses focus.

<TextInput
    placeholder="Enter name"
    onFocus={() => {
        console.log("Input focused");
    }}
    onBlur={() => {
        console.log("Input lost focus");
    }}
/>

22. onSubmitEditing

The onSubmitEditing event can be used to respond when the user submits the input using the keyboard's action key.

<TextInput
    placeholder="Enter name"
    onSubmitEditing={() => {
        console.log("Submitted");
    }}
/>

This can be useful when moving from one input field to another or submitting a form.

23. returnKeyType

The returnKeyType prop can influence the action key shown by the keyboard.

<TextInput
    placeholder="Enter name"
    returnKeyType="next"
/>

The available options can vary by platform.

24. TextInput with Button

TextInput and Button are commonly combined to create forms.

const [name, setName] =
    useState("");

<TextInput
    value={name}
    onChangeText={setName}
    placeholder="Enter name"
/>

<Button
    title="Submit"
    onPress={() => {
        console.log(name);
    }}
/>

25. TextInput Validation

Input values should often be validated before they are submitted.

const [name, setName] =
    useState("");

function handleSubmit() {

    if (name.trim() === "") {

        alert("Please enter your name");

        return;
    }

    alert("Submitted");

}

Validation rules should depend on the purpose of the input field.

26. Login Form with TextInput

const [email, setEmail] =
    useState("");

const [password, setPassword] =
    useState("");

<TextInput
    value={email}
    onChangeText={setEmail}
    placeholder="Email"
    keyboardType="email-address"
/>

<TextInput
    value={password}
    onChangeText={setPassword}
    placeholder="Password"
    secureTextEntry
/>

<Button
    title="Login"
    onPress={() => {

        console.log(
            email,
            password
        );

    }}
/>

27. Complete TextInput Example

import {
    View,
    Text,
    TextInput,
    Button,
    StyleSheet
} from "react-native";

import { useState } from "react";

export default function App() {

    const [name, setName] =
        useState("");

    const [email, setEmail] =
        useState("");

    function handleSubmit() {

        if (name.trim() === "") {

            alert("Enter your name");

            return;
        }

        alert(
            "Welcome " + name
        );

    }

    return (
        <View style={styles.container}>

            <Text style={styles.title}>
                Student Form
            </Text>

            <TextInput
                style={styles.input}
                value={name}
                onChangeText={setName}
                placeholder="Enter your name"
                autoCapitalize="words"
            />

            <TextInput
                style={styles.input}
                value={email}
                onChangeText={setEmail}
                placeholder="Enter your email"
                keyboardType="email-address"
                autoCapitalize="none"
            />

            <Button
                title="Submit"
                onPress={handleSubmit}
            />

        </View>
    );
}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        justifyContent: "center",
        padding: 20
    },

    title: {
        fontSize: 26,
        fontWeight: "bold",
        marginBottom: 20,
        textAlign: "center"
    },

    input: {
        borderWidth: 1,
        borderColor: "gray",
        padding: 12,
        marginBottom: 15,
        borderRadius: 8
    }

});

28. Common TextInput Mistakes

  • Forgetting to update state in onChangeText.
  • Using a controlled input without managing its value correctly.
  • Forgetting secureTextEntry for password fields.
  • Not validating important user input.
  • Using the wrong keyboard type for the field.
  • Allowing unlimited input when a maximum length is required.
  • Forgetting keyboard behavior when building long forms.

29. Best Practices for TextInput

  • Use meaningful placeholder text.
  • Use React state for controlled inputs.
  • Use the appropriate keyboardType.
  • Use secureTextEntry for password fields.
  • Validate input before processing it.
  • Use maxLength where appropriate.
  • Use multiline for longer text.
  • Use clear labels and accessible form designs.
  • Test forms on both Android and iOS when supporting both platforms.

30. Complete TextInput Workflow

TextInput
 ↓
Show Placeholder
 ↓
Accept User Input
 ↓
Store in State
 ↓
Validate Value
 ↓
Update State
 ↓
Submit Data
 ↓
Send to API / Process
 ↓
Show Result
 ↓
Complete Form

TextInput is one of the most important components for building interactive mobile applications. It allows your users to enter data that can later be validated, stored, submitted to an API or used by other parts of the application.

📌 Key Points

  • TextInput allows users to enter text.
  • placeholder displays hint text.
  • value controls the current input value.
  • onChangeText detects changes in the input.
  • React state is commonly used with controlled TextInput components.
  • keyboardType can provide an appropriate keyboard.
  • secureTextEntry is useful for password fields.
  • multiline allows multiple lines of input.
  • maxLength can limit the input length.
  • TextInput can be combined with Button to create forms.
  • Input values should be validated before important operations.

🧠 Quick Quiz

Question: Which prop is commonly used to detect changes in the text entered into a React Native TextInput?