Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[React] React ๊ธฐ๋ณธ - JSX ๋ฌธ๋ฒ• ๋ณธ๋ฌธ

๐Ÿง‘‍๐Ÿ’ป Web/React

[React] React ๊ธฐ๋ณธ - JSX ๋ฌธ๋ฒ•

Lennon 2022. 4. 12. 23:33
728x90
๋ฐ˜์‘ํ˜•

JSX ๋ฌธ๋ฒ•์ด๋ž€?

๋ฆฌ์•กํŠธ์—์„œ ์ƒ๊น€์ƒˆ๋ฅผ ์ •์˜ํ•  ๋•Œ, ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•์ด๋‹ค. HTML๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” JavaScript์ด๋‹ค!

const App = () => {
  return <div>Hello React!</div>
}

์‹ค์ œ JavaScript๊ฐ€ ์•„๋‹Œ๋ฐ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•˜๋Š” ๊ฑธ๊นŒ?

 

์—ฌ๊ธฐ์„œ Babel์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

Babel์€ JSX๋ฅผ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก JavaScript๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜ JSX๊ฐ€ JavaScript๋กœ ์˜ค๋ฅ˜ ์—†์ด ์ œ๋Œ€๋กœ ๋ณ€ํ™˜๋˜๋ ค๋ฉด ๊ทœ์น™์„ ์ง€์ผœ์•ผ ํ•œ๋‹ค.

 

1. ํƒœ๊ทธ๋Š” ๊ผญ ๋‹ซํ˜€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

const App = () => {
  return ( 
  <div>
    <h1>Hello</h1>
    <br>
    <input>
  </div>
  )
}

<br> , <input> ๋“ฑ ๋ณธ๋ž˜ HTML์—์„œ ๋‹ซํ˜€์žˆ๋Š” ํƒœ๊ทธ๋Š” JSX์—์„œ ๋‹ซ๋Š” ํƒœ๊ทธ๊ฐ€ ์•„๋‹ˆ๋‹ค!

๋‹น์—ฐํžˆ ํƒœ๊ทธ ์ง์ด ๋งž์ง€ ์•Š์•„๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค ( div์˜ ๋‹ซ๋Š” ํƒœ๊ทธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋“ฑ๋“ฑ.. )

const App = () => {
  return ( 
  <div>
    <h1>Hello</h1>
    <br />
    <input />
  </div>
  )
}

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

2. ํƒœ๊ทธ๋Š” ๊ฐ์‹ธ์ ธ์•ผ ํ•œ๋‹ค!

const App = () => {
  return ( 
  <div>
    <h1>Hello</h1>
  </div>
  
  <div>
    <h1>Bye</h1>
  </div>
  )
}

์•„๋งˆ ์œ„ ์ฝ”๋“œ๋Š” ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ฌ ๊ฒƒ์ด๋‹ค.

๋‘ ๊ฐœ์˜ ํƒœ๊ทธ๊ฐ€ ํ˜•์ œ ์œ„์น˜์— ์žˆ์„ ๋•Œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋Š” ์ตœ์ƒ๋‹จ์˜ ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

const App = () => {
  return ( 
  <>
      <div>
        <h1>Hello</h1>
      </div>

      <div>
        <h1>Bye</h1>
      </div>
  </>
  )
}

<div>, <main>, <article> ๋“ฑ๋„ ๋‹น์—ฐํžˆ ๋˜๋ฉฐ, ๋นˆ ํƒœ๊ทธ๋กœ๋„ ๋™์ž‘ํ•˜๋‹ˆ ์ฐธ๊ณ ํ•˜์ž.

 

3. ๋‚ด๋ถ€์˜ JavaScript ๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์ฃผ์ž!

const App = () => {
  const value = "Hello";
  return ( 
  <>
      <div>
        <h1>{value}</h1>
      </div>

      <div>
        <h1>Bye</h1>
      </div>
  </>
  )
}

return ๋‚ด๋ถ€์—์„œ JavaScript๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ {}๋กœ ๊ผญ ๋ฌถ์–ด์ฃผ์ž!

4. html๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฌธ๋ฒ•

const App = () => {
  return <div class="hi">Hello React!</div>
}

์šฐ๋ฆฌ์—๊ฒŒ ์ต์ˆ™ํ•œ ์œ„ ์ฝ”๋“œ๋Š” ์ฝ˜์†”์—์„œ "ํ˜น์‹œ HTML์„ ์ž‘์„ฑ์ค‘์ด์‹ ๊ฐ€์š”?" ๋ผ๋ฉฐ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ฌ ๊ฒƒ์ด๋‹ค. 

JSX ๋ฌธ๋ฒ•์—์„œ class๋Š” className์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค! 

์ฐธ๊ณ ๋กœ input์— ์ ์šฉํ•˜๋Š” label ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ for ์†์„ฑ๋„ htmlFor๋กœ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค!

728x90
๋ฐ˜์‘ํ˜•
Comments