React Native (Expo) Cheatsheet
Creating a Project
npx create-expo-app@latest MyApp
cd MyApp
npx expo start
Core Components
import { View, Text, TextInput, TouchableOpacity, FlatList } from 'react-native';
<View> // Container (like <div>)
<Text> // Text display
<TextInput /> // Text input field
<TouchableOpacity> // Pressable button
<FlatList /> // Scrollable list
<ActivityIndicator> // Loading spinner
<ScrollView> // Scrollable container
Styling
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
padding: 16,
},
title: {
fontSize: 20,
fontWeight: 'bold',
color: '#333',
},
});
<View style={styles.container}>
<Text style={styles.title}>Hello</Text>
</View>
State with useState
import { useState } from 'react';
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
FlatList (for chat messages)
<FlatList
data={messages}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<View style={item.sender === 'user' ? userBubble : aiBubble}>
<Text>{item.content}</Text>
</View>
)}
ref={flatListRef}
onContentSizeChange={() => flatListRef.current?.scrollToEnd()}
/>
TextInput + Send Button
<View style={inputRow}>
<TextInput
style={inputField}
value={input}
onChangeText={setInput}
placeholder="Type a message..."
/>
<TouchableOpacity style={sendButton} onPress={handleSend}>
<Text style={sendText}>Send</Text>
</TouchableOpacity>
</View>
Fetch API
const API_BASE_URL = 'http://192.168.1.100:5000';
const sendMessage = async (text: string) => {
const response = await fetch(`${API_BASE_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: text }),
});
const data = await response.json();
return data;
};
Keyboard Handling
import { KeyboardAvoidingView, Platform } from 'react-native';
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={{ flex: 1 }}
>
{/* your content */}
</KeyboardAvoidingView>
| Command |
Runs on |
npx expo start |
Scan QR with Expo Go |
Press a |
Android emulator |
Press i |
iOS simulator |
Press w |
Web browser |