๐Ÿ“—[React ์ฃผ์š” ๊ฐœ๋…] 1. ์ปดํฌ๋„ŒํŠธ, props

ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•จ.

props

  • react๊ฐ€ ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•œ element๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ, ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๋Š” ๋‹จ์ผ ๊ฐ์ฒด. (JSX ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์™€ ์ž์‹)
  • ์ฝ๊ธฐ ์ „์šฉ.(์ปดํฌ๋„ŒํŠธ ์ž์ฒด props๋Š” ์ˆ˜์ •ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.)
    ex) Welcome ์ปดํฌ๋„ŒํŠธ.
    ์–ดํŠธ๋ฆฌ๋ทฐํŠธ name์ด props ๊ฐ์ฒด ์•ˆ์— ํฌํ•จ๋˜์–ด ์ „๋‹ฌ๋จ.
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(
  element,
  document.getElementById('root')
);```

์ปดํฌ๋„ŒํŠธ ์ข…๋ฅ˜

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ

  • ์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค๊ณ ๋Š” ํ•จ.
  • javascriptํ•จ์ˆ˜ ์ž‘์„ฑ
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

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

์ปดํฌ๋„ŒํŠธ ๊ตฌ์„ฑ

์ปดํฌ๋„ŒํŠธ์•ˆ์— ์ปดํฌ๋„ŒํŠธ

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

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}
function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}
ReactDOM.render(
  <App />,
  document.getElementById('root')
);

์ปดํฌ๋„ŒํŠธ ์ชผ๊ฐœ๊ธฐ

  • ์ „
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>
  );
}
  • ํ›„
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>
  );
}

Class Component๊ฐ€ Functional Component๋ณด๋‹ค ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณต

Functional์€ ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์ œ๊ณตํ•˜์ง€ ๋ชปํ•จ.

=> Functional Component์—์„œ๋„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ React Hook์ด ๋‚˜์˜ด.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