π React μ λ¬Έ β € - Component
π React μ
λ¬Έ β
€ - 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}
/>
);
}
Profile
Component
μ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
λJavascript
ES6
μ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;
ProjectList
Component
λProject
Component
λ₯Ό μμComponent
λ‘ ν¬ν¨νκ³ μλ€.
νκ³
React Component
λ λμΌνprops
λ₯Ό λ°μΌλ©΄ νμ λμΌν κ²°κ³Όλ₯Ό λ°νν΄μΌ νλ©°, μ΄λ¬ν νΉμ±μpure
νλ€κ³ νννλ€.- μ΄λ
React
μElement
κ° λΆλ³ κ°μ²΄μ΄κΈ° λλ¬Έμ κ°λ₯ν κ²μ΄λ©°, λ°λΌμComponent
μprops
λ μ½κΈ° μ μ©μΌλ‘ μ·¨κΈλλ€λ μ¬μ€μ μ΄ν΄ ν μ μλ€.
This post is licensed under CC BY 4.0 by the author.