React Native course ยท Module 1: Your first app

Buttons and User Interaction

4 min read ยท
In this lesson4

In Atlantis, every button is a gateway to new functionality! Let's learn different ways to handle touch.

Pressable - Modern Button

Pressable is the newest and most flexible component for handling touch:

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

export default function PressableExample() {
  const handlePress = () => {
    Alert.alert('Atlantis', 'You discovered a secret of the depths!');
  };

  return (
    <View style={styles.container}>
      {/* Basic button */}
      <Pressable
        style={styles.button}
        onPress={handlePress}
      >
        <Text style={styles.buttonText}>Press me</Text>
      </Pressable>

      {/* Button with press effect */}
      <Pressable
        style={({ pressed }) => [
          styles.button,
          styles.primaryButton,
          pressed && styles.buttonPressed,
        ]}
        onPress={() => console.log('Pressed!')}
      >
        {({ pressed }) => (
          <Text style={[styles.buttonText, pressed && styles.textPressed]}>
            {pressed ? 'Pressed!' : 'Press'}
          </Text>
        )}
      </Pressable>

      {/* Disabled button */}
      <Pressable
        style={[styles.button, styles.disabledButton]}
        disabled={true}
      >
        <Text style={[styles.buttonText, styles.disabledText]}>
          Unavailable
        </Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    gap: 16,
    backgroundColor: '#1a5276',
  },
  button: {
    paddingVertical: 14,
    paddingHorizontal: 28,
    borderRadius: 8,
    backgroundColor: '#2980b9',
    minWidth: 200,
    alignItems: 'center',
  },
  primaryButton: {
    backgroundColor: '#27ae60',
  },
  buttonPressed: {
    backgroundColor: '#1e8449',
    transform: [{ scale: 0.98 }],
  },
  disabledButton: {
    backgroundColor: '#7f8c8d',
  },
  buttonText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
  textPressed: {
    color: '#d5f5e3',
  },
  disabledText: {
    color: '#bdc3c7',
  },
});

TouchableOpacity - Button with Opacity Effect

import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function TouchableExample() {
  return (
    <View style={styles.container}>
      <TouchableOpacity
        style={styles.card}
        activeOpacity={0.7} // Opacity when pressed (0-1)
        onPress={() => console.log('Card pressed')}
      >
        <Text style={styles.cardTitle}>Shell of Wisdom</Text>
        <Text style={styles.cardDescription}>
          Touch to discover the secret of the ocean
        </Text>
      </TouchableOpacity>

      <TouchableOpacity
        style={styles.iconButton}
        activeOpacity={0.6}
        onPress={() => console.log('Heart pressed')}
      >
        <Text style={styles.icon}>Like</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    gap: 20,
    backgroundColor: '#1a5276',
  },
  card: {
    backgroundColor: '#2c3e50',
    padding: 20,
    borderRadius: 12,
    width: '80%',
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 4 },
    shadowOpacity: 0.3,
    shadowRadius: 8,
    elevation: 5, // Shadow on Android
  },
  cardTitle: {
    fontSize: 18,
    fontWeight: 'bold',
    color: '#fff',
    marginBottom: 8,
  },
  cardDescription: {
    fontSize: 14,
    color: '#bdc3c7',
  },
  iconButton: {
    width: 60,
    height: 60,
    borderRadius: 30,
    backgroundColor: '#e74c3c',
    justifyContent: 'center',
    alignItems: 'center',
  },
  icon: {
    fontSize: 28,
  },
});

Handling Different Gestures

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

