[React] React Lifecycle

yayaya_hojinยท2023๋…„ 12์›” 19์ผ
0

๋ฆฌ์—‘ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
6/6
post-thumbnail
post-custom-banner

React Lifecycle

React ์ปดํฌ๋„ŒํŠธ๋Š” Mount(์ƒ์„ฑ) -> Update(์—…๋ฐ์ดํŠธ) -> Unmount(์ œ๊ฑฐ)์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

react_life_cycle

1. Mount(์ƒ์„ฑ)

์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ฒ˜์Œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ Mount๋กœ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ์„ฑ์„ ๋งํ•œ๋‹ค.

1) Constructor - ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋กœ ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰์ด ๋˜๊ณ  this.props, this.state๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

2) getDerivedStateFromProps - props๋กœ ๋ฐ›์•„์˜จ ๊ฐ’์„ state์— ๋„ฃ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ

3) render - ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง ๋ฉ”์„œ๋“œ

4) componentDidMount - ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ์ด ์™„๋ฃŒ๋จ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ฒซ๋ฒˆ์งธ ๋ Œ๋”๋ง์„ ๋งˆ์น˜๋ฉด ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ

2. Update(์—…๋ฐ์ดํŠธ)

์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

1) getDerivedStateFromProps - ์ปดํฌ๋„ŒํŠธ์˜ props๋‚˜ state๊ฐ€ ๋ณ€๊ฒฝ ๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฉ”์„œ๋“œ

2) shouldComponentUpdate - ์ปดํฌ๋„ŒํŠธ์˜ ์žฌ๋ Œ๋”๋ง ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ

3) getSnapshotBeforeUpdate - ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๊ฐ€ DOM์— ๋ฐ˜์˜ํ•˜๊ธฐ ์ „ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ

ex) ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค ์œ„์น˜๋ฅผ ์ €์žฅํ•ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜์–ด๋„ ์ด์ „์— ์Šคํฌ๋กค ์œ„์น˜๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์œ ์ง€ํ•œ๋‹ค.

4) componentDidUpdate - ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋œ ํ›„ ๋ฐœ์ƒํ•˜๋Š” ๋ฉ”์„œ๋“œ

3. Unmount(์ œ๊ฑฐ)

์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

1) componentWillUnmount - ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์›น์—์„œ ์‚ฌ๋ผ์ง€๊ธฐ ์ „์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ

post-custom-banner

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