3 minute read

๋ณธ ๊ฒŒ์‹œ๊ธ€์€ย ๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ด๋‹ค.

Components์™€ Props - ์ปดํฌ๋„ŒํŠธ์™€ ์†์„ฑ

: ๊ฐœ๋…์ ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋Š” JavaScript ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•˜๋‹ค. โ€œpropsโ€๋ผ๊ณ  ํ•˜๋Š” ์ž„์˜์˜ ์ž…๋ ฅ์„ ๋ฐ›์€ ํ›„, ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ํ‘œ์‹œ๋˜๋Š”์ง€๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

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

: ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ JavaScript ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ
1
2
3
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

: ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง„ ํ•˜๋‚˜์˜ "props" ๊ฐ์ฒด ์ธ์ž๋ฅผ ๋ฐ›์€ ํ›„ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์œ ํšจํ•œ React ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.
์ด๋Ÿฌํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” JavaScript ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ง ๊ทธ๋Œ€๋กœ โ€œํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธโ€๋ผ๊ณ  ํ˜ธ์นญํ•œ๋‹ค.

  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ
1
2
3
4
5
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}
  • React์˜ ๊ด€์ ์—์„œ ๋ณผ ๋•Œ ์œ„ ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์˜ ์ปดํฌ๋„ŒํŠธ๋Š” ๋™์ผํ•˜๋‹ค.

์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง

: React๋Š” ๊ธฐ์กด์˜ DOM ํƒœ๊ทธ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ๋„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ์กด DOM ํƒœ๊ทธ
1
const element = <div />;
  • ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ
1
const element = <Welcome name="Sara" />;

React๊ฐ€ ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด JSX ์–ดํŠธ๋ฆฌ๋ทฐํŠธ(name=โ€Saraโ€)์™€ ์ž์‹์„ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์— ๋‹จ์ผ ๊ฐ์ฒด๋กœ ์ „๋‹ฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๋•Œ ์ „๋‹ฌ๋˜๋Š” ๊ฐ์ฒด๋ฅผ โ€œpropsโ€ ๋ผ๊ณ  ํ•œ๋‹ค.

1
2
3
4
5
6
7
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

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

์ด ์˜ˆ์‹œ์—์„œ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ฌ๋Š”์ง€ ์š”์•ฝํ•˜์ž๋ฉด

  1. <Welcome name="Sara" />ย ์—˜๋ฆฌ๋จผํŠธ๋กœย root.render()๋ฅผ ํ˜ธ์ถœ
  2. React๋Š”ย {name: 'Sara'}๋ฅผย props๋กœ ํ•˜์—ฌย Welcomeย ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ˜ธ์ถœ
  3. Welcomeย ์ปดํฌ๋„ŒํŠธ๋Š”ย <h1>Hello, Sara</h1>ย ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜
  4. React DOM์€ย <h1>Hello, Sara</h1>ย ์—˜๋ฆฌ๋จผํŠธ์™€ ์ผ์น˜ํ•˜๋„๋ก DOM์„ ์—…๋ฐ์ดํŠธ

๐Ÿšจ ์ฃผ์˜ : ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฆ„์€ ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
React๋Š” ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ DOMํƒœ๊ทธ๋กœ ์ธ์‹ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ดย <div />๋Š” HTML div ํƒœ๊ทธ๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€๋งŒ,ย <Welcome />์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋–„๋ฌธ์— ๋ฒ”์œ„ ์•ˆ์—ย Welcome์ด ์ •์˜ ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค.

์ปดํฌ๋„ŒํŠธ ํ•ฉ์„ฑ

: ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ ์ถœ๋ ฅ์— ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ดย Welcome์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ Œ๋”๋งํ•˜๋Š”ย Appย ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

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

์ผ๋ฐ˜์ ์œผ๋กœ ์ƒˆ React ์•ฑ์€ ์ตœ์ƒ์œ„์— ๋‹จ์ผ App ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ๊ธฐ์กด ์•ฑ์— React๋ฅผ ํ†ตํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” Button๊ณผ ๊ฐ™์€ ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ทฐ ๊ณ„์ธต์˜ ์ƒ๋‹จ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ์ ์ง„์ ์œผ๋กœ ์ž‘์—…ํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์œ„์˜ ์˜ˆ์‹œ์—์„œ Welcome ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋งŒ ๋‹ค๋ฅธ์ฑ„ 3๊ฐœ๊ฐ€ ๋ฐฐ์น˜๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. : ์ปดํฌ๋„ŒํŠธ์˜ ์ถ”์ƒํ™”
  • ๋˜ App ์ปดํฌ๋„ŒํŠธ ์•ˆ์— Welcome ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐฐ์น˜๋œ ๊ฒƒ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. : ์ปดํฌ๋„ŒํŠธ์˜ ํ•ฉ์„ฑ

