2024-03-08.md

๐Ÿก

DIL: ๋ชจ๋˜ ๋ฆฌ์•กํŠธ ๋”ฅ ๋‹ค์ด๋ธŒ, 1์ฃผ์ฐจ-5

์Šคํ„ฐ๋””: ์›”๊ฐ„ CS, https://github.com/monthly-cs/2024-03-modern-react-deep-dive
์˜ค๋Š˜ ์ง„ํ–‰: ๊ฐœ์ธ๊ณต๋ถ€


DIL-week1-5_2024-03-08

| DIL ์ฃผ์ฐจ | ๋ฒ”์œ„ | ๋‚ด์šฉ | ์˜ค๋Š˜์ฐจ ์ง„๋„ | | -------- | ------ | ------------------------------ | ----------- | | 1์ฃผ์ฐจ | 1, 2์žฅ | ๋ฆฌ์•กํŠธ ํ•ต์‹ฌ์š”์†Œ์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ | 144p~159p |

์˜ค๋Š˜ ์ฝ์€ ๋‚ด์šฉ์„ markdown์œผ๋กœ ๊ฐ„๋‹จํžˆ ๋ฉ”๋ชจ
์ฝ์€ ์‹œ๊ฐ„: 9์‹œ~12์‹œ, ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ์˜ˆ์ œ ๋งŒ๋“ค์–ด๋ด„๐Ÿค”

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ

class MyComponent extends React.Component: <MyProps, MyState> {
  constructor(props: MyProps) {
    super(props)
    this.state = {
      count: 0
    }
  }
  ...
}

public render() {
  const {
    props: { required, text },
    state: { count, isLimited },
  } = this;

  return (
    <div>{count}</div>
  )
}
  • class ์ปดํฌ๋„ŒํŠธ์˜ constructor๋Š”
    • state์„ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค
    • props๋ฅผ super์— ์ „๋‹ฌํ•œ๋‹ค
  • constructor ์—†์ด state์„ ์ดˆ๊ธฐํ™”?
    • ES2022์˜ class field
    • @babel/plugin-proposal-class-properties๋กœ ํŠธ๋žœ์ŠคํŒŒ์ผ ํ•ด์•ผํ•จ

์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ

  • React 17 ์ด์ „์˜ unsafe ๋ฉ”์„œ๋“œ?

    • UNSAFE_componentWillMount()
    • UNSAFE_componentWillReceiveProps()
    • UNSAFE_componentWillUpdate()
  • Life Cycle Method ์‹คํ–‰๋˜๋Š” ์‹œ์ 

    1. mount
    2. update
    3. unmount
  • render()

    • mount, update
    • ์ˆœ์ˆ˜ ํ•จ์ˆ˜ no side-effects
  • componentDidMount()

    • ๋งˆ์šดํŠธ ํ›„ > this.setState ํ˜ธ์ถœ ์‹œ? ์ฆ‰์‹œ ๋‹ค์‹œ ๋žœ๋”๋ง >> ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์‹ค์ œ๋กœ UI๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์ „์— ์‹คํ–‰
    • ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ > ๊ผญ ํ•„์š”ํ•œ ์ž‘์—…๋งŒ ex) API ํ˜ธ์ถœ ํ›„ ์—…๋ฐ์ดํŠธ, DOM ์˜์กด
  • componentDidUpdate()

    • ์กฐ๊ฑด๋ฌธ์œผ๋กœ ํƒˆ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด? this.setState ๊ณ„์† ํ˜ธ์ถœ
  • componentWillUnmount()

    • ํด๋ฆฐ์—…, this.setState ํ˜ธ์ถœx
  • shouldComponentUpdate()

    • state์ด๋‚˜ props์˜ ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•ด ๋ฐœ์ƒํ•˜๋Š” ๋ฆฌ๋žœ๋”๋ง์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค
  • getDerivedStateFromError()

    • ์—๋Ÿฌ ๋ฐ”์šด๋”๋ฆฌ ๋งŒ๋“ฆ
    • ๋žœ๋”๋ง ๊ณผ์ •์—์„œ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ, side-effect ์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค. (error state ๋ฐ˜ํ™˜ ์™ธ์˜ ๋ชจ๋“  ๊ฒƒ, ๋กœ๊น…๋„ ์•ˆ๋จ)
  • componentDidCatch

    • ์ปค๋ฐ‹ ๋‹จ๊ฒŒ์—์„œ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ, side-effect๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค
    • ์—๋Ÿฌ ๋กœ๊น…
  • ๐Ÿ‘‰ Class Component ๋ฉ”์†Œ๋“œ ์‹คํ—˜