* ์ด ๊ธ€์€ ์†Œํ”Œ๋‹˜์˜ `์ฒ˜์Œ ๋งŒ๋‚œ ๋ฆฌ์—‘ํŠธ(React) ๊ฐ•์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ฒŒ์‹œ๋ฌผ์ž…๋‹ˆ๋‹ค.

์ฒ˜์Œ ๋งŒ๋‚œ ๋ฆฌ์—‘ํŠธ(React) - ์†Œํ”Œ

Ch05. Components

๋ฆฌ์—‘ํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์˜ ๊ตฌ์กฐ์ด๋‹ค! ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ ˆ๊ณ  ๋ธ”๋Ÿญ์„ ๋ผ์›Œ ๋„ฃ๋“ฏ์ด ๊ตฌ์„ฑํ•œ๋‹ค.

React Component๋Š” ๋ฌด์Šจ ์ผ์„ ํ• ๊นŒ?

์†์„ฑ(props)์„ ๋ฐ›์•„์„œ ๊ทธ์— ๋งž๋Š” react element๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. (์ผ์ข…์˜ ํ•จ์ˆ˜๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ํŽธํ•˜๋‹ค.)

โ€˜Component(๋ถ•์–ด๋นต ํ‹€) - Elemenet(๋ถ•์–ด๋นต)โ€™ ์ด๋ผ๊ณ  ๋น„์œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ์—์„œ ๋‚˜์˜ค๋Š” class์™€ ์ธ์Šคํ„ด์Šค ๊ฐœ๋…๊ณผ ๋น„์Šทํ•˜๋‹ค.

Props(Properties ์†์„ฑ๋“ค)

Component์— ์ „๋‹ฌํ•  ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์ด๋‹ค.

Component๋ฅผ ๋ถ•์–ด๋นต ํ‹€์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, props๋Š” ๋ถ•์–ด๋นต์˜ ์† ์žฌ๋ฃŒ(์Šˆํฌ๋ฆผ, ํŒฅ ๋“ฑ)๋ผ๊ณ  ๋น„์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Props์˜ ํŠน์ง• - Read-Only!

๊ฐ’์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ณ  ์ฝ์„ ์ˆ˜๋งŒ ์žˆ๋‹ค. ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์ƒˆ๋กœ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

๋ชจ๋“  react component๋Š” props๋ฅผ ์ง์ ‘ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ณ , ๊ฐ™์€ Props์— ๋Œ€ํ•ด์„œ๋Š” ํ•ญ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค!

Props ์‚ฌ์šฉ๋ฒ• - component์— props ์ „๋‹ฌํ•˜๊ธฐ

์˜ˆ์‹œ)

function App(props) {
	return (
		<Profile name="Daeun" introduction="Hello, my name is Daeun" viewCount={1500}/>
	)
}

props๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ JS ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค.

{
	name: "Daeun",
	introduction: "Hello, my name is Daeun",
	viewCount: 1500
}

์˜ˆ์‹œ) props์˜ ๊ฐ’์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

function App(props) {
	return (
		<Layout width={2560} height={1440} 
			header={<Header title="Daeun's Blog"/>}
			footer={<Footer/>}
		/>
	)
}

React.createElement()

React.createElement(type, [props], [...children])

์‚ฌ์šฉ ์˜ˆ์‹œ)

React.createElement(
	Profile, // Component ์ด๋ฆ„
	{ // props
		name: "Daeun",
		introduction: "Hello, my name is Daeun",
		viewCount: 1500
	},
	null // ํ•˜์œ„ Component๊ฐ€ ์—†์œผ๋ฏ€๋กœ
);

Component

๋ฆฌ์—‘ํŠธ์—์„œ Component๋Š” ํฌ๊ฒŒ Function Component์™€ Class Component๋กœ ๋‚˜๋‰œ๋‹ค.

  • function component: ์ฝ”๋“œ๊ฐ€ ๊ฐ„๋‹จํ•œ ๊ฒƒ์ด ์žฅ์ 
      function Welcome(props) {
          return <h1>Hello, {props.name}</h1>;
      }
    
  • class component: function ์ปดํฌ๋„ŒํŠธ์— ๋น„ํ•ด ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„๋‹ค.
          class Welcome extends React.Component {
              render() {
                  return <h1>Hello, {this.props.name}</h1>;
              }
          }
    

    React.Component๋ฅผ ์ƒ์†ํ•ด์„œ ๊ตฌ์„ฑ๋œ๋‹ค.

์ค‘์š”!! - Component ์ด๋ฆ„์€ ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค.

์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” DOM ํƒœ๊ทธ๋กœ ์ธ์‹ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

์˜ˆ์‹œ)

// HTML div ํƒœ๊ทธ๋กœ ์ธ์‹
const element = <div />;

// Welcome์ด๋ผ๋Š” React Component๋กœ ์ธ์‹
const element = <Welcome name="Daeun" />;

Component์˜ ๋ Œ๋”๋ง

function Welcome(props) {
	return <h1>Hello, {props.name}</h1>;
}

// React Component๋กœ element๋ฅผ ๋งŒ๋“  ๋ชจ์Šต
const element = <Welcome name="Daeun" />; // name: "Daeun"์ด๋ผ๋Š” props๋ฅผ ๋„ฃ์–ด element ์ƒ์„ฑ

// element๋ฅผ ๋ Œ๋”๋ง(DOM์— ์—…๋ฐ์ดํŠธ)
ReactDOM.render(
	element,
	document.getElementById('root')
);

Ch06. State and Lifecycle

Class Component์™€ ๊ด€๋ จ๋œ ๋‚ด์šฉ์„ ๋‹ค๋ฃฌ ์ฑ•ํ„ฐ์ด๋‹ค.

State

state๋ž€ ๋ฆฌ์—‘ํŠธ component์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•œ๋‹ค. state๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

state๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ๋ Œ๋”๋ง๊ณผ ๊ด€๋ จ๋˜์–ด ์žˆ๋‹ค!!

๋ Œ๋”๋ง์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’๋งŒ state์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•œ๋‹ค. (state๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ component๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ Œ๋”๋ง์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์— ๊ด€๋ จ ์—†๋Š” ๊ฒƒ์„ state์— ํฌํ•จํ•œ ๊ฒฝ์šฐ ๋ถˆํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์žฌ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜๊ฒŒ ๋˜๋ฏ€๋กœ ๋น„ํšจ์œจ์ ์ด๋‹ค.)

state๋Š” JS ๊ฐ์ฒด์ด๋‹ค!

class LikeButton extends React.Component {
	constructor(props) { // ์ƒ์„ฑ์ž
		super(props),

		this.state = {	// class component์˜ ๊ฒฝ์šฐ state๋ฅผ ์ƒ์„ฑ์ž(constructor)์—์„œ ์ •์˜ํ•œ๋‹ค.
			liked: false
		};
	}
}

state๋Š” ์ผ๋ฐ˜ ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฃจ๋“ฏ์ด ์ง์ ‘ ์ˆ˜์ •ํ•˜๋ฉด ์•ˆ๋œ๋‹ค! (setState๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ!)

React Component์˜ Lifecycle(์ƒ๋ช… ์ฃผ๊ธฐ)

component์˜ ์ƒ์„ฑ ์‹œ์ ๊ณผ ์‚ฌ๋ผ์ง€๋Š” ์‹œ์ ์ด ์ •ํ•ด์ ธ ์žˆ๋‹ค.

  1. Mounting(์ถœ์ƒ) - constructor
  2. Updating(์ธ์ƒ)
  3. Unmounting(์‚ฌ๋ง) - ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋”์ด์ƒ ํ‘œ์‹œํ•˜์ง€ ์•Š์„ ๋•Œ

Component๊ฐ€ ๊ณ„์† ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์‹œ๊ฐ„์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ์ƒ์„ฑ๋˜๊ณ  ์—…๋ฐ์ดํŠธ ๋˜๋‹ค๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค!