๐ŸŒฑ → ๐ŸŒณ

[React] JSX (Javascript XML) ๋ณธ๋ฌธ

Client/React

[React] JSX (Javascript XML)

BAY 2022. 9. 26. 15:25
728x90

๐Ÿ“Œ JSX(Javascript XML)

: Javascript์— XML์„ ์ถ”๊ฐ€ํ•œ ๋ฌธ๋ฒ•

๊ฐœ๋ฐœ์ž๋“ค์ด ๊ณผ๊ฑฐ DOM ๋ฐฉ์‹์œผ๋กœ html์„ ๊ทธ๋ฆฌ๋‹ค๋ณด๋‹ˆ ๋ฐ”๋กœ ๋‚ฉ๋“์ด ์•ˆ๊ฐ”์Œ -> ๊ทธ๋ž˜์„œ ๋งŒ๋“  ๋ฌธ๋ฒ•

๋ณดํ†ต ๋ฆฌ์•กํŠธ์—์„œ๋งŒ ์‚ฌ์šฉ 

html ๋ฌธ์„œ ๊ตฌ์กฐ๋ฅผ JS์—์„œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•จ -> ๋”ฐ๋ผ์„œ JS ๋‚ด๋ถ€์—์„œ๋„ html์„ ์งœ๋“ฏ ์ฝ”๋“œ ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•ด์ง 

 

๐Ÿ“ ๊ธฐ๋ณธ DOM ๋ฐฉ์‹์œผ๋กœ html ๊ทธ๋ฆฐ ์ฝ”๋“œ

function HelloWorldButton() {
  const [isClick, setClickState] = React.useState(false);
  const text = isClick ? "It's clicked" : "Hello, React world";
  
  return React.createElement(
    "button",
    { onClick: () => setClickState(!isClick) },
    React.createElement("div", null, React.createElement("span", null, text))
  );
}

const e = React.createElement;
const domContainer = document.querySelector("#app");
const root = ReactDOM.createRoot(domContainer);
root.render(e(HelloWorldButton));

 

๐Ÿ“ JSX๋กœ ์ง  ์ฝ”๋“œ

// ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ
function HelloWorldButton() {
  const [isClick, setClickState] = React.useState(false);
  const text = isClick ? "It's clicked" : "Hello, React world";
  return (
    <button onClick={() => setClickState(!isClick)}>
      <div>
        <span>{text}</span>
      </div>
    </button>
  );
}
const domContainer = document.querySelector("#app");
const root = ReactDOM.createRoot(domContainer);
root.render(<HelloWorldButton />);

 

JSX ๋ฌธ๋ฒ• 1. html์— class ๋„ฃ์„ ๋• className

JSX๋Š” ์ผ์ข…์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ผ์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์•ฝ์–ด์ธ class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ง‰ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋จ 

๊ทธ๋ž˜์„œ class="" ๋„ฃ๊ณ  ์‹ถ์œผ๋ฉด className์ด๋ผ๊ณ  ์จ์•ผํ•จ 

 

JSX ๋ฌธ๋ฒ• 2. ๋ณ€์ˆ˜๋ฅผ html์— ๊ฝ‚์•„๋„ฃ์„ ๋•Œ๋Š” {์ค‘๊ด„ํ˜ธ}

์˜›๋‚  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ• 

document.getElementById().innerHTML = ??

๋ฆฌ์•กํŠธ์—์„œ๋Š” ๋” ์‰ฝ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ 

function App(){

  let post = '๊ฐ•๋‚จ ์šฐ๋™ ๋ง›์ง‘';
  return (
    <div className="App">
      <div className="black-nav">
        <div>๋ธ”๋กœ๊ทธ์ž„</div>
        <div>{ post }</div>
      </div>
    </div>
  )
}

์ด๋ ‡๊ฒŒ ์ค‘๊ด„ํ˜ธ์•ˆ์— ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉํ•˜๊ณ  ์‹ถ์€ ๋ณ€์ˆ˜๋ช…๋งŒ ๋‹ด์œผ๋ฉด๋จ

 

์ฐธ๊ณ , ๋ณ€์ˆ˜์— ์žˆ๋˜๊ฑธ html์— ๊ฝ‚์•„๋„ฃ๋Š” ์ž‘์—… -> ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉ ์ด๋ผ๊ณ ํ•จ

 

JSX ๋ฌธ๋ฒ• 3. html์— style์†์„ฑ ๋„ฃ๊ณ ์‹ถ์œผ๋ฉด 

<div style="color : blue"> ์ด๋Ÿฐ๊ฑธ ๋„ฃ๊ณ  ์‹ถ์œผ๋ฉด

JSX ์ƒ์—์„œ๋Š” style={ } ์•ˆ์— { } ์ž๋ฃŒํ˜•์œผ๋กœ ์ง‘์–ด๋„ฃ์–ด์•ผํ•จ

์˜ˆ์‹œ)

<div style={ {color : 'blue', fontSize : '30px'} }> ๊ธ€์”จ </div>

{ ์†์„ฑ๋ช…: '์†์„ฑ๊ฐ’' } ์ด๋ ‡๊ฒŒ ๋„ฃ์œผ๋ฉด ๋จ

font-size ์ฒ˜๋Ÿผ ์†์„ฑ๋ช…์— ๋Œ€์‰ฌ๊ธฐํ˜ธ ์“ธ ์ˆ˜ ์—†์Œ 

๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๋ถ™์—ฌ์จ์•ผํ•˜๋ฉฐ ๋ถ™์—ฌ์“ธ ๋• ์•ž๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ์น˜ํ™˜

 

ํ•˜์ง€๋งŒ, JSX๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด์„œ๋Š” bebel์ด๋ผ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ(๋ฒˆ์—ญ๊ธฐ) ํ•„์š”

 

โœ… Babel(๊ตฌ six to five)

: JS์˜ ์ปดํŒŒ์ผ๋Ÿฌ

์˜ˆ์ „์— ES6 ๊ฐ€ ๋‚˜์˜ค๊ณ ๋‚˜์„œ ๋ช‡๋ช‡ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ES6๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„์„œ,

ES6 ๋ฌธ๋ฒ•๊ณผ ES5 ๋ฌธ๋ฒ•์˜ ์ถฉ๋Œ์ด ์žฆ์•„์„œ ES6 ๋ฌธ๋ฒ•์„ ES5 ๋ฌธ๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋˜ ๊ธฐ๋Šฅ์„ ํ•˜๋˜ ๊ธฐ๋Šฅ 

์š”์ฆ˜์€ ES6 ๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ ˆ๋ฒจ์—์„œ ์ง€์›์„ ๋งŽ์ด ํ•˜๋‹ค๋ณด๋‹ˆ, ๋‹ค๋ฅธ ์ถ”๊ฐ€์ ์ธ ์–ธ์–ด๋“ค์— ๋Œ€ํ•œ ์ปดํŒŒ์ผ๋Ÿฌ ์—ญํ• ํ•จ -> ๋Œ€ํ‘œ์ ์œผ๋กœ react

 

 

728x90

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

[React] useState vs useRef vs Variable  (0) 2022.09.30
[React] React.Fragment  (0) 2022.09.30