๋ฆฌ์กํธ๋ 3๊ฐ์ Life Cycle์ ๊ฐ์ง๊ณ ์๋๋ฐ ๊ทธ 3๊ฐ์ง๋ Mounting
, Updating
, Unmounting
์๋ค.
Mounting
์ ์ปดํฌ๋ํธ๊ฐ ์์๋ ๋
Updating
์ state๊ฐ ์
๋ฐ์ดํธ ๋๊ณ UI๊ฐ ์๋ฐ์ดํธ ๋ ๋
Unmounting
์ ์ปดํฌ๋ํธ๊ฐ ์ข
๋ฃ๋ ๋
Class Component
์์ ์ฒ์์ผ๋ก constructor
์์ state
๋ฅผ ์์ฑํ๋ค. ๊ทธ ์ด์ ๋ Life Cycle
์์ ์ฒซ ๋ฒ์งธ๋ก ์คํ๋๋ Life Cycle
ํจ์์ด๋ค. ๊ทธ๋์ ์ฑ์ ์์ํ์๋ง์ ์คํํ๊ณ ์ถ๋ค๋ฉด constructor
์์ ๋ฃ์ผ๋ฉด ๋๋ค.
import React, { Component } from 'react'
import './App.css';
import BoxClass from './component/BoxClass';
export default class Appclass extends Component {
constructor(props){ // constructor ๋ฆฌ์กํธ์์ ์ ๊ณตํ๋ lifecycle ํจ์ ์ค ํ๋ (์์ฑ์) : ์ปดํฌ๋ํธ๊ฐ ์คํ๋์ ๋ง์ ํจ์์คํ
super(props)
this.state={ // state๋ฅผ ์ ์ธํ๋ค.
counter2 : 0, //์ํ๋ state๋ฅผ ์ ์ธํด ์ฃผ๋ ๊ฒ์ด๋ค.
num:1,
value:0
};
}
}
getDerivedStateFromProps
๋ ๊ฐ๋จํ๊ฒ ์ค๋ช
ํ๋ฉด state์ props๋ฅผ ๋๊ธฐํ์์ผ์ฃผ๋ ํจ์์ด๋ค.
props๋ก ๋ฐ์ ์จ ๊ฐ์ state์ ๋๊ธฐํ์ํค๋ ์ฉ๋๋ก ์ฌ์ฉํ๋ฉฐ, ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ๋ ๋์ ์
๋ฐ์ดํธ ๋ ๋ ํธ์ถ๋๋ค.
function ScrollView({row}) {
const [isScrollingDown, setIsScrollingDown] = useState(false);
const [prevRow, setPrevRow] = useState(null);
if (row !== prevRow) {
// ๋ง์ง๋ง ๋ ๋๋ง ์ดํ ํ์ด ๋ณ๊ฒฝ๋์์ต๋๋ค. isScrollingDown์ ์
๋ฐ์ดํธํฉ๋๋ค.
setIsScrollingDown(prevRow !== null && row > prevRow);
setPrevRow(row);
}
return `Scrolling down: ${isScrollingDown}`;
}
render
๋ UI๋ฅผ ๊ทธ๋ ค์ฃผ๋ ํจ์์ด๋ค.
์ด๋ ๊ฐ์ฅ ๊ธฐ์ด์ ์ธ ๋ฉ์๋์ด๊ธฐ๋ํ๊ณ ๊ฐ์ฅ ์ค์ํ ๋ฉ์๋์ด๊ธฐ๋ ํ๋ค. ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ ๋ ํ์ํ ๋ฉ์๋๋ก ์ ์ผํ ํ์ ๋ฉ์๋์ด๊ธฐ๋ ํ๋ค. ํจ์ํ ์ปดํฌ๋ํธ์์๋ render๋ฅผ ์์ฐ๊ณ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ ์ ์๋ค.
render() {
console.log("render");
return (
<div>
<div>state: {this.state.counter2}</div> {/* class ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฅผ๋๋ counter2๋ง ์ ์ธํ๋ ๊ฒ์ด์๋๋ผ. this.state ๋ฅผ ๋งค๋ฒ ๋ถ์ฌ์ค์ผ ํ๋ค.*/}
<button onClick={this.increase}>ํด๋ฆญ!</button>
<BoxClass num={this.state.value}/>
</div>
)
}
UI๊ฐ ์
ํ
์ด ์๋ฒฝํ๊ฒ ๋์ ๋ ์๋ ค์ค๋ค.
์ฃผ๋ก API ํธ์ถ ์์
๋ค์ ํ๋ค. ์ฌ์ฉ์๊ฐ ์ฑ์ ์คํํ์ ๋ UI๊ฐ ๋จผ์ ๊ทธ๋ ค์ง ์ํ๋ก API๊ฐ ํธ์ถ ๋์ด์ผ ํ๋ค. UI๊ฐ ๊ทธ๋ ค์ง์ง ์์ ์ํ๋ก API๋ฅผ ํธ์ถ ํ์ ๋ ํ์๊ฐ ์๋๋ค. ๊ทธ๋์ render
๋ค์ ๋จ๊ณ์ธ componentDidMount
์ API๋ฅผ ํธ์ถ ํด์ฃผ๋ ๊ฒ์ด๋ค.
๊ฐ๋จํ๊ฒ ๋งํ๋ฉด component
๊ฐ render
๋ฅผ ํ ์ง ๊ฒฐ์ ํ๋ ํจ์์ด๋ค.
Updating
์ํฉ์ผ ๋ ์๋๋๋ค. state
๊ฐ ์
๋ฐ์ดํธ ๋๊ฑฐ๋ setState
๊ฐ ๋ฐ์ ๋ ๋ rnder
๊ฐ ๋ฐ์์ด ๋๋ค. ๊ทธ๋ฆฌ๊ณ render
๊ฐ ๋ฐ์๋๋ฉด componentDidUpdate
๊ฐ ํธ์ถ๋๋ค. ์ ํธ์ถ์ ํด์ฃผ๋๋ฉด ์ฐ๋ฆฌ๊ฐ setState
๋ฅผ ํด๋ ๋น๋๊ธฐ์์ผ๋ก ๊ธฐ๋ค๋ ธ๋ค๊ฐ ๋์ค์ ํธ์ถ๋๋ค. ๊ทธ๋์ state
๊ฐ update
๊ฐ ๋์๋์ง ์๋ ค์ฃผ๋ ๊ฒ์ด๋ค.
์ด ๋ฉ์๋๋ ์ปดํฌ๋ํธ๋ฅผ DOM์์ ์ ๊ฑฐํ ๋ ์คํํ๋ค. componentDidMount์์ ๋ฑ๋กํ ์ด๋ฒคํธ๊ฐ ์๋ค๋ฉด ์ฌ๊ธฐ์ ์ ๊ฑฐ ์์ ์ ํด์ผํ๋ค. ํจ์ํ ์ปดํฌ๋ํธ์์๋ useEffect CleanUp ํจ์๋ฅผ ํตํด ํด๋น ๋ฉ์๋๋ฅผ ๊ตฌํํ ์ ์๋ค.
ํ์ง๋ง ์์ ์๋ ๊ฒ๋ค์ Class Component๋ง ์ฌ์ฉํ ์ ์๋ค.
๊ทธ๋ ๋ค๋ฉด Function Component์๋ ์ด๋ป๊ฒ ์ฌ์ฉํ๋ ์์๋ณด์. ๊ทธ๊ฒ์ ๋ฐ๋ก useEfffect()์ด๋ค.
8. useEffect
useEffect
๋ ๊ธฐ๋ณธ์ ์ผ๋กcomponentDidMount
,componentDidUpdate
,componentWillUnmount
,getDerivedStateFromProps
์ ์ญํ ์ ๋ชจ๋ ํ๋ค.
๋๋ฌธ์ ์ ์ฝ๋๋ 1) ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ ๋ ํ, 2) ์ปดํฌ๋ํธ๊ฐ ์ ๋ฐ์ดํธ๋๊ณ ๋ ํ 3) ์ปดํฌ๋ํธ๊ฐ ์ธ๋ง์ดํธ ๋๊ธฐ ์ ๋ชจ๋ ์คํ๋๋ค.useEffect(()=>{ console.log("useEffect2 Firee", counter); },[counter]) //์ด๋ ์ด์ ๊ฐ์ ๋ฃ์ผ๋ฉด ์ปดํจํฐ๊ฐ ์ฃผ์ํ๋ค. ์ฆ ๊ฐ์ด ๋ฐ๋๋ฉด `componentDidUpdate`๊ฐ ๋๋ค. //์ด๋ ์ด์ ๋ด์ฉ์ด ํ๋๋ผ๋ ๋ฐ๋๋ฉด render๊ฐ ๋๋ค.