๐Ÿฅ ๊ณต์‹๋ฌธ์„œ๋กœ ๋ฐฐ์šฐ๋Š” React

Sohyeon Bakยท2022๋…„ 8์›” 16์ผ
0

React

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

03. Component

๋ฆฌ์•กํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ๋Š” ๋…๋ฆฝ์ ์ด๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ์˜ ์ฝ”๋“œ ๋‹จ์œ„์ด๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๋ชฉ์ ์„ ์ œ๊ณตํ•˜์ง€๋งŒ ๋ถ„๋ฆฌ๋˜์–ด ๋™์ž‘ํ•˜๊ณ  HTML์„ return ํ•ด์ฃผ๋Š” ํŠน์ง•์„ ๊ฐ€์กŒ๋‹ค.
์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฆ„์€ ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค.

Function and Class Components

์ปดํฌ๋„ŒํŠธ๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋‰œ๋‹ค.

  • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
    • export ํ•˜์—ฌ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}
  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ
    • ์ปดํฌ๋„ŒํŠธ ๊ตฌ์„ฑ ์š”์†Œ์™€ ๋ฆฌ์•กํŠธ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•œ๋‹ค.
    • ํ”„๋กœํผํ‹ฐ, state, ์ƒ๋ช…์ฃผ๊ธฐ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•œ ๊ตฌ์กฐ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋‹ค.
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

Rendering a Component

const element = <Welcome name="Sara" />;

๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ๋กœ DOM ํƒœํฌ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
๋ฆฌ์•กํŠธ๊ฐ€ ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋‚˜๋ฉด ์ž‘์„ฑ๋œ ์š”์†Œ๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ์ „๋‹ฌํ•ด์ฃผ๋Š”๋ฐ ๊ทธ๊ฒƒ์ด props๊ฐ€ ๋œ๋‹ค.
props๋Š” ๋‹จ์ผ ๊ฐ์ฒด๋กœ ์š”์†Œ๋ฅผ key์™€ value ๊ฐ’์œผ๋กœ ์ •์˜ํ•ด ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

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

const element = <Welcome name="Sara" />;
ReactDOM.render(
  element,
  document.getElementById('root')
);

๐Ÿ“Œ what happens in this example

  • <Welcome name="Sara" />ย ์—˜๋ฆฌ๋จผํŠธ๋กœย ReactDOM.render()๋ฅผ ํ˜ธ์ถœ
  • React๋Š”ย {name: 'Sara'}๋ฅผ props๋กœ ํ•˜์—ฌย Welcomeย ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ˜ธ์ถœ
    • Welcom ์ปดํฌ๋„ŒํŠธ์— ์ž‘์„ฑ๋œ ์š”์†Œ๋ฅผ props ๋‹จ์ผ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.
  • Welcomeย ์ปดํฌ๋„ŒํŠธ๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœย <h1>Hello, Sara</h1>ย ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜
  • React DOM์€ย <h1>Hello, Sara</h1>ย ์—˜๋ฆฌ๋จผํŠธ์™€ ์ผ์น˜ํ•˜๋„๋ก DOM์„ ํšจ์œจ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ

Composing Components

์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ ์ถœ๋ ฅ์— ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐธ์กฐํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ปดํฌ๋„ŒํŠธ์— ๋‹ค๋ฅธ ์š”์†Œ๋ฅผ ์ž…๋ ฅํ•ด ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

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

function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}

Extracting Components

์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฌ๋Ÿฌ ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ํฐ ํ”„๋กœ์ ํŠธ์—์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋กœ ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.
๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค.

function Comment(props) {
  return (
    <div className="Comment">
      <div className="UserInfo">
        <img className="Avatar"
          src={props.author.avatarUrl}
          alt={props.author.name}
        />
        <div className="UserInfo-name">
          {props.author.name}
        </div>
      </div>
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

์ด ์ปดํฌ๋„ŒํŠธ๋Š” props์— author(๊ฐ์ฒด),ย text(๋ฌธ์ž์—ด) ๋ฐย date(๋‚ ์งœ)๋ฅผ props๋กœ ๋ฐ›๋Š”๋‹ค.
์œ„ ์ฝ”๋“œ๋Š” ์ค‘์ฒฉ๊ตฌ์กฐ๋กœ ๋ณ€ํ˜•ํ•˜๊ธฐ ์–ด๋ ต๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ๋„ ๋†’์ง€ ์•Š์•„ ๋ณด์ธ๋‹ค.
ํ•˜์ง€๋งŒ ์ฝ”๋“œ๊ตฌ์กฐ์™€ ์ปดํฌ๋„ŒํŠธ ๋ถ„๋ฆฌ๋กœ ๊ฐ€๋…์„ฑ ๋†’์€ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

function Avatar(props) {
  return (
    <img className="Avatar"
      src={props.user.avatarUrl}
      alt={props.user.name}
    />
  );
}

function UserInfo(props) {
  return (
    <div className="UserInfo">
      <Avatar user={props.user} />
      <div className="UserInfo-name">
        {props.user.name}
      </div>
    </div>
  );
}


function Comment(props) {
  return (
    <div className="Comment">
      <UserInfo user={props.author} /> //๋ถ„๋ฆฌํ•œ ์ปดํฌ๋„ŒํŠธ ์ ์šฉ
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

Avatar ์ปดํฌ๋„ŒํŠธ์—์„œ props๋Š” ์ƒˆ๋กญ๊ฒŒ key ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.
props์˜ ์ด๋ฆ„์€ ์‚ฌ์šฉ๋  context๊ฐ€ ์•„๋‹Œ ์ปดํฌ๋„ŒํŠธ ์ž์ฒด์—์„œ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ด€์ ์œผ๋กœ ์ง“๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

์ฐธ๊ณ 

https://www.w3schools.com/react/react_components.asp
https://goddaehee.tistory.com/299
https://ko.reactjs.org/docs/components-and-props.html

profile
์ •๋ฆฌํ•˜๊ณ  ๊ธฐ์–ตํ•˜๋Š” ๊ณณ

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