๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์™€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ Lifecycle์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  ๊ด€๋ จ๋œ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ• ์•Œ๊ธฐ!! ์‹œ๊ธฐ ์ ์ ˆํ–ˆ๋˜ ์„ธ์…˜๐Ÿ‘ ๋ฐ”๋กœ ์ ์šฉํ–ˆ๋˜ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง!!


Lifecycle์ด๋ž€?

React ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ state์™€ ์ƒ๋ช…์ฃผ๊ธฐ



โ—‹ Lifecycle ๊ธฐ๋ณธ ์ˆœ์„œ

  1. constructor
  2. render
  3. componentDidMount
  4. (fetch ์™„๋ฃŒ)
  5. render
  6. (setState)
  7. componentDidUpdate (setState ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ ๋ฐœ์ƒ)
  8. componentWillUnmount



โ—‹ ๋ถ€๋ชจ - ์ž์‹ Lifecycle

  • ๋ถ€๋ชจ API์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž์‹์—๊ฒŒ props๋กœ ์ „๋‹ฌํ•˜์—ฌ ์ž์‹ ๋‚ด๋ถ€์—์„œ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ



AND์—ฐ์‚ฐ์ž(&&) ์‚ฌ์šฉํ•œ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง!

Cannot read property โ€˜mapโ€™ of undefined ๐Ÿคฌ๐Ÿคฌ๐Ÿคฌ๐Ÿคฌ๐Ÿคฌ

Mockup ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ importํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹œ์ž‘๋˜๊ณ  ๋ฐฑ์—”๋“œ API๋ฅผ ๋ถ™์ด๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ ์ค‘ ํ•˜๋‚˜. ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š”๋ฐ map์„ ๋Œ๋ ธ์œผ๋‹ˆ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ๋ฐ–์—โ€ฆ. ๐Ÿ˜ญ ์ƒํƒœ๊ฐ’์˜ ์‹œ์ ์— ๊ด€ํ•œ ๋ฌธ์ œ๋ผ๋ฉด React์˜ Lifecycle๊ณผ ์—ฐ๊ด€์ด ์žˆ๊ณ , ์ด ๋•Œ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ์ฃผ์–ด ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

์•„๋ž˜๋Š” 1์ฐจ ํ”„๋กœ์ ํŠธ์—์„œ ์‹ค์ œ ์ ์šฉํ•œ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง ์ฝ”๋“œ!

1์ฐจ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์ข…์ข… ๋ฐœ์ƒํ–ˆ๋˜ ์—๋Ÿฌ ์ค‘ ํ•˜๋‚˜..๐Ÿ˜‚



โ—‹ AND์—ฐ์‚ฐ์ž(&&) ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ํ•  ์ !

  • ๋ณ€์ˆ˜๊ฐ€ ์ˆซ์ž ํƒ€์ž…์ธ ๊ฒฝ์šฐ 0์€ false ์ด๋‹ค.
  • ๋ณ€์ˆ˜๊ฐ€ ๋ฌธ์ž์—ด ํƒ€์ž…์ธ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ดโ€œโ€๋„ false์ด๋‹ค.



์ฐธ๊ณ  ์ž๋ฃŒ

  • [React Lifecycle](https://velog.io/@joonsikyang/React-Project-Lifecycle)
  • [๊ณต์‹๋ฌธ์„œ State and Lifecycle](https://ko.reactjs.org/docs/state-and-lifecycle.html)