JSX ๐Ÿพ

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

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

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

JSX

const element = <h1>Hello, world!</h1>;

์ด๊ฒŒ ๋ฐ”๋กœ JSX ์ด๋‹ค !

JSX
: Javascript XML(syntax extension for JavaScript / extensive markup language)

๊ทธ๋Ÿผ XML์€?
: HTML์˜ ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•œ ๋งˆํฌ์—… ์–ธ์–ด

  • ํŠน์ง• : ํ˜ธํ™˜์„ฑ / ํ™•์žฅ์„ฑ์ด ๋›ฐ์–ด๋‚จ, tag๋กœ ์ •์˜ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    ํ™ˆํŽ˜์ด์ง€ ๊ตฌ์ถ•, ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ํ–ฅ์ƒ, ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์šฉ์ด

  • HTML์€ ํ™”๋ฉด์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์„œ๊ฐ€ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ์ €์žฅ But, XML์€ ์š”์†Œ๋ณ„ ๊ฐœ๋ณ„ํŒŒ์ผ๋กœ ๊ตฌ์„ฑํ•˜๊ณ 
    ์š”์†Œ๋ณ„๋กœ ์ €์žฅ/๊ฒ€์ƒ‰/์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  • ํ˜„์žฌ XML๋‹จ๋…์œผ๋กœ๋Š” ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋ ‡๋‹ค !
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์˜ ํ™•์žฅํŒ !
๋งˆํฌ์—…๊ณผ ๋กœ์ง์„ ํฌํ•จํ•˜๋Š” '์ปดํฌ๋„ŒํŠธ'๋กœ ์‚ฌ์šฉ๋  ๋ฆฌ์•กํŠธ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์ด๋‹ค.

JSX์˜ ํŠน์„ฑ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

JSX element

HTML๋ฌธ๋ฒ•์„ JavaScript ์ฝ”๋“œ ๋‚ด๋ถ€์— ์จ์ฃผ๋ฉด ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ JSX .js ํŒŒ์ผ ์–ด๋””์—์„œ๋‚˜ ํ•„์š”ํ•œ ๊ณณ์— ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„˜๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค.

const hi = <p>Hi</p>;

const myFavorite = {
 food: <li>์ƒ๋Ÿฌ๋“œ</li>,
 animal: <li>dog</li>,
 hobby: <li>programming</li>
};

JSX attribute

ํƒœ๊ทธ์— attribute(์†์„ฑ)์„ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ๋Š” ํ•ญ์ƒ "" ์Œ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ฃผ์„ธ์š”. attribute๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์‹ค์ œ HTML์—์„œ ์“ฐ๋Š” attribute name(์†์„ฑ๋ช…)๊ณผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋‹ˆ react ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด, class๋ฅผ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์›๋ž˜ ์†์„ฑ๋ช…์€ class์ด์ง€๋งŒ JSX์—์„œ๋Š” className์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

const hi = <input readOnly={true} />;

const myFavorite = {
 food: <li>์ƒ๋Ÿฌ๋“œ</li>,
 animal: <li>dog</li>,
 hobby: <li className="list-item">programming</li>
};

Self-Closing Tag

๊ทธ๋ฆฌ๊ณ  JSX์—์„œ๋Š” ์–ด๋–ค ํƒœ๊ทธ๋ผ๋„ self closing tag๊ฐ€ ํ•ญ์ƒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. <input>๊ณผ ๊ฐ™์ด ํ•˜๋‚˜์˜ ํƒœ๊ทธ๊ฐ€ ์š”์†Œ์ธ ๊ฒฝ์šฐ์—๋Š” <input />์™€ ๊ฐ™์ด ํ•ญ์ƒ /์œผ๋กœ ๋๋‚ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<div />์™€ <div></div>๋Š” ๊ฐ™์€ ํ‘œํ˜„์ž…๋‹ˆ๋‹ค.

Nested JSX

  1. ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ๊ธฐ

    const good = (
    <div>
        <p>hi</p>
    </div>
    );

    ์ค‘์ฒฉ๋œ ์š”์†Œ๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด () ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ฃผ์„ธ์š”!

  2. ํ•ญ์ƒ ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ์‹œ์ž‘

    const wrong = (
    <p>list1</p>
    <p>list2</p>
    );

    ์œ„์™€ ๊ฐ™์ด ์ œ์ผ ์ฒ˜์Œ ์š”์†Œ๊ฐ€ sibling์ด๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ.
    ๋ฌด์กฐ๊ฑด ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ์•ผ ํ•œ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ฒซ ์š”์†Œ๋Š” ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ. <> </>๋„ okay!

    const right = (
    <div>
        <p>list1</p>
        <p>list2</p>
    </div>
    );

Rendering

html ์š”์†Œ(element), ๋˜๋Š” React ์š”์†Œ ๋“ฑ์˜ ์ฝ”๋“œ๊ฐ€ ๋ˆˆ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๊ทธ๋ ค์ง€๋Š” ๊ฒƒ์„ ๋ Œ๋”๋ง(rendering) ์ด๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค.

React ์š”์†Œ๊ฐ€ DOM node์— ์ถ”๊ฐ€๋˜์–ด ํ™”๋ฉด์— ๋ Œ๋”๋˜๋ ค๋ฉด ReactDOM.render ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž์—๋Š” JSX๋กœ React ์š”์†Œ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธฐ๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ Œ๋”ํ•˜๊ณ  ์‹ถ์€ container(๋ถ€๋ชจ์š”์†Œ)๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);

React๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” JSX ์‚ฌ์šฉ์ด ํ•„์ˆ˜๋Š” ์•„๋‹ˆ๋ผ๊ณ  ํ•˜์ง€๋งŒ ๋Œ€๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๋‹ˆ ๋ฆฌ์•กํŠธ์˜ ๊ธฐ๋ณธ ๋งˆํฌ์—… ์–ธ์–ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค๋Š” ๊ฒฐ๋ก !

profile
Ryuwisdom
post-custom-banner

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