Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

REACT ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช… ์ฃผ๊ธฐ (Life Cycle) ๋ณธ๋ฌธ

React

REACT ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช… ์ฃผ๊ธฐ (Life Cycle)

kohi โ˜• 2022. 12. 10. 00:54

๐Ÿ‘ฝ ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช… ์ฃผ๊ธฐ (Life Cycle)


๋ฆฌ์•กํŠธ์˜ ์ปดํฌ๋„ŒํŠธ๋Š” ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. ์ƒ๋ช… ์ฃผ๊ธฐ๋ž€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์‚ฌ์šฉ๋˜๊ณ , ์†Œ๋ฉธ๋˜๋Š” ์ผ๋ จ์˜ ๊ณผ์ •์„ ๋œปํ•˜๊ณ , ์ƒ๋ช… ์ฃผ๊ธฐ ์•ˆ์—์„œ๋Š” ํŠน์ • ์‹œ์ ์— ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ๋ผ์ดํ”„ ์‚ฌ์ดํด ์ด๋ฒคํŠธ๋ผ๊ณ  ํ•œ๋‹ค.

์–ด๋–ค ๋ผ์ดํ”„ ์‚ฌ์ดํด ์ด๋ฒคํŠธ๋Š” ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๊ณ , ์–ด๋–ค ์ด๋ฒคํŠธ๋Š” ๊ณ„์†ํ•ด์„œ ์‹คํ–‰๋˜๊ธฐ๋„ ํ•œ๋‹ค. ๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ‘ฝ ๋ผ์ดํ”„ ์‚ฌ์ดํด ์ด๋ฒคํŠธ์˜ ๋ถ„๋ฅ˜


 

๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ์˜ ์ข…๋ฅ˜๋Š” ์ด 9๊ฐ€์ง€์ด๋‹ค.

  • will ์ ‘๋‘์‚ฌ๊ฐ€ ๋ถ™์€ ๋ฉ”์„œ๋“œ: ์–ด๋–ค ์ž‘์—…์„ ์ž‘๋™ํ•˜๊ธฐ ์ „ ์‹คํ–‰
  • did ์ ‘๋‘์‚ฌ๊ฐ€ ๋ถ™์€ ๋ฉ”์„œ๋“œ: ์–ด๋–ค ์ž‘์—…์„ ์ž‘๋™ํ•œ ํ›„์— ์‹คํ–‰

 

๋ผ์ดํ”„ ์‚ฌ์ดํด์€ 3๊ฐ€์ง€ Mount, Update, Unmount ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋‚˜๋ˆˆ๋‹ค. ๊ฐ ๋ถ„๋ฅ˜์— ๋”ฐ๋ผ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ๋˜๋Š” ํšŸ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋‹ค.

 

Mount

 

DOM์ด ์ƒ์„ฑ๋˜๊ณ  ์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ์— ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ Mount๋ผ๊ณ  ํ•œ๋‹ค. ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋œ๋‹ค.

  • constructor: ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํด๋ž˜์Šค ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ
  • getDerivedStateFromProps: props์— ์žˆ๋Š” ๊ฐ’์„ state์— ๋„ฃ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • componentWillMount(): DOM์— ์‚ฝ์ž…ํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋œ๋‹ค. ๋งˆ์šดํŒ… ์ค‘์ด๋ฏ€๋กœ props๋‚˜ state์— ์ ‘๊ทผํ•  ์ˆ˜๋Š” ์žˆ์œผ๋‚˜ ์ด ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋˜ํ•œ ์•„์ง render() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰ ์ „์ด๊ธฐ ๋•Œ๋ฌธ์— DOM์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.
  • render: ์ค€๋น„ํ•œ UI ๋ Œ๋”๋ง
  • componentDidMount(): DOM์— ์‚ฝ์ž…๋˜์–ด ๋ Œ๋”๋ง์ด ์™„๋ฃŒ๋œ ํ›„ ์‹คํ–‰๋œ๋‹ค. DOM์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ ์ด ๋‹จ๊ณ„์—์„œ AJAX ์š”์ฒญ์„ ํ•˜๊ฑฐ๋‚˜ setTimeout, setInterval ๊ฐ™์€ ์ž‘์—…์„ ์‹คํ–‰ํ•œ๋‹ค.

 

Update

์†์„ฑ์ด๋‚˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.

  • componentWillReceiveProps(nextProps): ์ปดํฌ๋„ŒํŠธ๊ฐ€ props๋ฅผ ๋ฐ›๊ธฐ ์ง์ „ ์‹คํ–‰๋œ๋‹ค. ๋ฐ”๋€” props์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ–๋Š”๋‹ค.
  • shouldComponentUpdate(nextProps, nextState): ์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง์„ ํ•ด์•ผํ• ์ง€ ๋ง์•„์•ผํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋‹ค์Œ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ„์† ์‹คํ–‰ํ•˜๊ณ , false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ž‘์—…์„ ์ค‘์ง€(๋ฆฌ๋ Œ๋”๋ง X)ํ•œ๋‹ค.
  • componentWillUpdate(nextProps, nextState): ์ปดํฌ๋„ŒํŠธ ๋ณ€ํ™”๋ฅผ DOM์— ๋ฐ˜์˜ํ•˜๊ธฐ ๋ฐ”๋กœ ์ง์ „์— ํ˜ธ์ถœํ•œ๋‹ค. ์ด ๋‹จ๊ณ„์—์„œ state๋ฅผ ๋ณ€๊ฒฝํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. (๋ฌดํ•œ ๋ฃจํ”„ ๋ฐœ์ƒ)
  • componentDidUpdate: ์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ ์ž‘์—…์ด ๋‹ค ๋๋‚œ ํ›„ ํ˜ธ์ถœํ•œ๋‹ค. ์ด ๋‹จ๊ณ„์—์„œ๋Š” render๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ DOM์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Unmount

 

