๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Development/React.js

[React] React Life Cycle ; ๋ฆฌ์•กํŠธ ์ƒ๋ช…์ฃผ๊ธฐ

by DevIseo 2023. 1. 14.

React๋ž€?

javascript ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

React ํŒŒ์ผ๋“ค์˜ ์—ญํ• 

index.js

  • src ํด๋”์— ํฌํ•จ
  • ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ
  • HTML ํ…œํ”Œ๋ฆฟ ๋ฐ JavaScript์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์กฐํ•ฉํ•˜๊ณ  ๋ Œ๋”๋งํ•ด ์‹ค์ œ ํ‘œ์‹œ

App.js

  • src ํด๋”์— ํฌํ•จ
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ
  • ์‹ค์ œ๋กœ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๋‚ด์šฉ๋“ฑ์ด ์ •์˜

index.html

  • public ํด๋”์— ์žˆ์Œ
  • ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์ธ index.js์— ๋Œ€์‘๋˜๋Š” ๊ฒƒ
  • HTML ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ
  • ์ง์ ‘ ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ , index.js์— ์˜ํ•ด ์ผ์–ด๋‚˜ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๊ฐ€ ํ‘œ์‹œ๋จ

React Life Cycle

์ถœ์ฒ˜ : https://adjh54.tistory.com/42

React lifecycle ์šฉ์–ด

์šฉ์–ด ์„ค๋ช…

~ will ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ด€๋ จ๋œ ์šฉ์–ด
~ did ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ์ดํ›„์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ด€๋ จ๋œ ์šฉ์–ด
mount ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ DOM์ด ์ƒ์„ฑ๋˜๊ณ  ์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ์— ๋‚˜ํƒ€๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ด€๋ จ๋œ ์šฉ์–ด
unmount ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ DOM์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ด€๋ จ๋œ ์šฉ์–ด
update ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„๋•Œ ์ˆ˜ํ–‰ํ•˜๋Š”๊ฒƒ์„ ์˜๋ฏธ
ex) props, state, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฆฌ ๋ Œ๋”๋ง,  
forceUpdate๋ฅผ ํ†ตํ•ด ๊ฐ•์ œ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ  

๐Ÿ’ก ์ƒ๋ช…์ฃผ๊ธฐ ์ˆœ์„œ

  1. ์ปดํฌ๋„ŒํŠธ์˜ ์‹คํ–‰
  2. constructor() ์‹คํ–‰
  3. render() ์‹คํ–‰
  4. ComponentDidMount() ์‹คํ–‰

์กฐ๊ฑด๋ถ€ ์ˆ˜ํ–‰

  • ์ปดํฌ๋„ŒํŠธ ๋‚ด์— ๋ณ€ํ™”๊ฐ€ ์žˆ์„ ๋•Œ : componentDidUpdate() ์‹คํ–‰
  • ์ปดํฌ๋„ŒํŠธ์˜ ์†Œ๋ฉธ์ด ์ˆ˜ํ–‰๋˜์—ˆ์„ ๋•Œ : componentWillUnmount() ์‹คํ–‰

 

1. Mounting - constructor()

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒˆ๋กœ ์ƒ์„ฑ์ด ๋˜๊ณ  ๋ Œ๋”๋ง(DOM ์ƒ์„ฑ) ์ด์ „์— ์ˆ˜ํ–‰์ด ๋˜๋Š” ํด๋ž˜์Šค ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์˜๋ฏธ

 

  • ์ƒ์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•  ๋•Œ ์‚ฌ์šฉ
  • state ๊ณต๊ฐ„์— ๋ณ€์ˆ˜ ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์‚ฌ์šฉ

2. Mounting - render()

 ๐Ÿ’ก ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ

 

  • ํ•ด๋‹น ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ๋ฐ›์€ props ๊ฐ’์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
  • constructor()์—์„œ ์ •์˜ํ•œ state ๊ฐ’์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
  • ํ•ด๋‹น ๊ณต๊ฐ„์—์„œ๋Š” setState() ์‚ฌ์šฉ ๋ถˆ๊ฐ€

3. Mounting - ComponentDidMount()

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ

 

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ด๋ฒคํŠธ ๋“ฑ๋ก์— ์‚ฌ์šฉ
  • setTimeout, setInterval, ๋„คํŠธ์›Œํฌ ์š”์ฒญ ๊ฐ™์€ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ

4. Updating - componentDidUpdate()

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒฝ์šฐ์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ

 

  • ๋ณ€ํ™”๋ผ ํ•จ์€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ props ๊ฐ’์˜ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒ์„ ํ•˜๋Š” ๊ฒฝ์šฐ ์ˆ˜ํ–‰
  • ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ state์˜ ๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๊ฒฝ์šฐ ์ˆ˜ํ–‰
  • forceUpdate() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ•์ œ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ์— ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰

