[React] Class Component์˜ Lifecycle Methods

soyiยท2021๋…„ 4์›” 8์ผ
0

React ๋ฆฌ์•กํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
3/4
post-thumbnail

๐Ÿš€ Lifecycle Methods in Class Component


๐Ÿ“Œ React Lifecycle Methods Diagram

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : React Lifecycle Methods Diagram - github.com/wojtekmaj


๐Ÿ“Œ Mount์—์„œ ํ˜ธ์ถœ๋˜๋Š” Lifecycle Methods

  • ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด DOM ์ƒ์— ์‚ฝ์ž…๋  ๋•Œ ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœ๋˜๋Š” methods

๐ŸŒฑ 1. constructor()

constructor(props) {
  super(props);
  this.state = { counter: 0 };
  this.handleClick = this.handleClick.bind(this);
}
  • constructor ์ƒ์„ฑ์ž๋กœ, ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ์ฒ˜์Œ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ.
  • ์ดˆ๊ธฐ state๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ์Œ. (setState ํ˜ธ์ถœ ์—†์ด this.state๋กœ ์ดˆ๊ธฐ state ๊ฐ’ ์ง€์ •)
  • ๋ฉ”์„œ๋“œ ๋ฐ”์ธ๋”ฉ์ด๋‚˜ state ์ดˆ๊ธฐํ™” ์ž‘์—…์ด ์—†๋‹ค๋ฉด ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๋ฉ”์„œ๋“œ.
  • ๐Ÿ’ก ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์—์„œ๋Š” state์— props๋ฅผ ๋ณต์‚ฌํ•˜์ง€ ์•Š๋„๋ก! props์˜ ๊ฐฑ์‹ ์„ ์˜๋„์ ์œผ๋กœ ๋ฌด์‹œํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ state์— prop๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค.

๐ŸŒฑ 2. static getDerivedStateFromProps(props, state)

static getDerivedStateFromProps(nextProps, prevState) {
  console.log('getDerivedStateFromProps');
  if (nextProps.color !== prevState.color) {  // ์กฐ๊ฑด์— ๋”ฐ๋ผ
    // ํŠน์ • ๊ฐ’ ๋™๊ธฐํ™”ํ•˜์—ฌ state ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด state return
    return { color: nextProps.color };  
  }
  return null;    // state ๋ณ€๊ฒฝ ํ•„์š”๊ฐ€ ์—†์œผ๋ฉด null ๋ฐ˜ํ™˜
}
  • ์ปดํฌ๋„ŒํŠธ ์ตœ์ดˆ ๋งˆ์šดํŠธ์™€ ์—…๋ฐ์ดํŠธ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, render() ํ˜ธ์ถœ ์ง์ „์— ํ˜ธ์ถœ๋จ.
  • ์‹œ๊ฐ„์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ๋ณ€ํ•˜๋Š” props์— state๊ฐ€ ์˜์กดํ•˜๋Š” ๋“œ๋ฌธ ์˜ˆ๋ฅผ ์œ„ํ•˜์—ฌ ์กด์žฌ. props๋กœ ๋ฐ›์•„์˜จ ๊ฐ’์„ state์— ๋™๊ธฐํ™”์‹œํ‚ค๋Š” ์šฉ๋„.

๐ŸŒฑ 3. render()

  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„๋ผ์•ผํ•˜๋Š” ์œ ์ผํ•œ ๋ฉ”์„œ๋“œ.
  • ์ปดํฌ๋„ŒํŠธ๋Š” jsx๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•˜๋Š”๋ฐ, ํด๋ž˜์Šค๋Š” return ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” JSX๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด render() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฆฌ์•กํŠธ๋Š” ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ render() ํ•จ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ์‹คํ–‰์‹œํ‚จ๋‹ค.
  • render() ํ•จ์ˆ˜๋Š” ์ˆœ์ˆ˜ํ•ด์•ผํ•œ๋‹ค. ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๊ณ , ๋ธŒ๋ผ์šฐ์ €์™€ ์ง์ ‘ ์ƒํ˜ธ์ž‘์šฉ ํ•˜์ง€ ์•Š๋Š”๋‹ค. (๋ธŒ๋ผ์šฐ์ €์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ์ž‘์—…์€ ๋‹ค๋ฅธ ์ƒ๋ช… ์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ์—์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค.)

๐ŸŒฑ 4. componentDidMount()

componentDidMount() {
  console.log('componentDidMount');
}
  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋œ ์งํ›„, ์ฆ‰ ํŠธ๋ฆฌ์— ์‚ฝ์ž…๋˜์–ด ์ฒซ ๋ Œ๋”๋ง์„ ๋‹ค ๋งˆ์นœ ์งํ›„์— ํ˜ธ์ถœ๋จ.
  • ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๋ฐ์ดํ„ฐ ๊ตฌ๋…์„ ์„ค์ •ํ•˜๊ธฐ ์ข‹์€ ์œ„์น˜. ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํ”„๋ ˆ์ž„์›Œํฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ด๋ฒคํŠธ ๋“ฑ๋ก, setTimeout, setInterval, ๋„คํŠธ์›Œํฌ ์š”์ฒญ ๊ฐ™์€ ๋น„๋™๊ธฐ ์ž‘์—… ์ฒ˜๋ฆฌ ์œ„์น˜

