Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Components and Styling

Touch, Buttons and Text Input

Handle taps and typing in Expo: Pressable states, hit slop and accessibility, controlled TextInput, keyboard types, forms with validation, and KeyboardAvoidingView.

Mobile UIs are touched, not clicked. Fingers are imprecise, there is no hover, and a software keyboard covers half the screen. This lesson covers the components for interaction: Pressable for taps, TextInput for typing, and the keyboard handling that makes forms usable.

The components are read-only code (they need a device); live stand-ins model press states and form validation.

Pressable

Pressable is the building block for anything tappable. (Button exists but cannot be styled much; TouchableOpacity is the older API.)

TSX
import { Pressable, Text, StyleSheet } from 'react-native';

export function PrimaryButton({ label, onPress, disabled }: Props) {
  return (
    <Pressable
      onPress={onPress}
      disabled={disabled}
      accessibilityRole="button"
      accessibilityState={{ disabled }}
      hitSlop={8}
      android_ripple={{ color: '#ffffff55' }}
      style={({ pressed }) => [styles.btn, pressed && styles.pressed, disabled && styles.disabled]}
    >
      <Text style={styles.label}>{label}</Text>
    </Pressable>
  );
}

const styles = StyleSheet.create({
  btn: { backgroundColor: '#0b5fff', paddingVertical: 14, paddingHorizontal: 20, borderRadius: 10, alignItems: 'center' },
  pressed: { opacity: 0.8 },
  disabled: { backgroundColor: '#9db8f5' },
  label: { color: 'white', fontWeight: '700' },
});

The press events, in order: onPressIn (finger down), onPressOut (finger up or moved away), onPress (completed tap), onLongPress (held for delayLongPress ms, default 500). style and children can be functions of { pressed }, so the look updates while touched. Other helpful props:

  • hitSlop: extends the touchable area beyond the visual bounds. Platform guidelines recommend at least 44x44 points (iOS) and 48x48 dp (Android).
  • android_ripple: Material ripple feedback.
  • accessibilityRole, accessibilityLabel, accessibilityHint, accessibilityState: so screen readers announce "Save, button, disabled".
JS Press lifecycle, modeled

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Feedback beyond looks

Combine taps with haptics (expo-haptics, see Sensors and Haptics) for a native feel, and with Alert.alert for confirmations:

TSX
Alert.alert('Delete trail?', 'This cannot be undone.', [
  { text: 'Cancel', style: 'cancel' },
  { text: 'Delete', style: 'destructive', onPress: remove },
]);

TextInput

TextInput is the single-line (or multiline) field. Keep it controlled with state:

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

export function EmailField() {
  const [email, setEmail] = useState('');
  return (
    <View>
      <Text accessibilityRole="header">Email</Text>
      <TextInput
        value={email}
        onChangeText={setEmail}
        placeholder="[email protected]"
        keyboardType="email-address"
        autoCapitalize="none"
        autoComplete="email"
        autoCorrect={false}
        textContentType="emailAddress"
        returnKeyType="next"
        style={{ borderWidth: 1, borderColor: '#bbb', borderRadius: 8, padding: 12 }}
      />
    </View>
  );
}

Props to know:

Prop Purpose
onChangeText gives the string directly (use this, not onChange)
keyboardType default, email-address, numeric, phone-pad, decimal-pad, url
secureTextEntry password dots
autoCapitalize, autoCorrect, autoComplete, textContentType right keyboard behavior and password-manager autofill
returnKeyType label of the return key (next, done, search, send)
onSubmitEditing fired by the return key
multiline, numberOfLines text areas
maxLength, editable, selectTextOnFocus limits and behavior

To move between fields, keep refs and call ref.current?.focus() in onSubmitEditing. To dismiss the keyboard, call Keyboard.dismiss() or use keyboardShouldPersistTaps="handled" and keyboardDismissMode="on-drag" on a surrounding ScrollView.

Validation without surprises

Validate on blur or submit, not on every keystroke (nobody wants "invalid email" after typing a). Keep rules in plain functions you can test.

JS Form validation

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Filtering input as it is typed (digits only) is a legitimate use of onChangeText:

JS Sanitize numeric input

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The keyboard problem

The software keyboard can hide the field being edited. Options, from simplest:

  1. KeyboardAvoidingView: wraps the form and shifts or resizes it. Use behavior="padding" on iOS and typically "height" or nothing on Android (where softwareKeyboardLayoutMode in the app config usually resizes the window already).
  2. A scrolling form: put fields in a ScrollView with keyboardShouldPersistTaps="handled".
  3. react-native-keyboard-controller (needs a development build): smoother, consistent keyboard animations and avoiding views.
TSX
<KeyboardAvoidingView
  style={{ flex: 1 }}
  behavior={Platform.OS === 'ios' ? 'padding' : undefined}
  keyboardVerticalOffset={Platform.OS === 'ios' ? headerHeight : 0}
>
  <ScrollView keyboardShouldPersistTaps="handled" contentContainerStyle={{ padding: 16, gap: 12 }}>
    {/* fields */}
  </ScrollView>
</KeyboardAvoidingView>

For bigger forms use a form library such as React Hook Form with a schema validator (Zod) and keep the field components thin.

Common mistakes

  • Tiny touch targets (icon buttons with no hitSlop).
  • No pressed feedback, so users double-tap.
  • Using onChange and reading event.target.value (that is web).
  • Wrong keyboardType, so users type numbers on a full alphabet keyboard.
  • Uncontrolled fields that reset unexpectedly, or controlled ones with an onChangeText that forgets to set state, making the field uneditable.
  • Validating on every keystroke.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Pressable prop enlarges the touchable area without changing the layout?

    hitSlop extends the pressable region beyond the visual bounds.
  2. 2. What does onChangeText receive?

    Unlike web onChange, onChangeText gives you the string directly.
  3. 3. When is validation feedback usually best shown?

    Early messages while the user is still typing are annoying; validate when they leave the field or submit.
  4. 4. Which component helps keep a focused input visible above the keyboard?

    KeyboardAvoidingView adjusts layout when the keyboard appears.

Summary

  • Use Pressable with state-based styles, hitSlop and accessibility props for every tap target.
  • Keep TextInput controlled; choose keyboardType, autoComplete and returnKeyType deliberately.
  • Validate with pure functions on blur or submit.
  • Handle the keyboard with KeyboardAvoidingView or a scrolling form.

Next: Themes, Dark Mode and Fonts