์ปดํฌ๋„ŒํŠธ ์ถ”์ถœ

: ์ปดํฌ๋„ŒํŠธ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ , ๊ฐ€๋Šฅํ•œ ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ธ ์ž‘์—…์„ ํ•˜๋Š”๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>
  );
}

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

๐Ÿขํ•ด๊ฒฐ:Commentย ์ปดํฌ๋„ŒํŠธ์—์„œย Avatar,ย UserInfo์™€ ๊ฐ™์ด ๋ช‡ ๊ฐ€์ง€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐœ๋ณ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค๋ฉดย Commentย ์ปดํฌ๋„ŒํŠธ๋Š” ๋” ๋‹จ์ˆœํ•ด์ง€๊ณ  ํšจ์œจ์ ์ธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

1๏ธโƒฃ ๋จผ์ € Avatar๋ฅผ ์ถ”์ถœํ•œ๋‹ค.

1
2
3
4
5
function Avatar(props) {
  return (
    <img className="Avatar" src={props.user.avatarUrl} alt={props.user.name} />
  );
}
  • Avatarย ๋Š” ์ž์‹ ์ดย Commentย ๋‚ด์—์„œ ๋ Œ๋”๋ง ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— props์˜ ์ด๋ฆ„์„ย author์—์„œ ๋”์šฑ ์ผ๋ฐ˜ํ™”๋œย user๋กœ ๋ณ€๊ฒฝํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ props์˜ ์ด๋ฆ„์€ ์‚ฌ์šฉ๋  context๊ฐ€ ์•„๋‹Œ ์ปดํฌ๋„ŒํŠธ ์ž์ฒด์˜ ๊ด€์ „์—์„œ ์ง“๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
function Comment(props) {
  return (
    <div className="Comment">
      <div className="UserInfo">
        <Avatar user={props.author} />
        <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>
  );
}
  • ๐Ÿ’จ๊ฒฐ๊ณผ: Commentย ๊ฐ€ ์‚ด์ง ๋‹จ์ˆœํ•ด์กŒ๋‹ค.

2๏ธโƒฃ Avatarย ์˜†์— ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„์„ ๋ Œ๋”๋งํ•˜๋Š”ย UserInfoย ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”์ถœํ•œ๋‹ค.

1
2
3
4
5
6
7
8
function UserInfo(props) {
  return (
    <div className="UserInfo">
      <Avatar user={props.user} />
      <div className="UserInfo-name">{props.user.name}</div>
    </div>
  );
}
1
2
3
4
5
6
7
8
9
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>
  );
}
  • ๐Ÿ’จ๊ฒฐ๊ณผ: Commentย ๊ฐ€ ๋”์šฑ ๋‹จ์ˆœํ•ด์กŒ๋‹ค.
    ์ด๋Ÿฐ ์‹์œผ๋กœ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฒƒ์€ ํฐ ๊ทœ๋ชจ์˜ ์•ฑ์—์„œ ์ž‘์—…ํ•  ๋•Œ ๋‘๊ฐ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. UI์˜ ์ผ๋ถ€๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉ๋˜๊ฑฐ๋‚˜(Button,ย Panel,ย Avatar), UI์˜ ์ผ๋ถ€๊ฐ€ ์ž์ฒด์ ์œผ๋กœ ๋ณต์žกํ•œ(App,ย FeedStory,ย Comment) ๊ฒฝ์šฐ์—๋Š” ๊ฐœ๋ณ„์ ์ธ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Props

: ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋‚˜ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋ชจ๋‘ ์ปดํฌ๋„ŒํŠธ์˜ ์ž์ฒด props๋ฅผ ์ˆ˜์ •ํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
React๋Š” ๋งค์šฐ ์œ ์—ฐํ•˜์ง€๋งŒ ํ•œ ๊ฐ€์ง€ ์—„๊ฒฉํ•œ ๊ทœ์น™์ด ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ props๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ๋ฐ˜๋“œ์‹œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ ์ฒ˜๋Ÿผ ๋™์ž‘ํ•ด์•ผ ํ•œ๋‹ค.
  • ์ˆœ์ˆ˜ ํ•จ์ˆ˜ โญ• : ์ž…๋ ฅ๊ฐ’์€ ๋ฐ”๊พธ๋ ค ํ•˜์ง€ ์•Š๊ณ  ํ•ญ์ƒ ๋™์ผํ•œ ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
1
2
3
function sum(a, b) {
  return a + b;
}
  • ์ˆœ์ˆ˜ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹˜ โŒ: ์ž์‹ ์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์ˆ˜ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.
1
2
3
function withdraw(account, amount) {
  account.total -= amount;
}

Tags:

Categories:

Updated:

Leave a comment