Kurs React Native · Moduł 1: Pierwsza aplikacja
Przyciski i interakcja z użytkownikiem
W tej lekcji4
W Atlantydzie każdy przycisk to brama do nowej funkcjonalności! Poznajmy różne sposoby obsługi dotyku.
Pressable - nowoczesny przycisk
Pressable to najnowszy i najbardziej elastyczny komponent do obsługi dotyku:
import { View, Text, Pressable, StyleSheet, Alert } from 'react-native';
export default function PressableExample() {
const handlePress = () => {
Alert.alert('Atlantyda', 'Odkryłeś sekret głębin!');
};
return (
<View style={styles.container}>
{/* Podstawowy przycisk */}
<Pressable
style={styles.button}
onPress={handlePress}
>
<Text style={styles.buttonText}>Naciśnij mnie</Text>
</Pressable>
{/* Przycisk z efektem naciśnięcia */}
<Pressable
style={({ pressed }) => [
styles.button,
styles.primaryButton,
pressed && styles.buttonPressed,
]}
onPress={() => console.log('Pressed!')}
>
{({ pressed }) => (
<Text style={[styles.buttonText, pressed && styles.textPressed]}>
{pressed ? 'Naciśnięty!' : 'Naciśnij'}
</Text>
)}
</Pressable>
{/* Przycisk disabled */}
<Pressable
style={[styles.button, styles.disabledButton]}
disabled={true}
>
<Text style={[styles.buttonText, styles.disabledText]}>
Niedostępny
</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 - przycisk z efektem przezroczystości
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
export default function TouchableExample() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.card}
activeOpacity={0.7} // Przezroczystość po naciśnięciu (0-1)
onPress={() => console.log('Card pressed')}
>
<Text style={styles.cardTitle}>Muszla mądrości</Text>
<Text style={styles.cardDescription}>
Dotknij, aby odkryć tajemnicę oceanu
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.iconButton}
activeOpacity={0.6}
onPress={() => console.log('Heart pressed')}
>
<Text style={styles.icon}>Lubię</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 na 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,
},
});Obsługa różnych gestów
import { View, Text, Pressable, StyleSheet } from 'react-native';
import { useState } from 'react';
export default function GesturesExample() {
const [lastEvent, setLastEvent] = useState('Oczekiwanie...');
return (
<View style={styles.container}>
<Text style={styles.status}>Ostatnie zdarzenie: {lastEvent}</Text>
<Pressable
style={styles.gestureArea}
onPress={() => setLastEvent('onPress - krótkie naciśnięcie')}
onLongPress={() => setLastEvent('onLongPress - długie naciśnięcie')}
onPressIn={() => console.log('Palec dotknął ekranu')}
onPressOut={() => console.log('Palec opuścił ekran')}
delayLongPress={500} // ms do uznania za long press
>
<Text style={styles.gestureText}>
Dotknij lub przytrzymaj ten obszar
</Text>
</Pressable>
<View style={styles.buttonRow}>
<Pressable
style={[styles.smallButton, styles.greenButton]}
onPress={() => setLastEvent('Tak!')}
>
<Text style={styles.smallButtonText}>Tak</Text>
</Pressable>
<Pressable
style={[styles.smallButton, styles.redButton]}
onPress={() => setLastEvent('Nie!')}
>
<Text style={styles.smallButtonText}>Nie</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 - okna dialogowe
import { Alert, View, Pressable, Text, StyleSheet } from 'react-native';
export default function AlertExample() {
// Prosty alert
const showSimpleAlert = () => {
Alert.alert('Atlantyda mówi', 'Witaj w podwodnym świecie!');
};
// Alert z przyciskami
const showConfirmAlert = () => {
Alert.alert(
'Czy chcesz kontynuować?',
'Ta akcja przeniesie Cię do głębin oceanu.',
[
{
text: 'Anuluj',
style: 'cancel',
onPress: () => console.log('Anulowano'),
},
{
text: 'Tak, zanurzam się!',
style: 'default',
onPress: () => console.log('Kontynuacja'),
},
]
);
};
// Alert destrukcyjny
const showDestructiveAlert = () => {
Alert.alert(
'Usuń dane?',
'Tej akcji nie można cofnąć!',
[
{ text: 'Anuluj', style: 'cancel' },
{ text: 'Usuń', style: 'destructive', onPress: () => console.log('Usunięto') },
]
);
};
return (
<View style={styles.container}>
<Pressable style={styles.button} onPress={showSimpleAlert}>
<Text style={styles.buttonText}>Prosty Alert</Text>
</Pressable>
<Pressable style={styles.button} onPress={showConfirmAlert}>
<Text style={styles.buttonText}>Alert z potwierdzeniem</Text>
</Pressable>
<Pressable style={[styles.button, styles.dangerButton]} onPress={showDestructiveAlert}>
<Text style={styles.buttonText}>Alert destrukcyjny</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',
},
});Kod do tej lekcji: App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet, Alert } from 'react-native';
export default function App() {
const handlePress = () => {
Alert.alert('Atlantyda', 'Odkryłeś sekret głębin!');
};
return (
<View style={styles.container}>
{/* TODO: dodaj <Pressable> z propem onPress={handlePress} i stylem styles.button */}
{/* Wewnątrz Pressable umieść <Text style={styles.buttonText}>Naciśnij mnie</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',
},
});Hasła z dokumentacji w tej lekcji
Widzisz błąd w tej lekcji?
Sprawdź się
Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.
1. Który komponent jest najnowszym i najbardziej elastycznym do obsługi dotyku?
2. Jak nazywa się prop do obsługi kliknięcia w React Native (zamiast onClick)?
Zadania praktyczne w grze
- Układanie w pionie
Ułóż argumenty Alert.alert() w odpowiedniej kolejności: