# React Native ์ค์ ๊ฐ์ด๋
## ํ์ต ๋ชฉํ
JavaScript๋ก iOS/Android ๋ค์ดํฐ๋ธ ์ฑ์ ๊ฐ๋ฐํฉ๋๋ค.
## 1. React Native๋?
Facebook์ด ๋ง๋ ํฌ๋ก์คํ๋ซํผ ๋ชจ๋ฐ์ผ ํ๋ ์์ํฌ์
๋๋ค.
- JavaScript๋ก ๋ค์ดํฐ๋ธ ์ฑ ๊ฐ๋ฐ
- iOS์ Android ๋์ ๊ฐ๋ฐ
- React ๋ฌธ๋ฒ ๊ทธ๋๋ก ์ฌ์ฉ
- ํซ ๋ฆฌ๋ก๋ฉ ์ง์
## 2. ํ๋ก์ ํธ ์์ฑ
npx create-expo-app myapp
cd myapp
npx expo start
# ๋๋ React Native CLI
npx react-native init MyApp
cd MyApp
npx react-native run-android
npx react-native run-ios
## 3. ๊ธฐ๋ณธ ์ปดํฌ๋ํธ
import { View, Text, Image, ScrollView, TouchableOpacity } from "react-native";
function App() {
return (
<View style={{flex: 1, padding: 20}}>
<Text style={{fontSize: 24}}>Hello</Text>
<Image source={{uri: "https://..."}} style={{width: 100, height: 100}} />
<TouchableOpacity onPress={() => alert("Clicked")}>
<Text>Press Me</Text>
</TouchableOpacity>
</View>
);
}
## 4. ์คํ์ผ๋ง
import { StyleSheet } from "react-native";
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
padding: 20,
},
title: {
fontSize: 24,
fontWeight: "bold",
color: "#333",
},
button: {
backgroundColor: "#007AFF",
padding: 15,
borderRadius: 10,
alignItems: "center",
},
});
## 5. ์ํ ๊ด๋ฆฌ
import { useState, useEffect } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<View>
<Text>Count: {count}</Text>
<Button title="Add" onPress={() => setCount(count + 1)} />
</View>
);
}
## 6. ๋ค๋น๊ฒ์ด์
npm install @react-navigation/native @react-navigation/stack
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Detail" component={DetailScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
// ์ด๋
navigation.navigate("Detail", { id: 123 });
## 7. API ํต์
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch("https://api.example.com/items")
.then(res => res.json())
.then(json => {
setData(json);
setLoading(false);
});
}, []);
## 8. FlatList
import { FlatList } from "react-native";
<FlatList
data={items}
keyExtractor={item => item.id.toString()}
renderItem={({ item }) => (
<View style={styles.item}>
<Text>{item.title}</Text>
</View>
)}
onEndReached={loadMore}
refreshing={refreshing}
onRefresh={handleRefresh}
/>
## 9. ๋ก์ปฌ ์ ์ฅ
import AsyncStorage from "@react-native-async-storage/async-storage";
// ์ ์ฅ
await AsyncStorage.setItem("token", "abc123");
// ์ฝ๊ธฐ
const token = await AsyncStorage.getItem("token");
// ์ญ์
await AsyncStorage.removeItem("token");
## 10. ์นด๋ฉ๋ผ/์ด๋ฏธ์ง
import * as ImagePicker from "expo-image-picker";
const pickImage = async () => {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
quality: 1,
});
setImage(result.assets[0].uri);
}
};
## 11. ํธ์ ์๋ฆผ
import * as Notifications from "expo-notifications";
// ๊ถํ ์์ฒญ
const { status } = await Notifications.requestPermissionsAsync();
// ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
const token = await Notifications.getExpoPushTokenAsync();
// ๋ก์ปฌ ์๋ฆผ
await Notifications.scheduleNotificationAsync({
content: { title: "์๋ฆผ", body: "๋ฉ์์ง" },
trigger: { seconds: 5 },
});
## 12. ๋น๋ ๋ฐ ๋ฐฐํฌ
# Expo
eas build --platform android
eas build --platform ios
eas submit --platform android
eas submit --platform ios
# React Native CLI
cd android && ./gradlew assembleRelease
xcodebuild -workspace ios/App.xcworkspace -scheme App
## ์ถ์ฒ ํ์ต ์์
1. React ๊ธฐ์ด ๋ณต์ต
2. ๊ธฐ๋ณธ ์ปดํฌ๋ํธ ์ตํ๊ธฐ
3. ๋ค๋น๊ฒ์ด์
๊ตฌํ
4. API ์ฐ๋
5. ๋น๋ ๋ฐ ๋ฐฐํฌ