5. Unmounting - componentWillUnmount()

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ DOM์„ ์ œ๊ฑฐํ•  ๋•Œ์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ

 

  • A, B๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ์— ํŠน์ • ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ์„œ A๋Š” ๋งˆ์šดํŠธํ•˜๊ณ , B๋Š” ์›๋งˆ์šดํŠธ๋ฅผ ํ•˜๊ณ ์ž ํ•  ๋•Œ์— ์ด๋ฅผ ์‚ฌ์šฉ
  • ์˜ˆ์‹œ
    • A๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ B๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ€๋ฅด๊ณ  ์žˆ์„ ์ „์ œ๋ผ๊ณ  ํ•  ๋•Œ, B ์ปดํฌ๋„ŒํŠธ๋ฅผ ์กฐ๊ฑด๋ถ€์— ๋”ฐ๋ผ์„œ ์†Œ๋ฉธ์„ ์‹œํ‚ฌ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰.

 

 

functional component State & Lifecycle

https://adjh54.tistory.com/43

๋ถ„๋ฅ˜ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

Mounting constructor() ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€
Mounting render() return()
Mounting ComponenDidMount() useEffect()
Updating componentDidUpdate() useEffect()
UnMounting componentWillUnmount() useEffect()

 ๐Ÿ’ก [์ƒ๋ช…์ฃผ๊ธฐ ์ˆœ์„œ ์š”์•ฝ]

  1. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ
  2. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ ์‹คํ–‰
  3. return()์œผ๋กœ ํ™”๋ฉด ๋žœ๋”๋ง
  4. useEffect() ์‹คํ–‰

[์กฐ๊ฑด๋ถ€ ์ˆ˜ํ–‰] ๋ณ€๊ฒฝ ํ˜น์€ ์ปดํฌ๋„ŒํŠธ์˜ ์†Œ๋ฉธ์ด ๋ฐœ์ƒ์‹œ useEffect() ์‹คํ–‰

 

1. Mounting - ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์„ ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ ๋˜๋Š” ๊ฒƒ State๋ฅผ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ๋  ํ•จ์ˆ˜๋“ค์— ๋Œ€ํ•ด ๋ฏธ๋ฆฌ ์ •์˜

 

import { useState } from "react";

function LifecycleFunctionComponent(props){
    **/**
   * ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ์ด ๋˜๋Š” ๊ณต๊ฐ„
   * ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๋  state๋‚˜ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
   */**
  console.log('ํ•ด๋‹น ๋ถ€๋ถ„์ด ์ œ์ผ ๋จผ์ € ํ˜ธ์ถœ')
  const [userInfo,setUserInfo] = useState({
    userId:'yuiseo',
    userAge:28,
    isShowTempComponent:false
  })
  return(
    <>
    </>
  )
}
export default LifecycleFunctionComponent;

2. Mounting - return()

๐Ÿ’ก ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ

 

  • ํ•ด๋‹น ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌ๋ฐ›์€ props ๊ฐ’์ด ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • constructor()์—์„œ ์ •์˜ํ•œ state ๊ฐ’์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
  • ํ•ด๋‹น ๊ณต๊ฐ„์—์„œ setState() ์‚ฌ์šฉ ๋ถˆ๊ฐ€
import { useState } from "react";

function LifecycleFunctionComponent(props){
    /**
   * ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ์ด ๋˜๋Š” ๊ณต๊ฐ„
   * ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๋  state๋‚˜ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
   */
  console.log('ํ•ด๋‹น ๋ถ€๋ถ„์ด ์ œ์ผ ๋จผ์ € ํ˜ธ์ถœ')
  const [userInfo,setUserInfo] = useState({
    userId:'yuiseo',
    userAge:28,
    isShowTempComponent:false
  })

   **/**
   * ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */**
  return(
    <>
    {console.log('rendering...')}
    <div>
      <div>Main Function Component</div>
      <div>{userInfo.userAge}</div>
    </div>
    </>
  )
}
export default LifecycleFunctionComponent;

3. Mounting/Updating/Unmounting - useEffect()

 ๐Ÿ’ก useEffect()๋ฅผ ํ†ตํ•ด Mounting/Updating/Unmounting ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋„ ๋ฌด๋ฐฉ

 

 ๐Ÿ’ก [ ๋ฉ”์„œ๋“œ ๊ตฌ์กฐ ]

