๐ React ์ ๋ฌธ 05 - Component
๐ React ์
๋ฌธ 05 - Component
๐
ใ์ํ์ ์ฒ์ ๋ง๋ ๋ฆฌ์กํธใ๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
Component๋?
Component๋props๋ฅผ ํตํด ์์ฑ์ ์ ๋ ฅํ์ฌReact Element๋ฅผ ์ถ๋ ฅํ๋Javascriptํจ์๋ก, ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ฑํ๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋ถํ์ด๋ผ๊ณ ํ ์ ์๋ค.- ํ๋์
Component๋ ์ฌ๋ฌ ๊ฐ์Component๋ก ๊ตฌ์ฑํ ์ ์๋ค. Component๋ก ๋ถ๋ฆฌํจ์ผ๋ก์จ ์ฝ๋์ ๊ฐ๋ ์ฑ์ ํ๋ณดํ ์ ์๊ณ , ์ฝ๋ ์์ ์ค์์ผ๋ก์จ ๊ฐ๋ฐ ๋ฐ ์ ์ง ๋ณด์ ์๊ฐ์ ์ ๊ฐํ ์ ์๋ค.
props๋?
Component๋ ํ๋์ ๊ฐ์ฒด์ด๋ฉฐ,props๋ ๊ฐ์ฒด์ ์์ฑ์ ํด๋นํ๋ค.- ๊ฐ๋ น ์์ฑ๋ง ๋ค๋ฅธ
React Element๋ฅผ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด ๋์ผComponent๋ฅผ ์ฌ์ฌ์ฉํ์ฌprops๋ง ๋ค๋ฅด๊ฒ ์ฃผ์ ํ๋ฉด ๋๋ค. props๋ ์ฝ๊ธฐ ์ ์ฉ์ด์ด์ ๊ฐ์ ์์ ํ ์ ์๋ค.React Element๋ฅผ ์์ฑํ๋ ๋์ค์props๊ฐ ๋ฐ๋์ด ๋ฒ๋ฆฌ๋ฉดReact๊ฐ ์ ๋๋ก ๋์ํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ์ฝ๊ธฐ ์ ์ฉ์ด๋ผ๋ ํน์ง์ ๊ฐ๋๋ค.- ๋์ผ
props์ ๋ํด์๋ ํญ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ์ด์ผ ํ๋ค. - ๋ค๋ฅธ
props๋ฅผ ๊ฐ์งReact Element๋ฅผ ๋ ๋๋ง ํ๊ณ ์ถ์ผ๋ฉด ์๋ก์ด ๊ฐ์Component์ ์ ๋ฌํ๋ฉด ๋๋๋ฐ, ์ด ๊ณผ์ ์์React Element๊ฐ ๋ ๋๋ง ๋๋ค.
1
2
3
4
5
6
7
8
9
function App(props) {
return (
<Profile
name="brobro332"
introduction="์๋
ํ์ธ์!"
viewCount={20}
/>
);
}
ProfileComponent์name,introduction,viewCount์์ฑ์ ์ ๋ฌํ์ฌReact Element๋ฅผ ์์ฑํ๋ ์์ ์ด๋ค.- ๋ฌธ์์ด ์ด์ธ์ ์ ์, ๋ณ์,
Component๋ฑ์ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ค๊ดํธ๋ก ๊ฐ์ธ์ผ ํ๋ค. - ๋ฌธ์์ด๋ ์ค๊ดํธ๋ก ๊ฐ์ธ๋ ์๊ด์๋ค.
- ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ๋ฉด
Javascript์ฝ๋๋ฅผ ์ฌ์ฉํ๋ค๊ณ ์ ์ธํ๋ ๊ฒ๊ณผ ๊ฐ๋ค.
Component ์ข
๋ฅ
โ
Class Component
1
2
3
4
5
class Welcome extends React.Component {
render() {
return <h1>์๋
, {this.name}</h1>;
}
}
Class Component๋JavascriptES6์Class๋ฅผ ์ฌ์ฉํด์ ๋ง๋ค์ด์ง ํํ์ด๋ค.React์ ๋ชจ๋Class Component๋React.Component๋ฅผ ์์ ๋ฐ์์ ๋ง๋ ๋ค.
โ
Functional Component
- ์ด๊ธฐ ๋ฒ์ ์๋
Class Component๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ์์ผ๋ ๋ถํธํ๋ค๋ ์๊ฒฌ์ด ๋ค์์๋ค. - ๊ฐ์ ํ๋ ๊ณผ์ ์์
Hook์ด๋ผ๋ ๊ฒ์ด ๊ฐ๋ฐ๋์๋๋ฐReact์ ์ฃผ๋ ฅ ๊ธฐ๋ฅ์ด๊ธฐ ๋๋ฌธ์ ์์ผ๋ก๋ ์ฃผ๋กFunctional Component๋ฅผ ์ฌ์ฉํ๊ฒ ๋ ๊ฒ์ด๋ค.
Component ํน์ง
โ
Component ์ด๋ฆ์ ํญ์ ๋๋ฌธ์๋ก ์์ํด์ผ ํ๋ค.
React๋ ์๋ฌธ์๋ก ์์ํ๋Component๋ฅผDOMํ๊ทธ๋ก ์ธ์ํ๊ธฐ ๋๋ฌธ์ด๋ค.- ์๋ฅผ ๋ค์ด
<div>๋ ๋ด์ฅComponent์ด๋ฉฐ"div"๋ฌธ์์ด๋กReact.createElement()์ ์ ๋ฌ๋๋ค. <Foo />์ ๊ฐ์ด ๋๋ฌธ์๋ก ์์ํ๋ ๊ฒฝ์ฐ๋"Foo"๋ฌธ์์ด๋กReact.createElement()์ ์ ๋ฌ๋๋ ๊ฑด ๋์ผํ์ง๋ง ๋ด์ฅComponent๊ฐ ์๋Javascriptํ์ผ ๋ด์์ ์ฌ์ฉ์๊ฐ ์ ์ํ๊ฑฐ๋importํComponent๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
โ
Component ๋ ๋๋ง
Element๋ ๋๋ง๊ณผ ๋์ผํ๊ฒroot.render()์ ์ธ์๋กComponent๋ฅผ ์ ๋ฌํ๋ฉด ๋๋ค.
โ
Component ํฉ์ฑ
- ์ฌ๋ฌ ๊ฐ์
Component๋ฅผ ํฉ์ณ์ ํ๋์Component๋ฅผ ๋ง๋๋ ๊ฒ์ ์๋ฏธํ๋ค.
โ
Component ์ถ์ถ
- ํฐ
Component์์ ์ผ๋ถ๋ฅผ ์ถ์ถํด์ ์๋ก์ดComponent๋ฅผ ๋ง๋๋ ๊ฒ์ด๋ค. - ๊ธฐ๋ฅ ๋จ์๋ก ๊ตฌ๋ถํ๊ณ ๋์ค์ ๊ณง๋ฐ๋ก ์ฌ์ฌ์ฉํ ์ ์๋ ํํ๋ก ์ถ์ถํ๋ ๊ฒ์ด ์ข๋ค.
Component๋ด์ ๋ฐ๋ณต๋๊ฑฐ๋ ๊ธฐ๋ฅ์ผ๋ก ๊ตฌ๋ถ๋๋ ์ฝ๋๋ฅผ ํจ์๋ก ์ถ์ํํ๋ ๊ฒ์ด๋ค.
์ค์ต
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const style = {
wrapper: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
gap: 20,
padding: 20,
backgroundColor: '#f4f4f9',
minHeight: '100vh'
},
card: {
width: 600,
padding: '20px 30px',
borderRadius: 20,
boxShadow: '0 10px 20px rgba(0, 0, 0, 0.1)',
backgroundColor: '#ffffff',
transition: 'all 0.3s ease',
cursor: 'default'
},
title: {
margin: 0,
fontSize: 20,
fontWeight: 'bold',
color: '#2c3e50'
},
description: {
marginTop: 8,
fontSize: 16,
color: '#555'
}
};
function Project(props) {
return (
<div style={style.card}>
<h3 style={style.title}>โ
{props.name}</h3>
<h4 style={style.description}>{props.description}</h4>
</div>
);
}
const projects = [
{
name: "java-co-working project",
description: "ํ์
์๋ฐ ํ๋ก์ ํธ"
},
{
name: "java-schedule-project",
description: "์ค์ผ์ฅด ์๋ฐ ํ๋ก์ ํธ"
},
{
name: "python-ai-project",
description: "AI ํ์ด์ฌ ํ๋ก์ ํธ"
}
];
function ProjectList() {
return (
<div style={style.wrapper}>
{
projects.map((project) => (
<Project
key={project.name}
name={project.name}
description={project.description}
/>
))
}
</div>
);
}
export default ProjectList;
ProjectListComponent๋ProjectComponent๋ฅผ ์์Component๋ก ํฌํจํ๊ณ ์๋ค.
ํ๊ณ
React Component๋ ๋์ผํprops๋ฅผ ๋ฐ์ผ๋ฉด ํญ์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํด์ผ ํ๋ฉฐ, ์ด๋ฌํ ํน์ฑ์pureํ๋ค๊ณ ํํํ๋ค.- ์ด๋
React์Element๊ฐ ๋ถ๋ณ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ ๊ฒ์ด๋ฉฐ, ๋ฐ๋ผ์Component์props๋ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ทจ๊ธ๋๋ค๋ ์ฌ์ค์ ์ดํด ํ ์ ์๋ค.
This post is licensed under CC BY 4.0 by the author.

