[TIL] 220321

Lee Syong·2022년 3ė›” 21ėž
0

TIL

ëŠĐ록 ëģīęļ°
200/204
post-thumbnail

📝 ė˜Ī늘 한 ęēƒ

  1. react rendering

📖 학ėŠĩ ėžëĢŒ

  1. react ëŽļė„œ

📚 ë°°ėšī ęēƒ

ėƒˆëĄ­ęēŒ ė•ŒęēŒ 된 ë‚īėšĐėī거나 ėīí•ī하ęļ° ė–īë Īė› ë˜ ëķ€ëķ„만 ė •ëĶŽ

1. JSX ė†Œę°œ

1) XSS ęģĩęēĐ ë°Đė§€

[HTTP/ëģīė•ˆ] 큮로ėŠĪ ė‚ŽėīíŠļ ėŠĪ큎ëĶ―íŠļ ęģĩęēĐ( XSS : Cross-site Scripting Attacks ) ė°ļęģ 

XSS(cross-site-scripting) ęģĩęēĐėī란 ė›đė‚ŽėīíŠļ ęī€ëĶŽėžę°€ ė•„ë‹Œ ėī가 ė›đ 페ėīė§€ė— ė•…ė„ą ėŠĪ큎ëĶ―íŠļëĨž ė‚―ėž…하는 ęēƒė„ 말한ë‹Ī.
ę·ļ런데 ęļ°ëģļė ėœžëĄœ React DOMė€ JSXė— ė‚―ėž…된 ëŠĻ든 값ė„ 렌더링하ęļ° ė „ė— ėīėŠĪėž€ėī프 한ë‹Ī. (ėīėŠĪėž€ėī핑 ė°ļęģ )
따띾ė„œ, ė• í”ŒëĶŽėž€ėīė…˜ė—ė„œ 멅ė‹œė ėœžëĄœ ėž‘ė„ąë˜ė§€ ė•Šė€ ë‚īėšĐė€ ė‚―ėž…될 ėˆ˜ ė—†ë‹Ī.

ë‹Īė‹œ 말í•ī, JSXė— ė‚―ėž…된 ëŠĻ든 값ė€ 렌더링 되ęļ° ė „ė— ëŽļėžė—ī로 ëģ€í™˜ë˜ęļ° 때ëŽļė— ė•…ė„ą ėŠĪ큎ëĶ―íŠļëĨž ė‚―ėž…하는 XSS ęģĩęēĐė„ ë°Đė§€í•  ėˆ˜ ėžˆëŠ” ęēƒėīë‹Ī.

2. JSX ėīí•ī하ęļ°

1) propė˜ ęļ°ëģļ값ė€ 'true'ėīë‹Ī

<MyTextBox autocomplete />

// ėœ„ė™€ ė•„래는 같ė€ 표현ėīë‹Ī.
// ėžë°˜ė ėœžëĄœ propė— 값ė„ ė „닎하는 ęēƒė„ ęķŒėžĨ한ë‹Ī. ES6 ëŽļëē•ęģž í—·ę°ˆëĶī ėˆ˜ ėžˆęļ° 때ëŽļėīë‹Ī.

<MyTextBox autocomplete={true} />

2) ė†ė„ą 펾ėđ˜ęļ° - ė „ę°œ ė—°ė‚°ėž(...) ė‚ŽėšĐ

  • HEAD ėŧī폮넌íŠļ가 ė‚ŽėšĐ할 propsė— í•īë‹đ하는 객ėēīëĨž ëķ€ëŠĻ ėŧī폮넌íŠļ(App2)가 ėīëŊļ 가ė§€ęģ  ėžˆë‹ĪëĐī, ė „ę°œ ė—°ė‚°ėž(spread ė—°ė‚°ėž)ëĨž ė‚ŽėšĐí•ī ė „ėēī 객ėēīëĨž ėžė‹ ėŧī폮넌íŠļ(ėĶ‰, HEAD ėŧī폮넌íŠļ Greeting)로 ę·ļ대로 넘ęēĻėĪ„ ėˆ˜ ėžˆë‹Ī.
