Component ๐Ÿพ

Bonnie Ryuยท2020๋…„ 11์›” 8์ผ
1

์ฐจ๊ทผ์ฐจ๊ทผ React ๐Ÿพ

๋ชฉ๋ก ๋ณด๊ธฐ
3/10
post-custom-banner

1. Component


1-1. ์ปดํฌ๋„ŒํŠธ์˜ ์ •์˜

  • component : ์žฌํ™œ์šฉ ๊ฐ€๋Šฅํ•œ UI ๊ตฌ์„ฑ ๋‹จ์œ„

1-2. ์ปดํฌ๋„ŒํŠธ์˜ ํŠน์ง•

- ์žฌํ™œ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์ฝ”๋“œ ์œ ์ง€๋ณด์ˆ˜์— ์ข‹๋‹ค.
- ํ•ด๋‹น ํŽ˜์ด์ง€๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ•œ ๋ˆˆ์— ํŒŒ์•…ํ•˜๊ธฐ ์ข‹๋‹ค.
- ์ปดํฌ๋„ŒํŠธ๋Š” ๋˜ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ - ์ž์‹ ์ปดํฌ๋„ŒํŠธ)

1-3. ์ปดํฌ๋„ŒํŠธ๋Š” ์–ธ์ œ ํ•„์š”ํ• ๊นŒ?

- ๋ฐ˜๋ณตํ•ด์„œ ์ถœํ˜„ํ•˜๋Š” HTML UI
- ์ž์ฃผ ๋ณ€๊ฒฝ๋˜๋Š” HTML UI
- ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๋•Œ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ (์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  withRouterHOC ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

1-4. ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์‹œ ์œ ์˜์ 

- ์ปดํฌ๋„ŒํŠธ์˜ ์ฒซ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. (์†Œ๋ฌธ์ž ์ž…๋ ฅ ์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ)
- ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ณผํ•˜๊ฒŒ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค. (state ์‚ฌ์šฉํ•  ๋•Œ)

2. Class vs Functional Component

์ปดํฌ๋„ŒํŠธ์˜ ์ข…๋ฅ˜, ์ฆ‰ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ์‹์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • Classํ˜• ์ปดํฌ๋„ŒํŠธ(Class Component)
  • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ(Functional Component)

1) Class Component

import React from 'react'

class Component extends React.Component {
  render() {
    return (
	<div>
		<h1>This is Class Component!</h1>
	</div>
     )
  }
}

export default Component
  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์œ„์™€๊ฐ™์ด render ํ•จ์ˆ˜๊ฐ€ ๊ผญ ์žˆ์–ด์•ผ ํ•˜๊ณ , ๊ทธ ์•ˆ์— ๋ณด์—ฌ ์ฃผ์–ด์•ผ ํ•  JSX๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • state ๊ธฐ๋Šฅ & ๋ผ์ดํ”„์‚ฌ์ดํด ๊ธฐ๋Šฅ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์ž„์˜๋ฉ”์„œ๋“œ ์ •์˜ ๊ฐ€๋Šฅ

2) Functional Component

import React from 'react'

const Component = () => {
  return (
    <div>
		<h1>This is Functional Component!</h1>
	</div>
	)
};

export default Component
  • ์„ ์–ธํ•˜๊ธฐ ๊ฐ„ํŽธํ•˜๋‹ค.
  • ๋ฉ”๋ชจ๋ฆฌ ์ž์›์ด ๋œ ์‚ฌ์šฉ๋œ๋‹ค.(ํ”„๋กœ์ ํŠธ๊ฐ€ ์ข…๋ฃŒ๋˜๊ณ  ๋นŒ๋“œ ํ›„ ๋ฐฐํฌ์‹œ์—๋„ ๊ฒฐ๊ณผ๋ฌผ ํŒŒ์ผํฌ๊ธฐ๊ฐ€ ๋” ์ž‘๋‹ค.)
  • state์™€ ๋ผ์ดํ”„์‚ฌ์ดํด API ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์ง€๋งŒ ์•ž์œผ๋กœ ๋ฐฐ์šธ Hooks๊ธฐ๋Šฅ ๋„์ž…๋˜๋ฉด์„œ ํ•ด๊ฒฐโœจ!

๐Ÿฅ ๊ฒฐ๋ก  - ํด๋ ˆ์Šคํ˜•๊ณผ ํ•จ์ˆ˜ํ˜•์€ ์—ญํ• ๊ณผ ์„ฑ๋Šฅ์€ ๊ฐ™๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ถ„๋ช…ํžˆ ํ•จ์ˆ˜ํ˜•์ด ๋” ํŽธ๋ฆฌํ•˜๊ณ  ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํด๋ ˆ์Šคํ˜•์œผ๋กœ ๊ธฐ๋ณธ๊ธฐ๋ฅผ ์ตํžŒํ›„ Hooks๋ฅผ ์™„๋ฒฝํžˆ ์ดํ•ดํ•˜๊ณ  ํ•จ์ˆ˜ํ˜•์œผ๋กœ ๋‹ค๋‹ค๋‹ค๋‹ฅ ์ž‘์„ฑํ• ๊ฑฐ๋‹ค ..๐Ÿคช

profile
Ryuwisdom
post-custom-banner

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