React Native course · Module 1: Your first app

TextInput - Text Fields

4 min read ·
In this lesson4

In Atlantis, communication is key! Let's learn how to receive text from users.

Basic TextInput

import { SafeAreaView, View, Text, TextInput, StyleSheet } from 'react-native';
import { useState } from 'react';

export default function TextInputExample() {
  const [name, setName] = useState('');
  const [message, setMessage] = useState('');

  return (
    <SafeAreaView style={styles.container}>
      <Text style={styles.title}>Message to Poseidon</Text>

      {/* Basic text field */}
      <View style={styles.inputGroup}>
        <Text style={styles.label}>Your name</Text>
        <TextInput
          style={styles.input}
          value={name}
          onChangeText={setName}
          placeholder="Enter your name..."
          placeholderTextColor="#7f8c8d"
        />
      </View>

      {/* Multiline text field */}
      <View style={styles.inputGroup}>
        <Text style={styles.label}>Message</Text>
        <TextInput
          style={[styles.input, styles.textArea]}
          value={message}
          onChangeText={setMessage}
          placeholder="Write a message to the lord of the seas..."
          placeholderTextColor="#7f8c8d"
          multiline={true}
          numberOfLines={4}
          textAlignVertical="top"
        />
      </View>

      {/* Preview */}
      <View style={styles.preview}>
        <Text style={styles.previewText}>
          {name ? `From: ${name}` : 'Enter your name'}
        </Text>
        <Text style={styles.previewMessage}>
          {message || 'Write a message...'}
        </Text>
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#1a5276',
    padding: 20,
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
    color: '#fff',
    marginBottom: 24,
    textAlign: 'center',
  },
  inputGroup: {
    marginBottom: 16,
  },
  label: {
    fontSize: 14,
    color: '#aed6f1',
    marginBottom: 8,
    fontWeight: '500',
  },
  input: {
    backgroundColor: '#2c3e50',
    borderRadius: 8,
    padding: 14,
    fontSize: 16,
    color: '#fff',
    borderWidth: 1,
    borderColor: '#34495e',
  },
  textArea: {
    minHeight: 100,
  },
  preview: {
    backgroundColor: '#2c3e50',
    borderRadius: 12,
    padding: 16,
    marginTop: 20,
  },
  previewText: {
    fontSize: 14,
    color: '#3498db',
    marginBottom: 8,
    fontWeight: '600',
  },
  previewMessage: {
    fontSize: 16,
    color: '#fff',
    lineHeight: 22,
  },
});

Different Keyboard Types

import { SafeAreaView, View, Text, TextInput, StyleSheet, ScrollView } from 'react-native';
import { useState } from 'react';

export default function KeyboardTypesExample() {
  const [email, setEmail] = useState('');
  const [phone, setPhone] = useState('');
  const [amount, setAmount] = useState('');
  const [password, setPassword] = useState('');
  const [url, setUrl] = useState('');

  return (
    <SafeAreaView style={styles.container}>
      <ScrollView contentContainerStyle={styles.scrollContent}>
        <Text style={styles.title}>Keyboard Types</Text>

        {/* Email */}
        <View style={styles.inputGroup}>
          <Text style={styles.label}>Email</Text>
          <TextInput
            style={styles.input}
            value={email}
            onChangeText={setEmail}
            placeholder="email@atlantis.com"
            placeholderTextColor="#7f8c8d"
            keyboardType="email-address"
            autoCapitalize="none"
            autoCorrect={false}
          />
        </View>

        {/* Phone */}
        <View style={styles.inputGroup}>
          <Text style={styles.label}>Phone</Text>
          <TextInput
            style={styles.input}
            value={phone}
            onChangeText={setPhone}
            placeholder="+1 234 567 890"
            placeholderTextColor="#7f8c8d"
            keyboardType="phone-pad"
          />
        </View>

        {/* Amount */}
        <View style={styles.inputGroup}>
          <Text style={styles.label}>Amount (gold)</Text>
          <TextInput
            style={styles.input}
            value={amount}
            onChangeText={setAmount}
            placeholder="0.00"
            placeholderTextColor="#7f8c8d"
            keyboardType="decimal-pad"
          />
        </View>

        {/* Password */}
        <View style={styles.inputGroup}>
          <Text style={styles.label}>Password</Text>
          <TextInput
            style={styles.input}
            value={password}
            onChangeText={setPassword}
            placeholder="••••••••"
            placeholderTextColor="#7f8c8d"
            secureTextEntry={true}
            autoCapitalize="none"
          />
        </View>

        {/* URL */}
        <View style={styles.inputGroup}>
          <Text style={styles.label}>URL Address</Text>
          <TextInput
            style={styles.input}
            value={url}
            onChangeText={setUrl}
            placeholder="https://atlantis.com"
            placeholderTextColor="#7f8c8d"
            keyboardType="url"
            autoCapitalize="none"
            autoCorrect={false}
          />
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#1a5276',
  },
  scrollContent: {
    padding: 20,
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
    color: '#fff',
    marginBottom: 24,
    textAlign: 'center',
  },
  inputGroup: {
    marginBottom: 16,
  },
  label: {
    fontSize: 14,
    color: '#aed6f1',
    marginBottom: 8,
    fontWeight: '500',
  },
  input: {
    backgroundColor: '#2c3e50',
    borderRadius: 8,
    padding: 14,
    fontSize: 16,
    color: '#fff',
    borderWidth: 1,
    borderColor: '#34495e',
  },
});

