Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

React JSX ๋ฌธ๋ฒ•๊ณผ ์˜ˆ์ œ ์ •๋ฆฌ ๋ณธ๋ฌธ

React

React JSX ๋ฌธ๋ฒ•๊ณผ ์˜ˆ์ œ ์ •๋ฆฌ

kohi โ˜• 2022. 11. 9. 12:14

๐Ÿ”Ž JSX๋ž€?


  • JSX๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ™•์žฅ ๋ฌธ๋ฒ•์ด๋‹ค. React์—์„œ HTML์„ ํ‘œํ˜„ํ•  ๋•Œ JSX๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ณต์‹์ ์ธ ๋ฌธ๋ฒ•์€ ์•„๋‹ˆ๋‹ค.
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์ฝ”๋“œ๊ฐ€ ๋ฒˆ๋“ค๋ง๋˜๋Š” ๊ณผ์ •์—์„œ ๋ฐ”๋ฒจ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ HTML์„ ์ž‘์„ฑํ•˜๋“ฏ์ด ๋น„์Šทํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด JSX์˜ ๊ฐ€์žฅ ํฐ ์žฅ์ ์ด๋‹ค.

 

  • JSX๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ React Element
const element = React.createElement(
    "div", null, React.createElement(
      "a", null, "Hello World", ""
    )
  );

 

  • JSX ๋ฌธ๋ฒ• ์‚ฌ์šฉ
const element = (
    <div>
        <a> {"Hello World"} </a>
    </div>
);

 

๐Ÿ”Ž JSX ๋ฌธ๋ฒ•


๐Ÿ’ก ๋ฐ˜๋“œ์‹œ ๋ถ€๋ชจ ์š”์†Œ๋Š” ํ•œ ๊ฐœ์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
function App() {
    return (
        <div>
            <div>์ด๊ฒƒ์ด๋ฐ”๋กœ</div>
            <div>์˜ฌ๋ฐ”๋ฅธ๋ฐฉ๋ฒ•</div>
        </div>
    );
}

function App() {
    return (
        // div ์š”์†Œ๋ฅผ ์“ฐ๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ 1๋ฒˆ
        <Fragment>
            <div>์ด๊ฒƒ์ด๋ฐ”๋กœ</div>
            <div>์˜ฌ๋ฐ”๋ฅธ๋ฐฉ๋ฒ•</div>
        </Fragment>
    );
}

function App() {
    return (
        // div ์š”์†Œ๋ฅผ ์“ฐ๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ 2๋ฒˆ
        <>
            <div>์ด๊ฒƒ์ด๋ฐ”๋กœ</div>
            <div>์˜ฌ๋ฐ”๋ฅธ๋ฐฉ๋ฒ•</div>
        </>
    );
}
  • div ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ Fragment๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ๋‹ค. Fragment๋Š” <>๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ๊ฐ์‹ธ์•ผ ํ•˜๋Š” ์ด์œ  ⇒ virtual DOM์—์„œ ์ปดํฌ๋„ŒํŠธ ํšจ๊ณผ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ฐ์ง€ํ•˜์—ฌ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€๋Š” ํ•˜๋‚˜์˜ DOM ํŠธ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

๐Ÿ’ก JSX ์•ˆ์—์„œ๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ {}๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๋œ๋‹ค.
function App() {
    const id = "trustmitt";
    return (
        <div>
            <div>Hello</div>
            <div>{id}!</div>
        </div>
    );
}

 

 

๐Ÿ’ก if๋ฌธ, for๋ฌธ ๋Œ€์‹  ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. if๋ฌธ์ด๋‚˜ for๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” JSX ๋ฐ–์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.
function App() {
    const id = "trustmitt";
    return <div>{id == "trustmitt" ? <div>๋ฐ‹์ž…๋‹ˆ๋‹ค</div> : <div>์•„๋‹™๋‹ˆ๋‹ค</div>}</div>;
}

// if๋ฌธ ์‚ฌ์šฉ ์˜ˆ์ œ
function App() {
    const id = "trustmitt";
    if (id == "trustmitt") {
        item = <div>๋ฐ‹์ž…๋‹ˆ๋‹ค</div>;
    } else {
        item = <div>์•„๋‹™๋‹ˆ๋‹ค</div>;
    }
    return <div>{item}</div>;
}

 

๐Ÿ’ก ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๊ณ , ๋งŒ์กฑํ•˜์ง€ ์•Š์„ ๋•Œ ์•„์˜ˆ ์•„๋ฌด๊ฒƒ๋„ ๋ Œ๋”๋ง ํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ์ƒํ™ฉ์— && ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
function App() {
    const id = "trustmitt";
    return (
        <div>
            {/* "๋ฐ‹์ž…๋‹ˆ๋‹ค" ๋งŒ ๋ Œ๋”๋ง */}
            {id === "trustmitt" && <div>๋ฐ‹์ž…๋‹ˆ๋‹ค</div>}
            {id === "nottrustmitt" && <div>์•„๋‹™๋‹ˆ๋‹ค</div>}
        </div>
    );
}

 

๐Ÿ’ก ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ํ•จ์ˆ˜์—์„œ undefined๋งŒ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ Œ๋”๋ง ํ•˜๋Š” ์ƒํ™ฉ์„ ๋งŒ๋“ค๋ฉด ์•ˆ ๋œ๋‹ค.
function App() {
    // ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์ฝ”๋“œ
    const item = undefined;
    return item;
}

function App() {
    // JSX ๋‚ด๋ถ€์—์„œ undefined๋ฅผ ๋ Œ๋”๋ง ํ•˜๋Š” ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค
    const item = undefined;
    return <div>{item}</div>;
}

function App() {
    // OR ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์˜ค๋ฅ˜ ๋ฐฉ์ง€
    const item = undefined;
    return item || "undefined ์ž…๋‹ˆ๋‹ค";
}

 

๐Ÿ’ก ํด๋ž˜์Šค ์ •์˜ ์‹œ class๊ฐ€ ์•„๋‹Œ className์„ ์‚ฌ์šฉํ•œ๋‹ค.
function App() {
    return (
        <div>
            <div className="container"></div>
        </div>
    );
}

 

๐Ÿ”Ž Reference


https://im-designloper.tistory.com/79

https://www.nextree.io/jsxmunbeobgwa-seontaegjeog-rendeoring/

https://goddaehee.tistory.com/296

https://chanhuiseok.github.io/posts/react-3/

https://developerntraveler.tistory.com/54

'React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[React] props์™€ state  (0) 2022.12.15
REACT ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช… ์ฃผ๊ธฐ (Life Cycle)  (0) 2022.12.10
React :: COIN TRACKER  (0) 2022.11.29
React๋กœ ๊ฐ„๋‹จํ•œ To-Do-List ๊ตฌํ˜„  (0) 2022.11.24
React useState(), useEffect()  (0) 2022.11.15