Lifecycle Methods (์๋ช ์ฃผ๊ธฐ ๋ฉ์๋)
์ปดํฌ๋ํธ์ ์๋ช
์ฃผ๊ธฐ ๋์ ํน์ ์์ ์ ์คํ๋๋ ๋ฉ์๋
Mounting
๋ฉ์๋ | ์ค๋ช
|
constructor(props) | ์ปดํฌ๋ํธ๋ฅผ ์ด๊ธฐํํ๊ณ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํฉ๋๋ค. |
getDerivedStateFromProps() | ํ๋กญ์ค์ ๋ณ๊ฒฝ์ ๋ฐ๋ผ ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. |
render() | ํ์ฌ ์ํ์ ํ๋กญ์ค์ ๊ธฐ๋ฐํ์ฌ ์ปดํฌ๋ํธ UI๋ฅผ ๋ ๋๋งํฉ๋๋ค. |
componentDidMount() | ์ปดํฌ๋ํธ๊ฐ DOM์ ๋ง์ดํธ๋ ํ์ ํธ์ถ๋ฉ๋๋ค. |
Updating
๋ฉ์๋ | ์ค๋ช
|
getDerivedStateFromProps() | ํ๋กญ์ค์ ๋ณ๊ฒฝ์ ๋ฐ๋ผ ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. |
shouldComponentUpdate() | ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋งํด์ผ ํ๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํฉ๋๋ค. |
render() | ํ์ฌ ์ํ์ ํ๋กญ์ค์ ๊ธฐ๋ฐํ์ฌ ์ปดํฌ๋ํธ UI๋ฅผ ๋ ๋๋งํฉ๋๋ค. |
getSnapshotBeforeUpdate() | DOM์ด ์
๋ฐ์ดํธ๋๊ธฐ ์ ์ ์ ๋ณด๋ฅผ ์บก์ฒํฉ๋๋ค. |
componentDidUpdate() | ์ปดํฌ๋ํธ๊ฐ DOM์์ ์
๋ฐ์ดํธ๋ ํ์ ํธ์ถ๋ฉ๋๋ค. |
Unmounting
๋ฉ์๋ | ์ค๋ช
|
componentWillUnmount() | ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ ํด์ ๋๊ณ ์๋ฉธ๋๊ธฐ ์ง์ ์ ํธ์ถ๋ฉ๋๋ค. |
์ปดํฌ๋ํธ ์๋ช ์ฃผ๊ธฐ (Lifecycle)
React ์ปดํฌ๋ํธ๋ฅผ ๋ชจ๋ํฐํ๊ณ ์กฐ์ํ ์ ์๋ 3๊ฐ์ง ์ฃผ์ ๋จ๊ณ
Mounting
: ์ปดํฌ๋ํธ์ ์ธ์คํด์ค๊ฐ DOM ์ ์ฝ์
๋๋ ๋จ๊ณ
Updating
: ์ปดํฌ๋ํธ๊ฐ ๋ณ๊ฒฝ๋๋ ๋จ๊ณ
Unmounting
: ์ปดํฌ๋ํธ๊ฐ DOM ์์ ์ ๊ฑฐ๋๋ ๋จ๊ณ
Mounting
โข
constructor(props)
โข
getDerivedStateFromProps()
โข
render()
โข
componentDidMount()
constructor(props)
โข
์ปดํฌ๋ํธ์ ์์ฑ์ ๋ฉ์๋๋ก, ์ปดํฌ๋ํธ๊ฐ ์์ฑ๋ ๋ ์ฒ์์ผ๋ก ํธ์ถ๋ฉ๋๋ค.
โข
props๋ฅผ ์ธ์๋ก ๋ฐ์ super(props)๋ก ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํฉ๋๋ค.
โข
์ฃผ๋ก ์ด๊ธฐ ์ํ์ ์ค์ ๊ณผ ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ฐ์ธ๋ฉ์ด ์ด๋ฃจ์ด์ง๋๋ค.
getDerivedStateFromProps()
โข
Mounting ๋จ๊ณ์์, ์ปดํฌ๋ํธ๊ฐ ์์ฑ๋๊ณ render ๋ฉ์๋๊ฐ ํธ์ถ๋๊ธฐ ์ ์ ์คํ๋ฉ๋๋ค.
โข
props์ state๋ฅผ ์ธ์๋ก ๋ฐ์ ์๋ก์ด ์ํ๋ฅผ ๋ฐํํฉ๋๋ค.
โข
์ผ๋ฐ์ ์ผ๋ก props์ ์์กด์ ์ธ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
render()
โข
render ๋ฉ์๋๋ ์ปดํฌ๋ํธ์ UI๋ฅผ ์ ์ํ๊ณ , React ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค.
โข
render ๋ฉ์๋์ ์คํ ๊ฒฐ๊ณผ๊ฐ ์ค์ DOM์ ๋ ๋๋ง๋๊ธฐ ์ ์ getDerivedStateFromProps์ ํจ๊ป ํธ์ถ๋ฉ๋๋ค.
componentDidMount()
โข
์ปดํฌ๋ํธ๊ฐ DOM์ ์ฝ์
๋ ํ์ ํธ์ถ๋ฉ๋๋ค.
โข
์ฃผ๋ก ๋คํธ์ํฌ ์์ฒญ, ์ด๊ธฐ ๋ฐ์ดํฐ ๋ก๋ฉ ๋ฑ์ ๋ถ์ ํจ๊ณผ๋ฅผ ์ํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
โข
์ด ๋จ๊ณ์์ ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ฉด ์ปดํฌ๋ํธ๊ฐ ๋ค์ ๋ ๋๋ง๋์ด UI๊ฐ ๊ฐฑ์ ๋ฉ๋๋ค.
Updating
โข
getDerivedStateFromProps()
โข
shouldComponentUpdate()
โข
render()
โข
getSnapshotBeforeUpdate()
โข
componentDidUpdate()
getDerivedStateFromProps()
โข
Updating ๋จ๊ณ์์, ์ปดํฌ๋ํธ๊ฐ ์
๋ฐ์ดํธ๋๊ณ render ๋ฉ์๋๊ฐ ํธ์ถ๋๊ธฐ ์ ์ ์คํ๋ฉ๋๋ค.
โข
props์ state๋ฅผ ์ธ์๋ก ๋ฐ์ ์๋ก์ด ์ํ๋ฅผ ๋ฐํํฉ๋๋ค.
โข
์ฃผ๋ก props์ ์์กดํ๋ ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
shouldComponentUpdate()
โข
์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง์ ๊ฒฐ์ ํ๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.
โข
true๋ฅผ ๋ฐํํ๋ฉด ๋ฆฌ๋ ๋๋ง์ด ๋ฐ์ํ๊ณ , false๋ฅผ ๋ฐํํ๋ฉด ๋ฆฌ๋ ๋๋ง์ด ๋ฐฉ์ง๋ฉ๋๋ค.
โข
์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํด ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
render()
โข
render ๋ฉ์๋๋ ์ปดํฌ๋ํธ์ UI๋ฅผ ์ ์ํ๊ณ , React ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค.
โข
์
๋ฐ์ดํธ๊ฐ ๋ฐ์ํ ๋๋ง๋ค ํธ์ถ๋์ด UI๋ฅผ ๊ฐฑ์ ํฉ๋๋ค.
getSnapshotBeforeUpdate()
โข
render ๋ฉ์๋ ํธ์ถ ์ดํ, ์ค์ ๋ก DOM์ ์ปค๋ฐ๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.
โข
์ฃผ๋ก ์
๋ฐ์ดํธ ์ ์ DOM ์ํ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์
์ ์ํํ๊ณ ์ถ์ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
componentDidUpdate()
โข
์ปดํฌ๋ํธ๊ฐ ์
๋ฐ์ดํธ๋ฅผ ์๋ฃํ ํ์ ํธ์ถ๋ฉ๋๋ค.
โข
์ด ๋จ๊ณ์์ ์ด์ props ๋ฐ state์ ์ต์ props ๋ฐ state์ ์ ๊ทผํ ์ ์์ต๋๋ค.
โข
์ฃผ๋ก ์
๋ฐ์ดํธ ์ดํ์ ์์
์ ์ํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
Unmounting
โข
componentWillUnmout()
componentWillUnmout()
โข
์ปดํฌ๋ํธ๊ฐ DOM์์ ์ ๊ฑฐ๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.
โข
์ด ๋ฉ์๋ ๋ด์์๋ ๋ฆฌ์์ค ํด์ ๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ ๊ฑฐ ๋ฑ์ ์ ๋ฆฌ ์์
์ ์ํํ ์ ์์ต๋๋ค.
โข
์ฃผ๋ก ์ปดํฌ๋ํธ์ ์ ๋ฆฌ ์์
์ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.
์์์ฝ๋
import React, { Component } from 'react';
class LifecycleClass extends Component {
constructor(props) {
super(props);
console.log('constructor: ์ปดํฌ๋ํธ๊ฐ ์์ฑ ์ค์
๋๋ค.');
this.state = {
data: null,
};
}
static getDerivedStateFromProps(nextProps, prevState) {
console.log('getDerivedStateFromProps: nextProps์ prevState๋ฅผ ๋ฐ์ต๋๋ค. ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด null์ ๋ฐํํฉ๋๋ค.');
return null;
}
componentDidMount() {
console.log('componentDidMount: ์ปดํฌ๋ํธ๊ฐ DOM์ ๋ง์ดํธ๋์์ต๋๋ค.');
// ๋น๋๊ธฐ ์์
, ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ ๋ฑ์ ์ํํฉ๋๋ค.
this.fetchData();
}
shouldComponentUpdate(nextProps, nextState) {
console.log('shouldComponentUpdate: ์ปดํฌ๋ํธ๊ฐ ๋ค์ ๋ ๋๋ง๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค. ๋ถ๋ฆฌ์ธ ๊ฐ์ ๋ฐํํฉ๋๋ค.');
return true;
}
getSnapshotBeforeUpdate(prevProps, prevState) {
console.log('getSnapshotBeforeUpdate: ๊ฐ์ DOM์์ ์ค์ DOM์ผ๋ก ๋ณ๊ฒฝ ์ฌํญ์ด ๋ฐ์๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.');
return null;
}
componentDidUpdate(prevProps, prevState, snapshot) {
console.log('componentDidUpdate: ์ปดํฌ๋ํธ๊ฐ DOM์์ ์
๋ฐ์ดํธ๋ ํ์ ํธ์ถ๋ฉ๋๋ค.');
}
componentWillUnmount() {
console.log('componentWillUnmount: ์ปดํฌ๋ํธ๊ฐ DOM์์ ์ ๊ฑฐ๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.');
}
fetchData() {
// ๋น๋๊ธฐ ์์
์ ์๋ฎฌ๋ ์ด์
ํฉ๋๋ค.
setTimeout(() => {
console.log('๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๊ฐ์ ธ์์ต๋๋ค!');
this.setState({ data: '๊ฐ์ ธ์จ ๋ฐ์ดํฐ' });
}, 2000);
}
render() {
console.log('render: ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง ์ค์
๋๋ค.');
return (
<div>
<h1>์ปดํฌ๋ํธ ์๋ช
์ฃผ๊ธฐ ์์ </h1>
<p>๋ฐ์ดํฐ: {this.state.data}</p>
</div>
);
}
}
export default LifecycleClass;
JavaScript
๋ณต์ฌ
import React, { useState, useEffect, useRef, useMemo } from 'react';
const LifecycleFunction = (props) => {
// constructor ๋์ useState๋ก ์ํ ์ด๊ธฐํ
console.log('constructor: ์ปดํฌ๋ํธ๊ฐ ์์ฑ ์ค์
๋๋ค.');
const [data, setData] = useState(null);
const [count, setCount] = useState(0);
const [otherValue, setOtherValue] = useState(0);
// useMemo - ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ ๋ฉ๋ชจ์ด์ ์ด์
ํ
// - ์ฒซ ๋ฒ์งธ ์ธ์: ๋ฉ๋ชจ์ด์ ์ด์
ํ ๊ฐ์ ๊ณ์ฐํ๋ ํจ์
// - ๋ ๋ฒ์งธ ์ธ์: ์์กด์ฑ ๋ฐฐ์ด - ๋ฐฐ์ด ๋ด ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋๋ง ์ฌ๊ณ์ฐ
// - ์์กด์ฑ ๋ฐฐ์ด์ ๊ฐ์ด ๋ณํ์ง ์์ผ๋ฉด ์ด์ ์ ๊ณ์ฐ๋ ๊ฐ์ ์ฌ์ฌ์ฉ
// - ๋ณต์กํ๊ฑฐ๋ ๋น์ฉ์ด ๋ง์ด ๋๋ ๊ณ์ฐ์ ์ต์ ํํ ๋ ์ฌ์ฉ
// - ๋ ๋๋ง๋ง๋ค ์คํ๋์ง ์์ ๋ถํ์ํ ๊ณ์ฐ์ ๋ฐฉ์ง
// ๋ฉ๋ชจ์ด์ ์ด์
์ด๋?
// : ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ์ฌ ๋์ผํ ์
๋ ฅ์ ๋ํด ๋ค์ ๊ณ์ฐํ์ง ์๊ณ ์ ์ฅ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ์ต์ ํ ๊ธฐ๋ฒ
const expensiveCalculation = useMemo(() => {
console.log('useMemo: ๋ณต์กํ ๊ณ์ฐ์ ์ํ ์ค...');
// ๋ณต์กํ ๊ณ์ฐ์ ์๋ฎฌ๋ ์ด์
(count๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง ์คํ)
let result = 0;
for (let i = 0; i < 1000000; i++) {
result += count;
}
return result;
}, [count]); // count๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง ์ฌ๊ณ์ฐ, otherValue๊ฐ ๋ณํด๋ ์ฌ๊ณ์ฐ ์ ํจ
// getDerivedStateFromProps - ๋ ๋๋ง ์ค์ ์ฒ๋ฆฌ
console.log('getDerivedStateFromProps: nextProps์ prevState๋ฅผ ๋ฐ์ต๋๋ค. ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด null์ ๋ฐํํฉ๋๋ค.');
// ์ด์ ์ํ๋ฅผ ์ถ์ ํ๊ธฐ ์ํ ref (componentDidUpdate์ฉ)
const prevDataRef = useRef();
// getSnapshotBeforeUpdate ๋์ฒด - DOM ์
๋ฐ์ดํธ ์ ๊ฐ ์ ์ฅ
useEffect(() => {
console.log('getSnapshotBeforeUpdate: ๊ฐ์ DOM์์ ์ค์ DOM์ผ๋ก ๋ณ๊ฒฝ ์ฌํญ์ด ๋ฐ์๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.');
prevDataRef.current = data;
});
// componentDidMount - ๋ง์ดํธ ์ ํ ๋ฒ๋ง ์คํ
useEffect(() => {
console.log('componentDidMount: ์ปดํฌ๋ํธ๊ฐ DOM์ ๋ง์ดํธ๋์์ต๋๋ค.');
// ๋น๋๊ธฐ ์์
, ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ ๋ฑ์ ์ํํฉ๋๋ค.
fetchData();
// componentWillUnmount - cleanup ํจ์
return () => {
console.log('componentWillUnmount: ์ปดํฌ๋ํธ๊ฐ DOM์์ ์ ๊ฑฐ๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.');
};
}, []); // ๋น ๋ฐฐ์ด: ๋ง์ดํธ/์ธ๋ง์ดํธ ์์๋ง ์คํ
// componentDidUpdate - data๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค ์คํ
useEffect(() => {
if (prevDataRef.current !== undefined) {
console.log('componentDidUpdate: ์ปดํฌ๋ํธ๊ฐ DOM์์ ์
๋ฐ์ดํธ๋ ํ์ ํธ์ถ๋ฉ๋๋ค.');
}
}, [data]);
// shouldComponentUpdate - React.memo๋ก ๋์ฒด ๊ฐ๋ฅ (์ปดํฌ๋ํธ ์ธ๋ถ์์)
console.log('shouldComponentUpdate: ์ปดํฌ๋ํธ๊ฐ ๋ค์ ๋ ๋๋ง๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค. ๋ถ๋ฆฌ์ธ ๊ฐ์ ๋ฐํํฉ๋๋ค.');
const fetchData = () => {
// ๋น๋๊ธฐ ์์
์ ์๋ฎฌ๋ ์ด์
ํฉ๋๋ค.
setTimeout(() => {
console.log('๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๊ฐ์ ธ์์ต๋๋ค!');
setData('๊ฐ์ ธ์จ ๋ฐ์ดํฐ');
}, 2000);
};
// render
console.log('render: ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง ์ค์
๋๋ค.');
return (
<div>
<h1>์ปดํฌ๋ํธ ์๋ช
์ฃผ๊ธฐ ์์ </h1>
<p>๋ฐ์ดํฐ: {data}</p>
<hr />
<h2>useMemo ์์ </h2>
<p>Count: {count}</p>
<p>๊ณ์ฐ๋ ๊ฐ (useMemo): {expensiveCalculation}</p>
<p>๋ค๋ฅธ ๊ฐ: {otherValue}</p>
<button onClick={() => setCount(count + 1)}>
Count ์ฆ๊ฐ (useMemo ์ฌ๊ณ์ฐ)
</button>
<button onClick={() => setOtherValue(otherValue + 1)}>
๋ค๋ฅธ ๊ฐ ์ฆ๊ฐ (useMemo ์ฌ๊ณ์ฐ ์ ํจ)
</button>
<p style={{ fontSize: '12px', color: '#666', marginTop: '10px' }}>
๐ก "๋ค๋ฅธ ๊ฐ ์ฆ๊ฐ" ๋ฒํผ์ ํด๋ฆญํด๋ useMemo๋ ์ฌ๊ณ์ฐ๋์ง ์์ต๋๋ค. (์ฝ์ ํ์ธ)
</p>
</div>
);
};
export default LifecycleFunction;
JavaScript
๋ณต์ฌ





.png&blockId=6fe4f177-713e-43f6-8a80-23aaaf4acf79)
.png&blockId=7e1c8382-f5aa-43c1-88fe-c84376f12764)
.png&blockId=f4218f1b-d446-43a4-a372-0ee749582df3)
.png&blockId=e9c3ff52-3af3-4dc3-a61d-55f85d396ca6)
_-_Updating.png&blockId=8f053c29-12dd-4cd2-9d94-bf495b4c06b5)
__-_Updating.png&blockId=6b4ee330-0bac-4954-92f6-42c598b6f3b2)
__-_Updating.png&blockId=ddf72168-e16d-42bd-b3d6-f2dbe6ec9601)
__-_Updating.png&blockId=630269af-f741-4bb0-84b4-5cbb12b5122f)
__-_Updating.png&blockId=8b54b35c-2597-4918-8649-e7502596a3d2)
_-_Unmounting.png&blockId=f45c6b81-23f3-4943-ab5e-e47484f37851)