Keyboard Handling

import {
  SafeAreaView,
  View,
  Text,
  TextInput,
  StyleSheet,
  KeyboardAvoidingView,
  Platform,
  Pressable,
  Keyboard
} from 'react-native';
import { useState } from 'react';

export default function KeyboardHandlingExample() {
  const [message, setMessage] = useState('');

  return (
    <SafeAreaView style={styles.container}>
      {/* KeyboardAvoidingView moves content when the keyboard appears */}
      <KeyboardAvoidingView
        behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
        style={styles.keyboardView}
      >
        {/* Tapping outside the input hides the keyboard */}
        <Pressable style={styles.inner} onPress={Keyboard.dismiss}>
          <Text style={styles.title}>Message in a Bottle</Text>

          <View style={styles.inputContainer}>
            <TextInput
              style={styles.input}
              value={message}
              onChangeText={setMessage}
              placeholder="Write a message..."
              placeholderTextColor="#7f8c8d"
              multiline
              returnKeyType="send"
              onSubmitEditing={() => {
                console.log('Sent:', message);
                setMessage('');
                Keyboard.dismiss();
              }}
            />

            <Pressable
              style={styles.sendButton}
              onPress={() => {
                console.log('Sent:', message);
                setMessage('');
                Keyboard.dismiss();
              }}
            >
              <Text style={styles.sendButtonText}>Send</Text>
            </Pressable>
          </View>
        </Pressable>
      </KeyboardAvoidingView>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#1a5276',
  },
  keyboardView: {
    flex: 1,
  },
  inner: {
    flex: 1,
    justifyContent: 'flex-end',
    padding: 20,
  },
  title: {
    fontSize: 20,
    fontWeight: 'bold',
    color: '#fff',
    textAlign: 'center',
    marginBottom: 20,
  },
  inputContainer: {
    flexDirection: 'row',
    alignItems: 'flex-end',
    gap: 10,
    marginBottom: 20,
  },
  input: {
    flex: 1,
    backgroundColor: '#2c3e50',
    borderRadius: 20,
    paddingHorizontal: 16,
    paddingVertical: 12,
    fontSize: 16,
    color: '#fff',
    maxHeight: 100,
  },
  sendButton: {
    width: 48,
    height: 48,
    borderRadius: 24,
    backgroundColor: '#3498db',
    justifyContent: 'center',
    alignItems: 'center',
  },
  sendButtonText: {
    fontSize: 20,
  },
});

Keyboard Types Summary

keyboardTypeDescriptionUsage
defaultStandardGeneral text
email-addressWith @ and .Email
numericNumbers onlyWhole numbers
decimal-padNumbers with decimalAmounts, decimal numbers
phone-padPhone numberPhone
number-padNumeric keypadPIN, codes
urlWith / and .comURL addresses
Code for this lesson: App.tsx
import React, { useState } from 'react';
import { View, Text, TextInput, StyleSheet } from 'react-native';

export default function App() {
  const [name, setName] = useState('');

  return (
    <View style={styles.container}>
      {/* TODO: add a <TextInput> that:
          - has value={name}
          - updates state via onChangeText={setName}
          - has placeholder="Enter name..."
          - uses the styles.input style */}

      <Text style={styles.label}>Welcome, {name || 'resident of Atlantis'}!</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    justifyContent: 'center',
    backgroundColor: '#1a5276',
  },
  input: {
    backgroundColor: '#2c3e50',
    borderRadius: 8,
    padding: 14,
    fontSize: 16,
    color: '#fff',
    marginBottom: 16,
  },
  label: {
    fontSize: 18,
    color: '#aed6f1',
  },
});

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. Which keyboardType will you use for an email field?

  2. 2. Which prop hides the typed text (for a password)?

  3. 3. What is KeyboardAvoidingView used for?

Hands-on tasks in the game

  • Code editor

    Fill in the TextInput component

  1. QuestionQuiz: Keyboard type for email
  2. QuestionQuiz: Hiding a password
  3. QuestionQuiz: KeyboardAvoidingView
  4. Coding taskFill in: TextInput

Useful articles