useEffect( function, deps? );

  • function : useEffect๊ฐ€ ์ˆ˜ํ–‰๋  ๋•Œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜
  • deps [optional] : ๋ฐฐ์—ด ํ˜•ํƒœ์ด๋ฉฐ, ์˜์กด(dependency) ๊ฐ’์„ ์˜๋ฏธ </aside>

deps [optinal] ๊ฐ’์˜ ์˜๋ฏธ

deps์˜ ๊ฐ’ ๊ตฌ์กฐ ์„ค๋ช…

๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ useEffect(()⇒{}) ํ™”๋ฉด์ด ๋ Œ๋”๋ง ๋œ ์ดํ›„ ์ˆ˜ํ–‰์ด ๋˜๋ฉฐ, ๋ฆฌ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์‹œ ์ˆ˜ํ–‰์ด ๋œ๋‹ค
๋นˆ ๋ฐฐ์—ด ์ธ๊ฒฝ์šฐ useEffect(()⇒{}, []) ํ™”๋ฉด์ด ๋ Œ๋”๋ง ๋œ ์ดํ›„์—๋งŒ ์ˆ˜ํ–‰์ด ๋œ๋‹ค.
๋ฐฐ์—ด ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ useEffect(()⇒{}, [๊ฐ’]) ํ™”๋ฉด์ด ๋ Œ๋”๋ง ๋œ ์ดํ›„์— ์ˆ˜ํ–‰์ด ๋˜๊ณ , ‘๊ฐ’’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰์ด ๋œ๋‹ค.

3.1 Mounting - useEffect()

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ deps ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋นˆ ๋ฐฐ์—ด๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„ ์ตœ์ดˆ ํ•œ๋ฒˆ ์ˆ˜ํ–‰

 

import { useState,useEffect  } from "react";