export default function GesturesExample() {
  const [lastEvent, setLastEvent] = useState('Waiting...');

  return (
    <View style={styles.container}>
      <Text style={styles.status}>Last event: {lastEvent}</Text>

      <Pressable
        style={styles.gestureArea}
        onPress={() => setLastEvent('onPress - short press')}
        onLongPress={() => setLastEvent('onLongPress - long press')}
        onPressIn={() => console.log('Finger touched the screen')}
        onPressOut={() => console.log('Finger left the screen')}
        delayLongPress={500} // ms until recognized as long press
      >
        <Text style={styles.gestureText}>
          Touch or hold this area
        </Text>
      </Pressable>

      <View style={styles.buttonRow}>
        <Pressable
          style={[styles.smallButton, styles.greenButton]}
          onPress={() => setLastEvent('Yes!')}
        >
          <Text style={styles.smallButtonText}>Yes</Text>
        </Pressable>

        <Pressable
          style={[styles.smallButton, styles.redButton]}
          onPress={() => setLastEvent('No!')}
        >
          <Text style={styles.smallButtonText}>No</Text>
        </Pressable>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 20,
    backgroundColor: '#1a5276',
  },
  status: {
    fontSize: 16,
    color: '#aed6f1',
    marginBottom: 30,
    textAlign: 'center',
  },
  gestureArea: {
    width: '100%',
    height: 150,
    backgroundColor: '#2c3e50',
    borderRadius: 12,
    justifyContent: 'center',
    alignItems: 'center',
    borderWidth: 2,
    borderColor: '#3498db',
    borderStyle: 'dashed',
    marginBottom: 30,
  },
  gestureText: {
    fontSize: 16,
    color: '#fff',
    textAlign: 'center',
  },
  buttonRow: {
    flexDirection: 'row',
    gap: 20,
  },
  smallButton: {
    paddingVertical: 12,
    paddingHorizontal: 30,
    borderRadius: 8,
  },
  greenButton: {
    backgroundColor: '#27ae60',
  },
  redButton: {
    backgroundColor: '#e74c3c',
  },
  smallButtonText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
});

Alert - Dialog Windows

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

export default function AlertExample() {
  // Simple alert
  const showSimpleAlert = () => {
    Alert.alert('Atlantis says', 'Welcome to the underwater world!');
  };

  // Alert with buttons
  const showConfirmAlert = () => {
    Alert.alert(
      'Do you want to continue?',
      'This action will take you to the ocean depths.',
      [
        {
          text: 'Cancel',
          style: 'cancel',
          onPress: () => console.log('Canceled'),
        },
        {
          text: 'Yes, I\'m diving in!',
          style: 'default',
          onPress: () => console.log('Continuing'),
        },
      ]
    );
  };

  // Destructive alert
  const showDestructiveAlert = () => {
    Alert.alert(
      'Delete data?',
      'This action cannot be undone!',
      [
        { text: 'Cancel', style: 'cancel' },
        { text: 'Delete', style: 'destructive', onPress: () => console.log('Deleted') },
      ]
    );
  };

  return (
    <View style={styles.container}>
      <Pressable style={styles.button} onPress={showSimpleAlert}>
        <Text style={styles.buttonText}>Simple Alert</Text>
      </Pressable>

      <Pressable style={styles.button} onPress={showConfirmAlert}>
        <Text style={styles.buttonText}>Confirmation Alert</Text>
      </Pressable>

      <Pressable style={[styles.button, styles.dangerButton]} onPress={showDestructiveAlert}>
        <Text style={styles.buttonText}>Destructive Alert</Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    gap: 16,
    backgroundColor: '#1a5276',
  },
  button: {
    backgroundColor: '#3498db',
    paddingVertical: 14,
    paddingHorizontal: 28,
    borderRadius: 8,
    minWidth: 220,
    alignItems: 'center',
  },
  dangerButton: {
    backgroundColor: '#e74c3c',
  },
  buttonText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
});
Code for this lesson: App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet, Alert } from 'react-native';

export default function App() {
  const handlePress = () => {
    Alert.alert('Atlantis', 'You discovered a secret of the deep!');
  };

  return (
    <View style={styles.container}>
      {/* TODO: add a <Pressable> with onPress={handlePress} and style={styles.button} */}
      {/* Inside the Pressable, place <Text style={styles.buttonText}>Press me</Text> */}

    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#1a5276',
  },
  button: {
    paddingVertical: 14,
    paddingHorizontal: 28,
    borderRadius: 8,
    backgroundColor: '#2980b9',
  },
  buttonText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
});

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 component is the newest and most flexible for handling touch?

  2. 2. What is the prop for handling a click in React Native called (instead of onClick)?

Hands-on tasks in the game

  • Vertical ordering

    Arrange the arguments of Alert.alert() in the correct order:

  1. QuestionQuiz: Component for handling touch
  2. QuestionQuiz: Click event
  3. Put in orderSort: Alert with confirmation

Useful articles