[TIL] React-component lifecycle

link717ยท2020๋…„ 10์›” 13์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
11/53
post-thumbnail

๐Ÿ™„ Component lifecycle

  • Mounting: component๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  DOM์— ์—ฐ๊ฒฐํ•  ๋•Œ
    . lifecycle method: constructor(), render()
    constructor(): mounting ๊ตฌ๋ฌธ์—์„œ ์ฒซ๋ฒˆ์งธ๋กœ ํ˜ธ์ถœ๋˜๋Š” method
    render(): moundting ๊ตฌ๋ฌธ์—์„œ ๋‚˜์ค‘์— ํ˜ธ์ถœ๋˜๋Š” method์ด๋ฉด์„œ update๋„ ํ•˜๋Š” method์ด๋‹ค.
  • Updating: state๊ฐ€ ๋ฐ”๋€Œ๊ฑฐ๋‚˜ props ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ ๊ฒฐ๊ณผ ๋ฐ˜์˜ํ•  ๋•Œ
  • Unmounting: DOM์—์„œ component๊ฐ€ ์ œ๊ฑฐ๋  ๋•Œ
    โŽ ํ•œ ๋ฒˆ unmounting ๋œ component๋Š” re-mount/undate ํ•  ์ˆ˜ ์—†๋‹ค.
 class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }

  render() {
    return <div>{this.state.date.toLocaleTimeString()}</div>
  } 
};

ReactDOM.render(<Clock />, document.getElementById('app'));

์œ„์˜ ์ฝ”๋“œ์—์„œ Clock component๋ฅผ ์ฃผ๊ธฐ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•˜๋„๋ก ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์šฐ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? JavaScript์— ์œ ์šฉํ•œ setInterval()์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •ํ•ด์ง„ ์‹œ๊ฐ„๋งˆ๋‹ค ํ˜„์žฌ์‹œ๊ฐ„์ด ์—…๋ฐ์ดํŠธ ๋˜๋Š” ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

const oneSecond = 1000;
setInterval(() => {
  this.setState({ date: new Date() });
}, oneSecond);

์ด๋ ‡๊ฒŒ โ‘ก๋งŒ๋“ค์–ด์ง„ ํ•จ์ˆ˜๋Š” Clock component์˜ ์–ด๋””์— ์œ„์น˜ํ•˜๋ฉด ์ข‹์„๊นŒ?

  • counstructor() ์œ„์น˜?
    . constructor๋Š” ์˜ค์ง mounting์˜ ์ฑ…์ž„์„ ๊ฐ–๋Š”๋ฐ ๊ทธ ์œ„์น˜์— Clock ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋ฉด Single Responsibility Principle(๋‹จ์ผ ์ฑ…์ž„ ์›์น™)์„ ์œ„๋ฐ˜ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์ข‹์ง€ ์•Š๋‹ค.

  • render() ์œ„์น˜?
    . mounting๊ณผ updating cycle ์ค‘๊ฐ„์—์„œ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ๋„ˆ๋ฌด ์ž์ฃผ ์‹คํ–‰๋˜๊ณ  ๋ฏธ๋ฌ˜ํ•œ ๋ฒ„๊ทธ(?)๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์œ„ํ—˜์š”์†Œ๊ฐ€ ์žˆ์–ด์„œ ์ข‹์ง€ ์•Š๋‹ค.

    ๊ทธ๋Ÿฌ๋ฉด.... ๋„๋Œ€์ฒด ์–ด๋””์— ๋„ฃ์–ด์•ผ ํ•˜๋Š”๊ฑธ๊นŒ? OMG! ๐Ÿ˜ฅ

๐Ÿค— componentDidMount

componentDidMount()๋Š” mounting ์—์„œ ํ˜ธ์ถœ๋˜๋Š” method๋กœ component๊ฐ€ render ๋˜๊ณ ๋‚˜์„œ ํ˜ธ์ถœ๋œ๋‹ค. SRP๋ฅผ ์œ„๋ฐ˜ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋„ˆ๋ฌด ์ž์ฃผ ์‹คํ–‰๋˜์ง€๋„ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น method๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์•ˆ์ •์ ์œผ๋กœ clock ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
โŽ setState ๋“ฑ์˜ ์ฝ”๋“œ๋กœ re-rendering์ด ๋  ๋•Œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

  componentDidMount() {
    const oneSecond = 1000;
    setInterval(() => {
      this.setState({ date: new Date() });
    }, oneSecond);
  }

์ด ๋•Œ ํ•˜๋‚˜์˜ ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด๋ณด์ž! Clock component๊ฐ€ unmount ๋  ๋•Œ ์ € method๋Š” ์–ธ์ œ๊นŒ์ง€ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š”๊ฑธ๊นŒ..? ๐Ÿ™‰ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” setInterval์˜ ID๋ฅผ clearInterval์— ์ „๋‹ฌํ•˜์—ฌ setInterval์„ clear ํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ทธ๊ฒƒ์€ ์กฐ๊ธˆ์˜ interval์„ ๊ฐ–๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.์ด ๋•Œ clearInterval๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์—ฌ componentDidMount ๋™์ž‘์œผ๋กœ ์ธํ•ด ์ง€์ €๋ถ„ํ•ด์ง„ ๊ฒƒ๋“ค์„ ์ •๋ฆฌํ•˜๋„๋ก ํ•ด์ฃผ๋Š” method๊ฐ€ ์žˆ๋‹ค.

const oneSecond = 1000;
this.intervalID = setInterval(() => {
  this.setState({ date: new Date() });
}, oneSecond);

// Some time later...๐Ÿ˜‚
clearInterval(this.intervalID);

๐Ÿ˜‹ componentWillUnmount

componentWillUnmount์€ component๊ฐ€ ์™„์ „ํžˆ ์ œ๊ฑฐ๋˜๊ธฐ ์ง์ „, unmount๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ clock component๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ, ์ด method๋Š” ํ˜ธ์ถœ๋˜๋ฉฐ clock's interval์„ ์ •๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฐ ๊ณผ์ •์€ component์˜ unmount ๊ณผ์ •์„ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•ด ์ค„ ๊ฒƒ์ด๋‹ค.

  componentDidMount() {
    const oneSecond = 1000;
    this.intervalID = setInterval(() => {
      this.setState({ date: new Date() });
    }, oneSecond);
  }

๐Ÿ˜ componentDidUpdate

componentDidUpdate๋Š” ์ตœ์ดˆ rendering์‹œ์—๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋‹ค๊ฐ€ ์ตœ์ดˆ ์ดํ›„ state ํ˜น์€ props์˜ update๊ฐ€ ์ผ์–ด๋‚˜๋ฉด ํ˜ธ์ถœ๋œ๋‹ค. ํ•ด๋‹น method๋Š” ์ด์ „ prevProps์™€ currProps๋ฅผ ๋น„๊ตํ•˜์—ฌ DOM์„ ์กฐ์ž‘ํ•  ๋•Œ ํ™œ์šฉํ•˜๋ฉด ์ข‹๋‹ค. ๋‹จ, setState()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๊ฐ์‹ธ์ง€ ์•Š์œผ๋ฉด ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์งˆ ์ˆ˜ ์žˆ๋‹ค.

profile
Turtle Never stop

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