function LifecycleFunctionComponent(props){
    /**
   * 1. ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ์ด ๋˜๋Š” ๊ณต๊ฐ„
   * ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๋  state๋‚˜ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
   */
  console.log('ํ•ด๋‹น ๋ถ€๋ถ„์ด ์ œ์ผ ๋จผ์ € ํ˜ธ์ถœ')
  const [userInfo,setUserInfo] = useState({
    userId:'yuiseo',
    userAge:28,
    isShowTempComponent:false
  })
  
    **/**
   * 3. ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  useEffect(()=>{
    console.log('ํ™”๋ฉด์ด ๋žœ๋”๋ง ๋œ ์ดํ›„์— ๋ฐ”๋กœ ์ˆ˜ํ–‰ : componentDidMoutn()์™€ ๋™์ผ')
  },[])**

   /**
   * 2. ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  return(
    <>
    {console.log('rendering...')}
    <div>
      <div>Main Function Component</div>
      <div>{userInfo.userAge}</div>
    </div>
    </>
  )
}
export default LifecycleFunctionComponent;

3.2 Updating - useEffect()

 ๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒฝ์šฐ์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ

 

  • ๋ณ€ํ™”
    • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ props๊ฐ’์˜ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒ
    • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ
  • ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ state์˜ ๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๊ฒฝ์šฐ ์ˆ˜ํ–‰
import { useState,useEffect } from "react";

function LifecycleFunctionComponent(props){
    /**
   * 1. ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ์ด ๋˜๋Š” ๊ณต๊ฐ„
   * ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๋  state๋‚˜ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
   */
  console.log('ํ•ด๋‹น ๋ถ€๋ถ„์ด ์ œ์ผ ๋จผ์ € ํ˜ธ์ถœ')
  const [userInfo,setUserInfo] = useState({
    userId:'yuiseo',
    userAge:28,
    isShowTempComponent:false
  })
  
    **/**
   * 3. ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  useEffect(()=>{
    console.log('์ „๋‹ฌ ๋ฐ›์€ props์˜ ๊ฐ’์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ / ์‚ฌ์šฉ์ž ๋‚˜์ด์˜ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒฝ์šฐ')
  },[props.appState,userInfo.userAge])**

   /**
   * 2. ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  return(
    <>
    {console.log('rendering...')}
    <div>
      <div>Main Function Component</div>
      <div>{userInfo.userAge}</div>
    </div>
    </>
  )
}

3.3 Unmonting - useEffect()

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ DOM์„ ์ œ๊ฑฐํ•  ๋•Œ์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ

 

  • ์ปดํฌ๋„ŒํŠธ์˜ DOM์ด ์ œ๊ฑฐ๋  ๋•Œ์— ์ˆ˜ํ–‰
  • ์˜ˆ์‹œ
    • A๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ B๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ€๋ฅด๊ณ  ์žˆ์„ ์ „์ œ๋ผ๊ณ  ํ•  ๋•Œ, B ์ปดํฌ๋„ŒํŠธ๋ฅผ ์กฐ๊ฑด๋ถ€์— ๋”ฐ๋ผ์„œ ์†Œ๋ฉธ์„ ์‹œํ‚ฌ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰
import { useState,useEffect } from "react";

function LifecycleFunctionComponent(props){
    /**
   * 1. ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ์ด ๋˜๋Š” ๊ณต๊ฐ„
   * ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๋  state๋‚˜ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
   */
  console.log('ํ•ด๋‹น ๋ถ€๋ถ„์ด ์ œ์ผ ๋จผ์ € ํ˜ธ์ถœ')

  //unMount
  const [isShowTempComp,setIsShowTempComp]=useState(true)

  const [userInfo,setUserInfo] = useState({
    userId:'yuiseo',
    userAge:28,
    isShowTempComponent:false
  })
  
    /**
   * 3. ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  // useEffect(()=>{
  //   console.log('์ „๋‹ฌ ๋ฐ›์€ props์˜ ๊ฐ’์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ / ์‚ฌ์šฉ์ž ๋‚˜์ด์˜ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒฝ์šฐ')
  // },[props.appState,userInfo.userAge])

  /**
 * ์ž„์‹œ ์ปดํฌ๋„ŒํŠธ Unmount
 */
  const fn_unmountCompoent = () => {
    setIsShowTempComp(!isShowTempComp);
  }

   /**
   * 2. ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  return(
    <>
    {console.log('rendering...')}
    <div>
      <div>Main Function Component</div>
      <button onClick={fn_unmountCompoent}>delete Component</button>
      {
        isShowTempComp && (
          <LifecycleUnmountComponent/>
        )
      }
    </div>
    </>
  )
}

function LifecycleUnmountComponent(){
  useEffect(()=>{
    return(
      console.log('LifecycleUnmountComponent๊ฐ€ Unmount ๋˜์—ˆ์Œ')
    )
  },[])

  return(
    <div>
      <h1>Unmount Component</h1>
    </div>
  )
}

//MAIN FUNCTION
function App() {
  return (
    <div className="App">
      <h1> hola, yo soy iseo</h1>
      <LifecycleFunctionComponent/>
    </div>
  );
}

export default App;
import { useState,useEffect } from "react";

function LifecycleFunctionComponent(props){
    /**
   * 1. ์ปดํฌ๋„ŒํŠธ ํ˜ธ์ถœ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ์ด ๋˜๋Š” ๊ณต๊ฐ„
   * ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๋  state๋‚˜ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
   */
  console.log('ํ•ด๋‹น ๋ถ€๋ถ„์ด ์ œ์ผ ๋จผ์ € ํ˜ธ์ถœ')

  //unMount
  const [isShowTempComp,setIsShowTempComp]=useState(true)

  const [userInfo,setUserInfo] = useState({
    userId:'yuiseo',
    userAge:28,
    isShowTempComponent:false
  })
  
    /**
   * 3. ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋œ ์ดํ›„์— ์‹คํ–‰์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  // useEffect(()=>{
  //   console.log('์ „๋‹ฌ ๋ฐ›์€ props์˜ ๊ฐ’์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ / ์‚ฌ์šฉ์ž ๋‚˜์ด์˜ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒฝ์šฐ')
  // },[props.appState,userInfo.userAge])

  /**
 * ์ž„์‹œ ์ปดํฌ๋„ŒํŠธ Unmount
 */
  const fn_unmountCompoent = () => {
    setIsShowTempComp(!isShowTempComp);
  }

   /**
   * 2. ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ HTML๋ฅผ ํ™”๋ฉด์ƒ์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
   */
  return(
    <>
    {console.log('rendering...')}
    <div>
      <div>Main Function Component</div>
      <button onClick={fn_unmountCompoent}>delete Component</button>
      {
        isShowTempComp && (
          <LifecycleUnmountComponent/>
        )
      }
    </div>
    </>
  )
}

function LifecycleUnmountComponent(){
  useEffect(()=>{
    return(
      console.log('LifecycleUnmountComponent๊ฐ€ Unmount ๋˜์—ˆ์Œ')
    )
  },[])

  return(
    <div>
      <h1>Unmount Component</h1>
    </div>
  )
}

//MAIN FUNCTION
function App() {
  return (
    <div className="App">
      <h1> hola, yo soy iseo</h1>
      <LifecycleFunctionComponent/>
    </div>
  );
}

export default App;

๋Œ“๊ธ€