React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ DOM์—์„œ ์ œ๊ฑฐํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋ฉฐ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋œ๋‹ค.

  • componentWillUnmount(): ์ปดํฌ๋„ŒํŠธ๋ฅผ DOM์—์„œ ์ œ๊ฑฐํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋ฉฐ, ์ฃผ๋กœ ์—ฐ๊ฒฐํ–ˆ๋˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋“ฑ์˜ ์—ฌ๋Ÿฌ ์ •๋ฆฌ ์ž‘์—…์„ ํ•œ๋‹ค.

 

 

๐Ÿ‘ฝ ์ƒ๋ช… ์ฃผ๊ธฐ ์ด๋ฒคํŠธ ์˜ˆ์ œ ์ฝ”๋“œ


const root = ReactDOM.createRoot(document.getElementById('root'));
  
function tick() {
  const element = (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {new Date().toLocaleTimeString()}.</h2>
    </div>
  );
  root.render(element);
}

setInterval(tick, 1000);

 

tick ์ปดํฌ๋„ŒํŠธ๋Š” ์‹œ๊ฐ„์ด ๋ณ€ํ•˜๋Š” ๊ฒƒ์„ 1000ms๋งˆ๋‹ค ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‹ค์Œ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์บก์Šํ™”
  • ์ปดํฌ๋„ŒํŠธ ์Šค์Šค๋กœ ํƒ€์ด๋จธ ์„ค์ •ํ•˜๊ณ  ์—…๋ฐ์ดํŠธ

 

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }
  
    componentWillUnmount() {
    clearInterval(this.timerID);
  }

 

tick()์ด ์ฒ˜์Œ DOM์— ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๊ณ , ๋˜ tick()์— ์˜ํ•ด ์ƒ์„ฑ๋œ DOM์ด ์‚ญ์ œ๋  ๋•Œ๋งˆ๋‹ค ํƒ€์ด๋จธ๋ฅผ ํ•ด์ œํ•˜๊ธฐ ์œ„ํ•ด componentDidMount() ๋ฉ”์„œ๋“œ์™€ componentWillUnmount() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Clock />);

 

 

๐Ÿ‘ฝ Reference


https://velog.io/@gyumin_2/React.js-%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%83%9D%EB%AA%85%EC%A3%BC%EA%B8%B0Life-Cycle

 

React.js - ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช…์ฃผ๊ธฐ(Life Cycle)

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช…์ฃผ๊ธฐ(Life Cycle) ๋ฆฌ์•กํŠธ์˜ ์ปดํฌ๋„ŒํŠธ๋Š” ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ƒ๋ช…์ฃผ๊ธฐ๋ž€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์‚ฌ์šฉ๋˜๊ณ  ์†Œ๋ฉธ๋  ๋•Œ ๊นŒ์ง€ ์ผ๋ จ์˜ ๊ณผ์ •์„ ๋งํ•œ๋‹ค. ์ƒ๋ช…์ฃผ๊ธฐ ์•ˆ์—์„œ๋Š” ํŠน์ • ์‹œ์ ์—

velog.io

https://ko.reactjs.org/docs/state-and-lifecycle.html

 

State and Lifecycle – React

A JavaScript library for building user interfaces

ko.reactjs.org

https://velog.io/@youngminss/React-%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%83%9D%EB%AA%85%EC%A3%BC%EA%B8%B0-%EB%A9%94%EC%84%9C%EB%93%9C

 

[React] ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—๋Š” ๋ผ์ดํ”„์‚ฌ์ดํด(์ˆ˜๋ช… ์ฃผ๊ธฐ)์ด ์กด์žฌํ•œ๋‹ค.์ปดํฌ๋„ŒํŠธ์˜ ์ˆ˜๋ช…์€ ํŽ˜์ด์ง€์— ๋ Œ๋”๋ง๋˜๊ธฐ ์ „์ธ ์ค€๋น„๊ณผ์ •์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํŽ˜์ด์ง€์—์„œ ์‚ฌ๋ผ์งˆ ๋•Œ๋๋‚œ๋‹ค.์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฒ˜์Œ์œผ๋กœ ๋ Œ๋”๋ง ํ• 

velog.io

 

'React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Ref์˜ ํ™œ์šฉ๊ณผ useRef  (0) 2023.01.02
[React] props์™€ state  (0) 2022.12.15
React :: COIN TRACKER  (0) 2022.11.29
React๋กœ ๊ฐ„๋‹จํ•œ To-Do-List ๊ตฌํ˜„  (0) 2022.11.24
React useState(), useEffect()  (0) 2022.11.15