1.์ปดํฌ๋ํธ
: ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฐ๋ณ์ ์ธ ์ฌ๋ฌ ์กฐ๊ฐ
UI(User Interface)๋ ์ฌ๋ฌ ๊ฐ์ Component๋ก ๋๋๋ค.
์นดํก ํ๋ฉด์ ์์ ๊ฐ์ด 5๊ฐ์ ์ปดํฌ๋ํธ๋ก ๋๋๋ ๊ฒ์ด๋ค.
์ปดํฌ๋ํธ๋ java๋ก ์น๋ฉด ๋งค์๋ ๊ฐ์ ๋ ์์ด๋ค.
2. ์ปดํฌ๋ํธ์ ์ข ๋ฅ
(1) ํด๋์คํ ์ปดํฌ๋ํธ
a. ์ฐ๊ธฐ ์ํ ์กฐ๊ฑด
- React.Component๋ผ๋ ํด๋์ค๋ฅผ ๋ฌด์กฐ๊ฑด ์์ํด์ผ ํจ.
- render() ๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๊ณ , ์ ํจ์์ return ๊ฐ์๋ค๊ฐ ํ๊ณ ์ถ์ ๋ด์ฉ๋ฌผ์ ์ ์ด์ผํจ.
b. state, lifestyle(์๋ช ์ฃผ๊ธฐ)๋ผ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์.
c. ํจ์ ํ ์ปดํฌ๋ํธ๋ณด๋ค ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ ์ฌ์ฉํ๋ค๋ ๋จ์ .
(2) ํจ์ํ ์ปดํฌ๋ํธ
a. state, lifestyle์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ง ๋ชปํจ. ๋ฐ๋ผ์ Hook์ด๋ผ๋ ๊ธฐ๋ฅ์ ๋์ ์ฌ์ฉํด์ผํจ.
b. class๋ณด๋จ ๊ฐ๊ฒฐํจ. ๋ฐ๋ผ์ ๊ฐ๋ ์ฑ์ด ์ข์
c. class ์ปดํฌ๋ํธ๋ณด๋ค ๋ฉ๋ชจ๋ฆฌ ์ฉ๋์ด ์์
d. React ๊ณต์ ๋ฌธ์์์๋ ํจ์ํ ์ปดํฌ๋ํธ + Hook์ ์ฌ์ฉํ๋ผ๊ณ ๊ถ์ฅํจ.
3. Props๋ฅผ ์ด์ฉํด ์์ ์ปดํฌ๋ํธ์์ ํ์ ์ปดํฌ๋ํธ๋ก ๊ฐ ์ฃผ๊ณ ๋ฐ๊ธฐ
import { StatusBar } from 'expo-status-bar';
import { Button, Switch } from 'react-native';
import { Image, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native';
// props์๋ title = "์น๊ตฌ"๋ผ๋ ๊ฐ์ด props์์ ์ธ์คํด์ค ๋ณ์๋ ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ฒ๋ผ ๋ค์ด๊ฐ๋ค.
const Header = (props) => {
console.log("props of Header", props)
// props์์ ํน์ ๋ณ์๋ฅผ ๊บผ๋ด ์ฐ๋ ๋ฒ.
return <Text>{props.title }</Text>
};
const MyProfile = (props) => {
return <Text> MyProfile </Text>
};
const Division = (props) => {
return <Text> Division </Text>
};
const FriendSection = (props) => {
return <Text> FriendSection </Text>
};
const Profile = (props) => {
return <Text>{props.name}</Text>
};
const FriendList = () => {
return (
<View>
<Profile name="์ด์๋ก"/>
<Profile name="์ด์งํ"/>
<Profile name="์ ์์ด"/>
<Profile name="์ ์ฌ์ฑ"/>
<Profile name="ํ๊ท๋ฒ"/>
</View>
);
};
export default function App() {
return (
<View style={styles.container}>
<Header title="์น๊ตฌ"/>
<MyProfile/>
<Division/>
<FriendSection/>
<FriendList/>
</View>
);
}
์์ ๋ชจ๋ฅด๊ฒ ์ง๋ง, App()์ด๋ ๋ฉ์ธ ํจ์ํ ์ปดํฌ๋ํธ์ ์๋ ํ๊ทธ๋ค์ด ์์ ์ปดํฌ๋ํธ ์ด๊ณ ,
ํด๋น ์ด๋ฆ๊ณผ ๋๊ฐ์ ์ด๋ฆ์ JS ๋ณ์๋ค์ด ํ์ ์ปดํฌ๋ํธ ์ธ ๊ฑฐ ๊ฐ๋ค. ๋์๊ฐ๋ ์๋ฆฌ๋ฅผ ๋ณด๋ฉด.
๋ณ์๋ค์ ์ต๋ช ํจ์๋ฅผ ๋ฐ์ดํฐ๋ก ๊ฐ์ง๊ณ ์๊ณ , ์ด ์ต๋ช ํจ์์๋ props๋ผ๋ ๊ฐ์ฒด ์ธ์๋ฅผ ๋ฐ์ ์ธ ์ ์๋ค.
props๋ ์์ ์ปดํฌ๋ํธ์์ ๋ฐ์ ๊ฐ๋ค์ ํ์ ์ปดํฌ๋ํธ๋ก ์ ๋ฌํด์ฃผ๋ ๊ฐ์ฒด์ด๋ค.
์ฌ๊ธฐ์ ์์ ์ปดํฌ๋ํธ๋ App() ์ด๊ณ , ํ์ ์ปดํฌ๋ํธ๋ myProfile์ ๋น๋กฏํ ๋๋จธ์ง ์ปดํฌ๋ํธ๋ค์ด๋ค.
** ์ฐธ๊ณ **
App()์ ์ปดํฌ๋ํธ๋ฅผ ํจ์๋ฅผ ์ ์์ผ๋ก ํํํ๋ ๊ตฌ๋ฌธ์ผ๋ก ๋ํ๋๊ณ ,
๋๋จธ์ง๋ ๋ณ์์ ์ต๋ช ํจ์ ๋ฐ์ดํฐ๋ฅผ ๋์ ํ๋ ํ์์ผ๋ก ๋ํ๋๋ค.
props ๋ผ๋ ๊ฐ์ฒด์๋ ์์ ์ปดํฌ๋ํธ์ ์์ฑ์ผ๋ก ์ฐ์ธ ๊ฐ๋ค์ด ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ ํ์์ผ๋ก ๋ค์ด๊ฐ.
name = "์ด์๋ก" ๋ผ๋ ์์ฑ์ด ์์ผ๋ฉด,
//์ฒซ ๋ฒ์งธ Myprofile ์ปดํฌ๋ํธ
props{
name = "์ด์๋ก"
}
์๋ฐ ์์ผ๋ก ๋ค์ด๊ฐ.
props์์ name์ด๋ ๋ ์์ Value๋ฅผ ๊บผ๋ผ๋ ค๋ฉด props.name์ผ๋ก ๊บผ๋ด์ผํจ. C++ ์ ๊ตฌ์กฐ์ฒด๋ Java ์ ๊ฐ์ฒด ์ฒ๋ผ ์์ชฝ์ด ๋์ด์๋๋ณด๋ค.
**์ฃผ์์ **
์ด๋ค ๋ณ์์ ๊ฐ์ ๊บผ๋ด ์ธ ๋๋ {} ํ ํ๋ฆฟ ์ด์ฉํด์ค์ผ ํ๋ค. {๋ณ์์ด๋ฆ} == ๊ฐ ์ด๋ค.
4. ์ค์ค๋ก ํด๋ณด๊ธฐ
import { StatusBar } from 'expo-status-bar';
import { Button, Switch } from 'react-native';
import { Image, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native';
export default function App() {
return(
<View style={styles.container}>
<FriendList/>
<Footer/>
</View>
)
}
const FriendList = () => {
return(
<View>
<Profile name = "์ด๋ฏผํ"/>
<Profile name = "์ด์๋ก"/>
<Profile name = "์ด์งํ"/>
<Profile name = "์ ์ฌ์ฑ"/>
</View>
)
}
const Profile =(props) => {
return (<Text>{props.name}</Text>)
}
const Footer =() => {
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
์ปดํฌ๋ํธ๋ ๋ฌด์กฐ๊ฑด ๋๋ฌธ์์ฌ์ผ ํ๋ค.
์ ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง view ์์๋ HTML ํ๊ทธ ์ฐ๋ฏ์ด <> </> ์ด๊ฑธ ์จ์ ์ด๋ค ๊ธฐ๋ฅ์ ๋ฐ๋ก ๊ตฌํํ์ง ๋ชปํ๋ค.
๋ฌด์กฐ๊ฑด </>ํ๊ทธ๋ก ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด์ ๊ทธ ์ปดํฌ๋ํธ์ ํ์ ์ปดํฌ๋ํธ์์ ์ฐ์ฐ์ด๋ ์ถ๋ ฅ์ ๋ง๋ค์ด์ ํ๋ฉด์ ๋์์ผ ํ๋ค. (ํ ๋จ๊ณ ๋ ๊ฑธ๋ฆฐ๋ค.)