function App1() {
  return <Greeting firstName="Ben" lastName="Hector" />;
}

// ėœ„ė™€ ė•„래는 같ė€ 표현ėīë‹Ī.

function App2() {
  const props = {firstName: 'Ben', lastName: 'Hector'};
  return <Greeting {...props} />;
}
  • HEAD ėŧī폮넌íŠļė—ė„œ HEAD ėŧī폮넌íŠļ가 ė‚ŽėšĐ하ęēŒ 될 íŠđė • propė„ ė„ íƒí•˜ęģ , 나ëĻļė§€ propė€ ė „ę°œ ė—°ė‚°ėžëĨž í†ĩí•ī 넘ęļļ ėˆ˜ ėžˆë‹Ī. ėī는 HEAD ėŧī폮넌íŠļëĨž ėœ ėšĐ하ęēŒ ėžŽė‚ŽėšĐ할 ėˆ˜ ėžˆë„록 한ë‹Ī.
const Button = props => {
  const { kind, ...other } = props;
  const className = kind === "primary" ? "PrimaryButton" : "SecondaryButton";
  return <button className={className} {...other} />;
};

const App = () => {
  return (
    <div>
      <Button kind="primary" onClick={() => console.log("clicked!")}>
        Hello World!
      </Button>
    </div>
  );
};

3) í•Ļėˆ˜ëĨž ėžė‹ėœžëĄœ ė‚ŽėšĐ하ęļ°

ëģīí†ĩ JSXė— ė‚―ėž…된 JavaScript 표현ė‹ė€ ëŽļėžė—īėī나 React element 또는 ėīë“Īė˜ ë°°ė—ī로 환ė‚°ëœë‹Ī.

ę·ļ럮나, props.children 또한 ë‹ĪëĨļ propë“Īęģž 마ė°Žę°€ė§€ëĄœ React가 렌더링 할 ėˆ˜ ėžˆëŠ” 데ėī터ė˜ 형태ëŋ ė•„니띞 (onClick ėīëēĪíŠļ ëĶŽėŠĪ너 í•Ļėˆ˜ëĨž 넘ęļ°ëŠ” ęēƒėē˜ëŸž) ė–īë–Ī 형태ė˜ 데ėī터도 넘ęēĻė§ˆ ėˆ˜ ėžˆë‹Ī.

ė•„래ė˜ ė˜ˆė‹œė—ė„œëŠ” props.childrenė„ í†ĩí•īė„œ ė―œë°ąė„ 넘ęēĻ받ė•„ 반ëģĩė ėļ ėŧī폮넌íŠļëĨž ėƒė„ąí•˜ęģ  ėžˆë‹Ī.

function Repeat(props) {
  // console.log(props); => {numTimes: 10, children: ƒ}

  let items = [];

  for (let i = 0; i < props.numTimes; i++) {
    items.push(props.children(i));
  }

  return <div>{items}</div>; {/* React elementë“Īė˜ ë°°ė—īė„ 반환한ë‹Ī */}
}

function ListOfTenThings() {
  return (
    <Repeat numTimes={10}>
      {(index) => <div key={index}>This is item {index} in the list</div>} {/* props.children */}
    </Repeat>
  );
}

const root = document.getElementById("root");
ReactDOM.render(<ListOfTenThings />, root);

4) ėĄ°ęąīëķ€ 렌더링

0ęģž 같ė€ falsy 값ė€ React가 렌더링하ęļ° 때ëŽļė— ėĢžė˜í•īė•ž 한ë‹Ī.

