1. View and Components
1.1 View
-
์ ๊ทธ๋ฆผ์ ๊ฐ๊ฐ Android์ iOS์์ ํ๋ฉด์ ๊ทธ๋ฆด ๋, ์ฐ๋ ์ปดํฌ๋ํธ๋ค์ ๋ณด์ฌ์ค๋ค.
-
๋ฆฌ์คํธ๋ค ์ค์์ ํ๋์ ์์ดํ ์ ํํํ ๋,
- Android๋ ViewGroup์ผ๋ก ๊ฐ์ธ๊ณ , ๊ทธ ์์ชฝ ์์ด์ฝ์ ImageView, ๊ธ์๋ TextView ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉ
- iOS๋ UIView์ผ๋ก ๊ฐ์ธ๊ณ , ๊ทธ ์์ชฝ ์์ด์ฝ์ UIImageView, ๊ธ์๋ UITextView ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉ
-
๊ทธ๋ฌ๋ฉด React Native์์๋ ๊ฐ OS๋ณ ์ปดํฌ๋ํธ๋ค์ ์ด๋ค ๊ฑธ๋ก ๋์ฒดํด์ผ ํ ๊น?
- ๋ฐ๋ก Native Component์ด๋ค.
-
cf. https://reactnative.dev/docs/intro-react-native-components
1.2 Native Component
Native Components: ํ๋ซํผ(iOS ๋๋ Android)์ view๋ฅผ JS๋ก ํธ์ถํ ์ ์๊ฒ ํด์ฃผ๋ React ์ปดํฌ๋ํธ
- ์ฝ๋๋ฅผ ์์ฑํ ๋, JavaScript์ React Component
- Runtime์์ React Native๊ฐ ๊ตฌํํ ์ฝ๋์ ์์ํ๋ ์๋๋ก์ด๋์ iOS view๋ฅผ ์์ฑํจ
- ๋ค๋ฅธ ์ฌ๋์ด ๋ง๋ Native Component๋ฅผ ์ฌ์ฉํ ์๋ ์์: React Native Directory
1.3 ๋ํ์ ์ธ Core Components
| REACT NATIVE UI COMPONENT | ANDROID VIEW | IOS VIEW | WEB ANALOG | DESCRIPTION |
|---|---|---|---|---|
<View> | <ViewGroup> | <UIView> | A non-scrolling <div> | flexbox, ์คํ์ผ, ์ผ๋ถ ํฐ์น ์ฒ๋ฆฌ, ์ ๊ทผ์ฑ ์ปจํธ๋กค์ ์ง์ํ๋ ์ปจํ ์ด๋ |
<Text> | <TextView> | <UITextView> | <p> | ํ ์คํธ ๋ฌธ์์ด์ ํ์, ์คํ์ผ๋งํ๊ณ ์ค์ฒฉํ๋ฉฐ, ์ฌ์ง์ด ํฐ์น ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌ |
<Image> | <ImageView> | <UIImageView> | <img> | ๋ค์ํ ์ ํ์ ์ด๋ฏธ์ง๋ฅผ ํ์ |
<ScrollView> | <ScrollView> | <UIScrollView> | <div> | ์ฌ๋ฌ ์ปดํฌ๋ํธ์ ๋ทฐ๋ฅผ ํฌํจํ ์ ์๋ ์ผ๋ฐ ์คํฌ๋กค ์ปจํ ์ด๋ |
<TextInput> | <EditText> | <UITextField> | <input type="text"> | ์ฌ์ฉ์๊ฐ ํ ์คํธ๋ฅผ ์ ๋ ฅํ ์ ์๊ฒ ํจ |
Core Components: React Native๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ Native Components ์ธํธ- ๊ทธ ์ธ ๋ชจ๋ Core Component๋ ๊ณต์ ๋ฌธ์ Core Components and APIs ยท React Native๋ฅผ ํ์ธ
2. Core Components ์์
2.1 Text Input
<TextInput>: html์์์<input type="text">์ ๋์ผ- ์ ์ ์ ํ ์คํธ ์ ๋ ฅ์ ๋ฐ์ ์ ์๋ ์ปดํฌ๋ํธ
- cf. https://reactnative.dev/docs/next/handling-text-input
1import React, { useState } from 'react'2import { Text, TextInput, View } from 'react-native'34const PizzaTranslator = () => {5const [text, setText] = useState('')67return (8<View style={{ padding: 100 }}>9<TextInput10style={{ height: 40 }}11placeholder="Type here to translate!"12onChangeText={newText => setText(newText)}13defaultValue={text}14/>15<Text style={{ padding: 10, fontSize: 42 }}>16{text17.split(' ')18.map(word => word && '๐')19.join(' ')}20</Text>21</View>22)23}2425export default PizzaTranslator
2.2 Scroll View
View๋ 2๊ฐ์ง ์ข ๋ฅ๊ฐ ์๋ค.<View>: ์คํฌ๋กค์ด ๋ถ๊ฐ๋ฅํ<div><ScrollView>: ์คํฌ๋กค ๊ฐ๋ฅํ<div>
- ์คํฌ๋กค ๊ฐ๋ฅํ ์ปจํ
์ด๋๋ฅผ ๋ง๋๋ ค๋ฉด
<ScrollView>๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. - cf. https://reactnative.dev/docs/next/using-a-scrollview
1import React from 'react'2import { Image, ScrollView, Text } from 'react-native'34const logo = {5uri: 'https://reactnative.dev/img/tiny_logo.png',6width: 64,7height: 64,8}910const App = () => (11<ScrollView>12<Text style={{ fontSize: 96 }}>Scroll me plz</Text>13<Image source={logo} />14<Image source={logo} />15<Image source={logo} />16<Image source={logo} />17<Image source={logo} />18<Text style={{ fontSize: 96 }}>If you like</Text>19<Image source={logo} />20<Image source={logo} />21<Image source={logo} />22<Image source={logo} />23<Image source={logo} />24<Text style={{ fontSize: 96 }}>Scrolling down</Text>25<Image source={logo} />26<Image source={logo} />27<Image source={logo} />28<Image source={logo} />29<Image source={logo} />30<Text style={{ fontSize: 96 }}>What's the best</Text>31<Image source={logo} />32<Image source={logo} />33<Image source={logo} />34<Image source={logo} />35<Image source={logo} />36<Text style={{ fontSize: 96 }}>Framework around?</Text>37<Image source={logo} />38<Image source={logo} />39<Image source={logo} />40<Image source={logo} />41<Image source={logo} />42<Text style={{ fontSize: 80 }}>React Native</Text>43</ScrollView>44)4546export default App
iOS์ ๊ฒฝ์ฐ, ScrollView์์ ์ค์ด ๊ฐ๋ฅํ๊ฒ ํ ์ ์์
maximumZoomScale: ์ต๋ ์ค ์ค์ผ์ผ ์ค์ minimumZoomScale: ์ต์ ์ค ์ค์ผ์ผ ์ค์
1<ScrollView minimumZoomScale={1.0} maximumZoomScale={2.0}>2<!-- ~~~ -->3</ScrollView>
iOS ์๋ฎฌ๋ ์ดํฐ์์ pinch ์ค์ ํ๋ ค๋ฉด?
- optionํค๋ฅผ ๋๋ฅด๊ณ ๋๋๊ทธ
์คํฌ๋กค๋ทฐ๋ ํ๋ฉด์๋ ์๋ณด์ด๋ ์์ดํ ๋ ํ๋ฒ์ ๋๋๋ง ํด๋ฒ๋ฆผ โ ๋ง์ ์์ดํ ์ด ์๋ค๋ฉด ๋๋๋ง ์๊ฐ ๊ธธ์ด์ง
- ์์ดํ ์ด ๋ง๋ค๋ฉด **List View**๋ฅผ ์ฌ์ฉ.
2.3 List View
2.3.1 FlatList
- ํ๋ฉด์ ํ์ฌ ๋ณด์ด๋ ๋ถ๋ถ๋ง ๋จผ์ ๋๋๋ง ๋จ
- props ์ค๋ช
data: ์ถ๋ ฅํ๊ณ ์ ํ๋ ๋ชจ๋ ์์ดํ ๋ค์ ์ ๋ณด๋ฅผ array(list) ํํ๋ก ์ ๋ฌrenderItem: ๊ฐ ์์๋ค์ ์ด๋ค ํฌ๋งท์ผ๋ก ์ถ๋ ฅํ ๊ฒ์ธ์ง ๊ตฌํํ๋ ๊ณณkeyExtractor: ๊ฐ ์์ดํ ๋ง๋ค uniqueํ key ๊ฐ์ ์ค (React์์์ key์ ๋์ผํ ์ญํ )
- cf. https://reactnative.dev/docs/next/using-a-listview
1import React from 'react'2import { FlatList, StyleSheet, Text, View } from 'react-native'34const styles = StyleSheet.create({5container: {6flex: 1,7paddingTop: 22,8},9item: {10padding: 10,11fontSize: 18,12height: 44,13},14})1516const FlatListBasics = () => {17return (18<View style={styles.container}>19<FlatList20data={[21{ key: 'Devin' },22{ key: 'Dan' },23{ key: 'Dominic' },24{ key: 'Jackson' },25{ key: 'James' },26{ key: 'Joel' },27{ key: 'John' },28{ key: 'Jillian' },29{ key: 'Jimmy' },30{ key: 'Julie' },31]}32renderItem={({ item }) => <Text style={styles.item}>{item.key}</Text>}33/>34</View>35)36}3738export default FlatListBasics
2.3.2 SectionList
- FlatList๋ค์ ๊ตฌ๋ถํ ์ ์๋ ์ธ์
ํค๋๊ฐ ์กด์ฌ (iOS์
UITableView์ ๋น์ทํจ) - props ์ค๋ช
sections: ์ถ๋ ฅํ๊ณ ์ ํ๋ ๋ชจ๋ Section์ ์ ๋ณด๋ฅผ array(list) ํํ๋ก ์ ๋ฌ- (๊ฐ ์ธ์ ์ FlatList์ด๊ธฐ ๋๋ฌธ์ FlatList์ props์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ํด์ผ ํจ)
renderItem: ๊ฐ ์์ดํ ๋ค์ ์ด๋ค ํฌ๋งท์ผ๋ก ์ถ๋ ฅํ ๊ฒ์ธ์ง ๊ตฌํํ๋ ๊ณณrenderSectionHeader: ์ธ์ ์ ๊ตฌ๋ถํ๋ ํค๋๋ฅผ ๊ตฌํํ๋ ๊ณณkeyExtractor: ๊ฐ ์์ดํ ๋ง๋ค uniqueํ key ๊ฐ์ ์ค (React์์์ key์ ๋์ผํ ์ญํ )
- cf. https://reactnative.dev/docs/sectionlist
1import React from 'react'2import { StyleSheet, Text, View, SafeAreaView, SectionList, StatusBar } from 'react-native'34const DATA = [5{6title: 'Main dishes',7data: ['Pizza', 'Burger', 'Risotto'],8},9{10title: 'Sides',11data: ['French Fries', 'Onion Rings', 'Fried Shrimps'],12},13{14title: 'Drinks',15data: ['Water', 'Coke', 'Beer'],16},17{18title: 'Desserts',19data: ['Cheese Cake', 'Ice Cream'],20},21]2223const SectionListBasics = () => (24<SafeAreaView style={styles.container}>25<SectionList26sections={DATA}27keyExtractor={(item, index) => item + index}28renderItem={({ item }) => (29<View style={styles.item}>30<Text style={styles.title}>{item}</Text>31</View>32)}33renderSectionHeader={({ section: { title } }) => <Text style={styles.header}>{title}</Text>}34/>35</SafeAreaView>36)3738const styles = StyleSheet.create({39container: {40flex: 1,41paddingTop: StatusBar.currentHeight,42marginHorizontal: 16,43},44item: {45backgroundColor: '#f9c2ff',46padding: 20,47marginVertical: 8,48},49header: {50fontSize: 32,51backgroundColor: '#fff',52},53title: {54fontSize: 24,55},56})5758export default SectionListBasics