๐Ÿ“•CS

React ์ปดํฌ๋„ŒํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด

stonesy 2024. 4. 19. 00:04
728x90

๐Ÿ“–๋ผ์ดํ”„์‚ฌ์ดํด

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

 

๐Ÿ”ŽMount >  Update, Re-render > Unmount

โœ๏ธMount

DOM์ด ์ƒ์„ฑ๋˜๊ณ  ์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ(ํ™”๋ฉด)์— ๋‚˜ํƒ€๋‚จ. Mount ์‹œ์—๋Š” ๋‹ค์Œ์˜ ๋ฉ”์„œ๋“œ๋“ค์ด ์ฐจ๋ก€๋Œ€๋กœ ํ˜ธ์ถœ๋œ๋‹ค.

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

โœ๏ธUpdate, Re-render

์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋˜๋Š” ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • props๊ฐ€ ๋ฐ”๋€” ๋•Œ
  • state๊ฐ€ ๋ฐ”๋€” ๋•Œ
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋  ๋•Œ
  • this.forceUpdate๋กœ ๊ฐ•์ œ๋กœ ๋ Œ๋”๋ง์„ ํŠธ๋ฆฌ๊ฑฐํ•  ๋•Œ

โœ๏ธUnmount

์ปดํฌ๋„ŒํŠธ์˜ ์†Œ๋ฉธ

  • componentWillUnmount: ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ ์‚ฌ๋ผ์ง€๊ธฐ ์ „์— ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

 

 

๐Ÿ“–ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

๐Ÿ”Žํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ

  • ๊ณผ๊ฑฐ์˜ ๋ฐฉ์‹
  • Component๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•œ๋‹ค.
  • render() ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค.
  • state, lifeCycle ๊ด€๋ จ ๊ธฐ๋Šฅ์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.   16.8๋ฒ„์ „๋ถ€ํ„ฐ๋Š” Hook์„ ํ†ตํ•ด ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ lifeCycle ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฉ”๋ชจ๋ฆฌ ์ž์›์„ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ณด๋‹ค ์กฐ๊ธˆ ๋” ์‚ฌ์šฉํ•œ๋‹ค.
import React, {Component} from 'react';

class App extends Component{
	render(){
		const text = 'Hi';
		return <div>{text}</div>
	}
}

export default App;

 

๐Ÿ”Žํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

์œ„์™€ ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ›จ์”ฌ ์ฝ”๋“œ๊ฐ€ ๊ฐ€๋…์„ฑ์ด ์ข‹๊ณ  ๊ฐ„๊ฒฐํ•˜๋‹ค.)

 

import React from 'react';

const App = () => {
	const text = 'Hi';
	return <div>{text}</div>
}

export default App;

 

 

์•ž์„œ ๋งํ–ˆ๋“ฏ 16.8๋ฒ„์ „๋ถ€ํ„ฐ๋Š” Hook์„ ํ†ตํ•ด ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ lifeCycle ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

*Mount & Unmount

import React from 'react';

const App = () => {
	const text = 'Hi';
	return <div>{text}</div>
}

export default App;

 

*Re-render

useEffect(() => {
// ์—…๋ฐ์ดํŠธ ๋‹จ๊ณ„ ์‹คํ–‰ ์ฝ”๋“œ
}, [count]);
728x90