const falsy = 0;
const SomeComponent = () => <p>I am something else.</p>;
const Article = () => (
  <div className="something">
    {true && <SomeComponent />} {/* ëļŒëžėš°ė €ė— <SomeComponent /> 가 렌더링ëĻ */}
    {false && <SomeComponent />} {/* ëļŒëžėš°ė €ė— ė•„ëŽīęēƒë„ 렌더링되ė§€ ė•ŠėŒ */}
    {falsy && <SomeComponent />} {/* ëļŒëžėš°ė €ė— 0ėī 렌더링ëĻ */}
  </div>
);

const root = document.getElementById("root");
ReactDOM.render(<Article />, root);

cf. true, false, undefined 같ė€ 값ė„ ėķœë Ĩ하ęģ  ė‹ķë‹ĪëĐī String(false) ėē˜ëŸž ëŽļėžė—ī로 바ęŋ”ė•ž 한ë‹Ī.

3. Componentsė™€ Props

1) props는 ė―ęļ° ė „ėšĐėž…니ë‹Ī.

ëŠĻ든 React ėŧī폮넌íŠļ는 ėžė‹ ė˜ propsëĨž ë‹ĪëĢ° 때 반드ė‹œ ėˆœėˆ˜ í•Ļėˆ˜ėē˜ëŸž 동ėž‘í•œë‹Ī.
ėŧī폮넌íŠļ í•Ļėˆ˜ ë‚īëķ€ė—ė„œ ėļėžëĄœ 받ė€ propsė˜ 값ė„ ėˆ˜ė •í•īė„œ ė‚ŽėšĐí•īė„œëŠ” ė•ˆëœë‹Ī.

2) ėŧī폮넌íŠļ ėķ”ėķœ

UIė˜ ėžëķ€ę°€ ė—ŽëŸŽ ëēˆ ė‚ŽėšĐ되거나(Button, Panel, Avatar) UIė˜ ėžëķ€ę°€ ėžėēīė ėœžëĄœ ëģĩėžĄí•œ ęē―ėš°(App, FeedStory, Comment)ė—ëŠ” ëģ„도ė˜ ėŧī폮넌íŠļ로 만드는 ęēƒėī ėĒ‹ë‹Ī.

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

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

function Comment(props) { // props === { author: "...", text: "...", date: ... }
  const formatDate = (date) => date + "ė‹œ";

  return (
    <div className="Comment">
      <UserInfo user={props.author} />
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

function App() {
  const author = {
    name: "Syong",
    avatarUrl: "https://t1.daumcdn.net/cfile/tistory/24283C3858F778CA2E",
  };

  return (
    <div>
      <Comment author={author} text="댕댕ėīėž…니ë‹Ī" date={new Date().getHours()} />
    </div>
  );
};

const root = document.getElementById("root");
ReactDOM.render(<App />, root);
  • ES6 ęĩŽėĄ° ëķ„í•ī 할ë‹đ
function UserInfo({user}) { // // props.user === { avatarUrl: "...", name: "..." }
  return (
    <div className="UserInfo">
      <Img user={user} />
      <div className="UserInfo-name">
        {user.name}
      </div>
    </div>
  );
}

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

function Comment({author, text, date}) { // props === { author: "...", text: "...", date: "..." }
  const formatDate = (date) => date + "ė‹œ";

  return (
    <div className="Comment">
      <UserInfo user={author} />
      <div className="Comment-text">
        {text}
      </div>
      <div className="Comment-date">
        {formatDate(date)}
      </div>
    </div>
  );
}

function App() {
  const author = {
    name: "Syong",
    avatarUrl: "https://t1.daumcdn.net/cfile/tistory/24283C3858F778CA2E",
  };

  return (
    <div>
      <Comment author={author} text="댕댕ėīėž…니ë‹Ī" date={new Date().getHours()} />
    </div>
  );
};

const root = document.getElementById("root");
ReactDOM.render(<App />, root);

âœĻ ë‚īėž 할 ęēƒ

  1. react state
profile
ëŠĨ동ė ėœžëĄœ ė‚īėž, 행ëģĩ하ęēŒðŸ˜

0개ė˜ 댓ęļ€