์๋ ํ์ธ์ Foma ์ ๋๋ค!
์ค๋์ React Native์์ Component๋ฅผ ๋ง๋ค์ด List์ ์ ์ฉํด ๋ณด๋ ๊ฒ์ ์ ๋ฆฌํด ๋ณด๋ ค๊ณ ํฉ๋๋ค.
(iOS๋ก ๋งํ๋ฉด TableView๋ฅผ ๋ง๋ค์ด Cell์ ์ ์ฉํ๋ ๊ฒ๊ณผ ๋น์ทํ๋๋ผ๊ตฌ์.)
๋ฐ๋ก ์์ํ ๊ฒ์~
์ธ์ด๋ TypeScript, ํ๊ฒฝ์ Expo๋ก ์งํ ํ๊ฒ ์ต๋๋ค!
Model
Person.ts
๋ฐ์ดํฐ๋ฅผ ๋์ธ ๋ชจ๋ธ์ ๋ง๋ค์ด ์ค๋๋ค.
export interface Person {
name: string;
age: number;
gender: string;
}
Component
List์ ๋์ธ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด ์ค๋๋ค.
์ปดํฌ๋ํธ ์ด๋ฆ์ PersonCell๋ก ๋ง๋ค์ด ์ฃผ๊ฒ ์ต๋๋ค. (iOS ์คํ์ผ๋ก...ใ )
ํด๋น ์ปดํฌ๋ํธ์ ์์ดํ ์ Person์ผ๋ก ์ง์ ํด ์ฃผ๊ณ ๊ฐ ํ๋กํผํฐ๋ฅผ Text๋ก ๋์์ค๋๋ค.
PersonCell.tsx
import { View, Text, StyleSheet } from "react-native";
import { Person } from "./Person";
export default function PersonCell({ item }: { item: Person }) {
return (
<View style={styles.container}>
<Text style={styles.name}>{item.name}</Text>
<Text style={styles.age}>{item.age} years old</Text>
<Text style={styles.gender}>
{item.gender} {item.gender == "Male" ? "โ" : "โ"}
</Text>
</View>
);
}
PersonCell Stylesheet
๊ฐ ํ๊ทธ์ ๋ง๊ฒ ์คํ์ผ๋ ๋ง๋ค์ด ์ค๋๋ค.
const styles = StyleSheet.create({
container: {
borderRadius: 10,
borderColor: "white",
borderWidth: 1,
padding: 10,
marginBottom: 10,
color: "white",
backgroundColor: "rgba(50,50,50,1)",
},
name: {
color: "white",
fontSize: 15,
fontWeight: "bold",
marginBottom: 5,
},
gender: {
color: "white",
fontSize: 15,
},
age: {
color: "white",
fontSize: 15,
},
});
Data
Person ๋ชจ๋ธ์ ํ์์ ๋ง๋ Json ๋ชจ๋ธ์ ๋ง๋ค์ด ์ค๋๋ค.
personData.json
[
{
"name": "Kalid",
"age": 15,
"gender": "Male"
},
{
"name": "Fomagran",
"age": 20,
"gender": "Male"
},
{
"name": "May",
"age": 19,
"gender": "Female"
},
{
"name": "Julia",
"age": 29,
"gender": "Female"
},
{
"name": "Hunter",
"age": 18,
"gender": "Male"
},
{
"name": "Jane",
"age": 28,
"gender": "Female"
},
{
"name": "Kendrick",
"age": 34,
"gender": "Male"
},
{
"name": "Kali",
"age": 25,
"gender": "Female"
},
{
"name": "Jay",
"age": 37,
"gender": "Male"
},
{
"name": "Mac",
"age": 31,
"gender": "Male"
}
]
List
App.tsx
List๋ฅผ ๋์ฐ๊ธฐ ์ํด FlatList๋ฅผ ์ฌ์ฉํฉ๋๋ค.
FlatList๋ ScrollView์ ๊ฑฐ์ ๋์ผํฉ๋๋ค.
๋ค๋ง FlatList๋ ํ๋ฉด์ ์๋ณด์ฌ์ง๋ ๋ฐ์ดํฐ๋ฅผ ๋ ๋๋ง ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๊ฐ ๊ฐ๋ณ์ ์ด๊ณ ๋ง์ ๋ ์ฌ์ฉํ๊ธฐ์ ์ข์ต๋๋ค.
data์๋ personData๋ฅผ Person ํ์ผ๋ก ๋ฐ๊ฟ์ค์ ๋ฃ์ด์ค๋๋ค.
renderItem = PersonCell์ ์ ์ฉํด์ฃผ๊ณ
keyExtractor ์ด๋ ํ๋กํผํฐ ๊ธฐ์ค์ผ๋ก ๋ง๋ค๊ฑด์ง๋ฅผ ์ ํด ์ค๋๋ค.
import { StyleSheet, Text, View, FlatList } from "react-native";
import { Person } from "./Person";
import PersonCell from "./PersonCell";
import personData from "./personData.json";
export default function App() {
return (
<View style={styles.container}>
<Text style={styles.header}>Person List</Text>
<FlatList
data={personData as Person[]}
renderItem={PersonCell}
keyExtractor={(item) => item.name}
/>
</View>
);
}
App StyleSheet
const styles = StyleSheet.create({
container: {
padding: 20,
flex: 1,
backgroundColor: "rgba(50,50,50,1)",
},
header: {
fontSize: 20,
marginBottom: 20,
fontWeight: "bold",
color: "white",
},
});
์คํ ํ๋ฉด
์๋์ ๊ฐ์ด List ์์ ๋ฐ์ดํฐ์ ๋ด์ฉ์ ๋ง๋ ์ปดํฌ๋ํธ๊ฐ ๋์์ง๊ฒ ๋ฉ๋๋ค.
Pressable
๋ง์ฝ ๋ฆฌ์คํธ์ ์๋ ์ปดํฌ๋ํธ๋ฅผ ํด๋ฆญ์ด ๊ฐ๋ฅํ๋๋ก ๋ง๋ค๊ณ ์ถ๋ค๋ฉด Pressable์ด๋ผ๋ react-native ์์ฒด์์ ์ ๊ณตํ๋ component๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
App.tsx
Pressable์ import ํด์ค๋๋ค.
import { Pressable } from "react-native";
ํด๋ฆญ์ด ๊ฐ๋ฅํ๋๋ก Component๋ฅผ <Pressable> ํ๊ทธ๋ก ๊ฐ์ธ์ค๋๋ค.
ํด๋ฆญ ํ์ ๋ ๊ฐ๋จํ๊ฒ ์๋ฆผ์ฐฝ์ ๋์ฐ๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค.
const PressableItem = ({ item }: { item: Person }) => {
return (
<Pressable onPress={() => alert(`Pressed: ${item.name}`)}>
<PersonCell item={item} />
</Pressable>
);
};
renderItem์ ์์์ ๊ฐ์ธ์ค PressableItem์ผ๋ก ์ ์ฉํด ์ค๋๋ค.
export default function App() {
return (
<View style={styles.container}>
<Text style={styles.header}>Person List</Text>
<FlatList
data={personData as Person[]}
renderItem={PressableItem}
keyExtractor={(item) => item.name}
/>
</View>
);
}
๋ฆฌ์คํธ์ ์ปดํฌ๋ํธ๋ฅผ ํด๋ฆญํ๋ฉด ์๋์ ๊ฐ์ด ์๋ฆผ์ฐฝ์ด ๋จ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
๋๊ธ