๐Ÿ“Œ Update์—์„œ ํ˜ธ์ถœ๋˜๋Š” Lifecycle Methods

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ re-rendering ๋  ๋•Œ, ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœ๋˜๋Š” methods

๐ŸŒฑ 1. static getDerivedStateFromProps()

๐ŸŒฑ 2. shouldComponentUpdate(nextProps, nextState)

shouldComponentUpdate(nextProps, nextState) {
  console.log('shouldComponentUpdate', nextProps, nextState);

  // ์ˆซ์ž ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๊ฐ€ 4๋ฉด ๋ฆฌ๋ Œ๋”๋ง ์•ˆํ•จ.
  return nextState.number % 10 !== 4;
}
  • props ๋˜๋Š” state๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ๋˜์–ด ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋จ.
  • ํ˜„์žฌ state ๋˜๋Š” props์˜ ๋ณ€ํ™”๊ฐ€ ์ปดํฌ๋„ŒํŠธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ๋ฆฌ์•กํŠธ์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ณ , state ๋˜๋Š” props์˜ ๋ณ€๊ฒฝ์ด ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ๋ฆฌ๋ Œ๋”๋ง์„ ํ• ์ง€ ์ง€์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ '์„ฑ๋Šฅ ์ตœ์ ํ™”'๋ฅผ ์œ„ํ•œ ๊ฒƒ์ž„.
  • return์€ true or false๋กœ default๋Š” true. false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์—…๋ฐ์ดํŠธ ๊ณผ์ • ์ค‘์ง€.
  • ํ˜„์žฌ props์™€ state๋Š” this.props, this.state๋กœ ์ ‘๊ทผํ•˜๊ณ  ์ƒˆ๋กœ ์„ค์ •๋  props์™€ state๋Š” nextProps, nextState๋กœ ์ ‘๊ทผ.

๐ŸŒฑ 3. render()

๐ŸŒฑ 4. getSnapshotBeforeUpdate(prevProps, prevState)

getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log('getSnapshotBeforeUpdate');
        if (prevProps.color !== this.props.color) {
            return this.myRef.style.color;
        }
        return null;
    }
  
  • ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ Œ๋”๋ง๋œ ๊ฒฐ๊ณผ๊ฐ€ DOM ๋“ฑ์— ๋ฐ˜์˜๋˜์—ˆ์„ ๋•Œ, render()์—์„œ ๋งŒ๋“ค์–ด์ง„ ๊ฒฐ๊ณผ๋ฌผ์ด ๋ธŒ๋ผ์šฐ์ €์— ์‹ค์ œ๋กœ ๋ฐ˜์˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋จ.
  • componentDidUpdate()์˜ ์„ธ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ snapshot์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์ด ์ „๋‹ฌ๋จ.
    ์Šค๋ƒ…์ƒท ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    ์ฃผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ง์ „์— ๊ฐ’์„ ์ฐธ๊ณ ํ•  ์ผ์ด ์žˆ์„ ๋•Œ ํ™œ์šฉ๋œ๋‹ค.

๐ŸŒฑ 5. componentDidUpdate(prevProps, prevState, snapshot)

getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log('getSnapshotBeforeUpdate');
        if (prevProps.color !== this.props.color) {
            return this.myRef.style.color;
        }
        return null;
    }
  • ๊ฐฑ์‹ , ์—…๋ฐ์ดํŠธ๊ฐ€ ์ผ์–ด๋‚œ ์งํ›„์— ํ˜ธ์ถœ๋จ. (๋ฆฌ๋ Œ๋”๋ง์—์„œ๋งŒ ํ˜ธ์ถœ๋จ)
    DOM ์กฐ์ž‘์„ ์œ„ํ•ด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์ด์ „๊ณผ ํ˜„์žฌ props๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์ž‘์—…๋„ ์ด๋ฃจ์–ด์ง.
  • prevProps์™€ prevState๋กœ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด์ „์— ๊ฐ€์กŒ๋˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ.
  • ์ปดํฌ๋„ŒํŠธ์—์„œ getSnapshotBeforeUpdate()์„ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ๋ฉ”์„œ๋“œ ๋ฐ˜ํ™˜๊ฐ’์ด ์„ธ ๋ฒˆ์งธ "snapshot" ์ธ์ž๋กœ ๋„˜๊ฒจ์ง

๐Ÿ“Œ Unmount์—์„œ ํ˜ธ์ถœ๋˜๋Š” Lifecycle Methods

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ DOM ์ƒ์—์„œ ์ œ๊ฑฐ๋  ๋•Œ์— ํ˜ธ์ถœ๋˜๋Š” method

๐ŸŒฑ componentWillUnmount()

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ํ•ด์ œ๋˜์–ด ์ œ๊ฑฐํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋จ.
    componentDidMount() ๋‚ด์—์„œ ๋“ฑ๋กํ•œ ์ด๋ฒคํŠธ, ํƒ€์ด๋จธ ์ œ๊ฑฐ, ๊ตฌ๋… ํ•ด์ œ, ๋„คํŠธ์›Œํฌ ์š”์ฒญ ์ทจ์†Œ ๋“ฑ์˜ ์ •๋ฆฌ ์ž‘์—… ์ˆ˜ํ–‰

References

profile
ใƒพ(^โ–ฝ^*)))

0๊ฐœ์˜ ๋Œ“๊ธ€