Kurs React Native · Moduł 1: Pierwsza aplikacja
Podstawowe komponenty React Native
W tej lekcji5
W React Native nie używamy HTML - zamiast tego mamy natywne komponenty. Poznajmy najważniejsze z nich!
View - kontener (odpowiednik div)
View to podstawowy kontener, jak <div> w HTML:
import { View, StyleSheet } from 'react-native';
export default function ContainerExample() {
return (
<View style={styles.outer}>
<View style={styles.box1} />
<View style={styles.box2} />
<View style={styles.box3} />
</View>
);
}
const styles = StyleSheet.create({
outer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
backgroundColor: '#1a5276',
},
box1: {
width: 80,
height: 80,
backgroundColor: '#3498db',
borderRadius: 10,
},
box2: {
width: 80,
height: 80,
backgroundColor: '#2ecc71',
borderRadius: 40, // Koło
},
box3: {
width: 80,
height: 80,
backgroundColor: '#e74c3c',
borderRadius: 10,
},
});Text - wyświetlanie tekstu
W React Native cały tekst musi być w komponencie Text:
import { View, Text, StyleSheet } from 'react-native';
export default function TextExample() {
const userName = "Podróżniku";
return (
<View style={styles.container}>
{/* Podstawowy tekst */}
<Text style={styles.heading}>Witaj w Atlantydzie!</Text>
{/* Tekst z interpolacją */}
<Text style={styles.greeting}>Cześć, {userName}!</Text>
{/* Zagnieżdżony tekst (inline styles) */}
<Text style={styles.paragraph}>
Atlantyda to <Text style={styles.bold}>zatopione miasto</Text> pełne{' '}
<Text style={styles.italic}>tajemnic</Text> i{' '}
<Text style={styles.highlight}>skarbów</Text>.
</Text>
{/* Wieloliniowy tekst */}
<Text style={styles.multiline} numberOfLines={3}>
Ten tekst zostanie obcięty po trzech liniach jeśli będzie za długi.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation.
</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#1a5276',
},
heading: {
fontSize: 24,
fontWeight: 'bold',
color: '#fff',
marginBottom: 16,
textAlign: 'center',
},
greeting: {
fontSize: 18,
color: '#aed6f1',
marginBottom: 16,
},
paragraph: {
fontSize: 16,
color: '#fff',
lineHeight: 24,
marginBottom: 16,
},
bold: {
fontWeight: 'bold',
},
italic: {
fontStyle: 'italic',
},
highlight: {
color: '#f1c40f',
fontWeight: 'bold',
},
multiline: {
fontSize: 14,
color: '#bdc3c7',
lineHeight: 20,
},
});Image - wyświetlanie obrazów
import { View, Image, StyleSheet } from 'react-native';
export default function ImageExample() {
return (
<View style={styles.container}>
{/* Obraz lokalny */}
<Image
source={require('../assets/images/atlantis-logo.png')}
style={styles.localImage}
/>
{/* Obraz z URL */}
<Image
source={{ uri: 'https://picsum.photos/200/200' }}
style={styles.remoteImage}
/>
{/* Obraz z różnymi trybami skalowania */}
<Image
source={{ uri: 'https://picsum.photos/400/200' }}
style={styles.coverImage}
resizeMode="cover" // cover, contain, stretch, center
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
padding: 20,
backgroundColor: '#1a5276',
},
localImage: {
width: 100,
height: 100,
marginBottom: 20,
},
remoteImage: {
width: 150,
height: 150,
borderRadius: 75, // Okrągły obraz
marginBottom: 20,
},
coverImage: {
width: '100%',
height: 200,
borderRadius: 10,
},
});Różnice między React (Web) a React Native
| React (Web) | React Native | Opis |
|---|---|---|
<div> | <View> | Kontener |
<span>, <p>, <h1> | <Text> | Tekst |
<img> | <Image> | Obrazy |
<input> | <TextInput> | Pola tekstowe |
<button> | <Pressable>, <TouchableOpacity> | Przyciski |
<ul>, <li> | <FlatList> | Listy |
<a> | <Link> (expo-router) | Linki |
| CSS classes | StyleSheet | Style |
onClick | onPress | Eventy kliknięcia |
Ważne zasady
Tekst MUSI być w
<Text>- inaczej błąd!// Błąd! <View>Hello</View> // Poprawnie <View><Text>Hello</Text></View>Style jako obiekty JavaScript
// Nie ma CSS classes <View className="container"> // StyleSheet lub inline <View style={styles.container}> <View style={{ flex: 1 }}>Flexbox domyślnie w kolumnie
// Web: flex-direction: row (domyślnie) // React Native: flexDirection: 'column' (domyślnie)
Kod do tej lekcji: App.tsx
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<Text style={styles.text}>Witaj w Atlantydzie!</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
// TODO: ustaw flex: 1, aby kontener wypełnił cały ekran
// TODO: wycentruj zawartość w pionie (justifyContent: 'center')
// TODO: wycentruj zawartość w poziomie (alignItems: 'center')
backgroundColor: '#1a5276',
},
text: {
fontSize: 22,
color: '#fff',
},
});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. Jaki komponent w React Native jest odpowiednikiem <div> z HTML?
2. Dlaczego ten kod jest błędny: <View>Hello World</View>?
Zadania praktyczne w grze
- Klikanie w kolejności
Kliknij elementy w odpowiedniej kolejności, aby stworzyć komponent View:
- Układanie w poziomie
Jaki jest odpowiednik <img> w React Native?