๐ React ์ ๋ฌธ โ ฆ - React Component์ ์๋ช ์ฃผ๊ธฐ
๐ React ์
๋ฌธ โ
ฆ - React Component์ ์๋ช
์ฃผ๊ธฐ
๐
ใ์ํ์ ์ฒ์ ๋ง๋ ๋ฆฌ์กํธใ
๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
React Component
์ ์๋ช
์ฃผ๊ธฐ๋?
React Component
๊ฐ ์์ฑ๋๊ณ , ๋ ๋๋ง ๋๊ณ , ํ๋ฉด์์ ์ฌ๋ผ์ง๋ ์ผ๋ จ์ ๊ณผ์ ์ ๋งํ๋ค.
React Component
์ ์๋ช
์ฃผ๊ธฐ ์ข
๋ฅ
โ
Mount
Component
์ ์์ฑ์๊ฐ ์คํ๋๋ ๊ณผ์ ์ด๋ค.- ์์ฑ์์์๋
State
๋ฅผ ์ ์ํ๊ณComponent
๊ฐ ๋ ๋๋ง ๋๋ค. Class Component
์์๋componentDidMount()
ํจ์๊ฐ ํธ์ถ๋๋ค.
โ
Update
Component
์props
๊ฐ ๋ณ๊ฒฝ๋๊ฑฐ๋State
๊ฐ ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ, ๋๋forceUpdate()
ํจ์๋ฅผ ํตํด ๊ฐ์ ๋กUpdate
๋ ๋Component
๊ฐ ๋ค์ ๋ ๋๋ง ๋๋ค.- ์ด๋
Class Component
์์๋componentDidUpdate()
ํจ์๊ฐ ํธ์ถ๋๋ค.
โ
Unmount
- ํ์ฌ
Component
๋ฅผ ๋ ์ด์ ํ๋ฉด์ ํ์ํ์ง ์์ ๋Unmount
๋๋ค๊ณ ํ ์ ์๋ค. Unmount
ํ๋ ค๋ฉด ํ์ฌ ๋ ๋๋ง ํ๊ณ ์๋Component
์ ์ํ ๊ฐ์ ๋น ๊ฐ์ผ๋ก ์์ ํด์ผ ํ๋ค.Class Component
์์๋componentWillUnmount()
ํจ์๊ฐ ํธ์ถ๋๋ค.
์ค์ต
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React from "react";
const style = {
container: {
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "flex-start",
gap: 24,
padding: "60px 20px",
background: "linear-gradient(135deg, #fdfbfb 0%, #ebedee 100%)",
minHeight: "100vh",
fontFamily: "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
color: "#2c3e50"
}
};
class Alert extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.style = {
wrapper: {
width: 800,
height: 130,
border: "0",
borderRadius: 25,
display: "flex",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#ffffff",
boxShadow: "0 4px 12px rgba(0, 0, 0, 0.1)",
fontSize: 18,
fontWeight: "500"
}
};
}
componentDidMount() {
console.log(`${this.props.id} componentDidMount() called!`);
}
componentDidUpdate() {
console.log(`${this.props.id} componentDidUpdate() called!`);
}
componentWillUnmount() {
console.log(`${this.props.id} componentWillUnmount() called!`);
}
render() {
return (
<div style={this.style.wrapper}>
<span>{this.props.message}</span>
</div>
);
}
}
const reservedAlerts = [
{
id: 1,
message: "์ฌ์ดํธ์ ๊ธ์ด ์
๋ก๋ ๋์์ต๋๋ค."
},
{
id: 2,
message: "์ ์ฌ ์์ฌ ์๊ฐ์
๋๋ค."
},
{
id: 3,
message: "๊ณง ํ์๊ฐ ์์๋ฉ๋๋ค."
}
];
var timer;
class AlertList extends React.Component {
constructor(props) {
super(props);
this.state = {
alerts: []
};
}
componentDidMount() {
const { alerts } = this.state;
timer = setInterval(() => {
if (alerts.length < reservedAlerts.length) {
const index = alerts.length;
alerts.push(reservedAlerts[index]);
this.setState({
alerts: alerts
});
} else {
this.setState({
alerts: []
});
clearInterval(timer);
}
}, 1000);
}
componentWillUnmount() {
if (timer) {
clearInterval(timer);
}
}
render() {
return (
<div style={style.container}>
{this.state.alerts.map((alert) => {
return (
<Alert
key={alert.id}
id={alert.id}
message={alert.message}
/>
);
})}
</div>
);
}
}
export default AlertList;
- ์๋ฆผ์ ์์ฐจ์ ์ผ๋ก ๋ ๋๋ง ํ๊ณ , ์๋ฆผ์ด ๋ชจ๋ ๋ ๋๋ง ๋์์ ๊ฒฝ์ฐ ํ๋ฉด์ ๋น์ด๋ค.
- ์์ฑ์์์๋
props
๋ฅผReact.Component
Class
์ ์ ๋ฌํ๊ณ ,alerts
State
๋ฅผ ์ ์ธํ๋ค. - ํด๋น
Component
๊ฐMount
๋ ๋ 1์ด๋ง๋ค ๊ฐฑ์ ๋ ์๋ฆผ ๋ชฉ๋ก์State
๊ฐ์ ๋ณ๊ฒฝํด ์ค๋ค. State
๊ฐ์ ๋ณ๊ฒฝํจ์ผ๋ก์จrendor()
ํจ์๊ฐ ์คํ๋์ด ๋ค์ ๋ ๋๋ง ๋๋ ๊ฒ์ด๋ค.- ์๋ฆผ ๋ชฉ๋ก์ด ๋ชจ๋ ๋ ๋๋ง ๋์์ ๋,
State
๊ฐ์ ๋น ๊ฐ์ ๋ฃ์ด์ ํ๋ฉด์ ๋น์ด๋ค. - ํด๋น
Component
๊ฐUnmount
๋ ๋ ํ์ด๋จธ์setInterval()
ํจ์๊ฐ ์ค๋จ๋๋ค. - ์ด๋ฒ ์ค์ต์์๋
index.js
์<React.StrictMode></React.StrictMode>
๋ผ์ธ์ ์ ๊ฑฐํด์ผ ํ๋ค. - ํด๋น ์ฝ๋๋
Side Effect
๋ฅผ ๊ฐ์งํ๊ธฐ ์ํด ์ผ๋ถ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋ฅผ ๋ ๋ฒ ํธ์ถํ๋๋ฐ, ๊ฐ๋ฐ ํ๊ฒฝ์์๋ง ๋์ํ๊ณ ์ค์ ์ด์ ํ๊ฒฝ์์๋ ๋์ํ์ง ์๋๋ค๊ณ ํ๋ค.
- ์์ฐจ์ ์ผ๋ก ์๋ฆผ์ด ์ถ๊ฐ๋๊ณ , ๋ง์ง๋ง์ ํ๋ฉด์ ๋น์ด๋ค.
- ์ฒซ ๋ฒ์งธ ์์
Mount
- ์ฒซ ๋ฒ์งธ ์์
Update
, ๋ ๋ฒ์งธ ์์Mount
- ์ฒซ ๋ฒ์งธ, ๋ ๋ฒ์งธ ์์
Update
, ์ธ ๋ฒ์งธ ์์Mount
- ์ฒซ ๋ฒ์งธ, ๋ ๋ฒ์งธ, ์ธ ๋ฒ์งธ ์์ ๋ชจ๋
Unmount
ํ๊ณ
Class Component
๋ฅผ ํตํดReact Component
์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ดํด ํ ์ ์์๋ค.Functional Component
์์๋Hook
์ ์ด์ฉํด ์๋ช ์ฃผ๊ธฐ ํจ์๊ฐ ํธ์ถ๋๋ค๊ณ ํ๋ค.
This post is licensed under CC BY 4.0 by the author.