React/Velopert's React

[React] Study #3 | JSX์˜ ๊ธฐ๋ณธ๊ทœ์น™

yevdev 2021. 9. 29. 21:34

๐Ÿ’กJSX?

  • ๋ฆฌ์•กํŠธ์—์„œ ์ƒ๊น€์ƒˆ๋ฅผ ์ •์˜ํ•  ๋•Œ, ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•
  • HTML์ฒ˜๋Ÿผ ์ƒ๊ฒผ์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” JavaScript!
return <div>JSX๋ฅผ ์•Œ์•„๋ณด์ž!</div>;

๐Ÿ’ก JSX์˜ ๊ธฐ๋ณธ๊ทœ์น™

1๏ธโƒฃ ๊ผญ ๋‹ซํ˜€์•ผ ํ•˜๋Š” ํƒœ๊ทธ

import React from 'react'; 
import Hello from './Hello';  

function App() {
  return (
    <div>
      <Hello /> 
      <Hello /> 
      <Hello />
      
      /* ์—ฌ๊ธฐ์—์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ! ๊ผญ </div>๋กœ ๋‹ซ์•„์ฃผ์ž */
      <div>	
      
    </div>
  );
}

export default App;
  • HTML์—์„œ๋Š” input๊ณผ br ํƒœ๊ทธ๋ฅผ ๋‹ซ์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค! ํ•˜์ง€๋งŒ ๋ฆฌ์•กํŠธ์—์„œ๋Š” ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ๋จ!
  • Self Closing ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์ž!
  • /* ์ž˜๋ชป๋จ */
    <input>
    <br>
    
    /* Self Closing ํƒœ๊ทธ */
    <input />
    <br />

2๏ธโƒฃ ๊ผญ ๊ฐ์‹ธ์ ธ์•ผํ•˜๋Š” ํƒœ๊ทธ

  • ๋‘๊ฐœ ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ๋ฌด์กฐ๊ฑด ํ•˜๋‚˜๋กœ ๊ฐ์‹ธ์ ธ์•ผ ํ•œ๋‹ค
{/* ์ž˜๋ชป๋œ ๊ฒฝ์šฐ! */}
import React from 'react';
import Hello from './Hello';

function App() {
  return (
    <Hello />
    <div>๋‘๊ฐœ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ์–ด๋–ป๊ฒŒ?</div>
  );
}

export default App;


{/* ์˜ณ์€ ๊ฒฝ์šฐ : ๊ผญ ํƒœ๊ทธ ํ•˜๋‚˜๋กœ ํ•œ๋ฒˆ๋” ๊ฐ์‹ธ์ฃผ์ž */}
import React from 'react';
import Hello from './Hello';

function App() {
  return (
    <div>
      <Hello />
      <div>๋‘๊ฐœ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ์–ด๋–ป๊ฒŒ?</div>
     </div>
  );
}

export default App;

 

 

โ—๏ธ๊ทธ๋Ÿฐ๋ฐ, div๋กœ ๊ฐ์‹ธ๊ธฐ ์• ๋งคํ•˜๊ณ  ์‹ซ์„๋• Fragment ๋กœ ๊ฐ์‹ธ์ฃผ์žโ—๏ธ

  • ๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ ๋”ฐ๋กœ ๋ณ„๋„์˜ ์—˜๋ฆฌ๋จผํŠธ๋กœ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š”๋‹ค.
  • <> ... </> ํ˜•ํƒœ
import React from 'react';
import Hello from './Hello';

function App() {
  return (
    <>
      <Hello />
      <div>๋‘๊ฐœ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ์–ด๋–ป๊ฒŒ?</div>
    </>
  );
}

export default App;

 

3๏ธโƒฃ ์ฃผ์„

  • JSX ๋‚ด๋ถ€์˜ ์ฃผ์„ {/* ์š”๋ก ํ˜•ํƒœ */}
  • ์—ด๋ฆฌ๋Š” ํƒœ๊ทธ ๋‚ด๋ถ€์—์„œ๋Š” // 
import React from 'react'; 
import Hello from './Hello';  

function App() {
  return (
    <div>
      {/* ์ฃผ์„์€ ํ™”๋ฉด์— ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. */}
      /* ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ง€ ์•Š์œผ๋ฉด ํ™”๋ฉด์— ๋ณด์ธ๋‹ค */
      <Hello 
        // ์—ด๋ฆฌ๋Š” ํƒœ๊ทธ ๋‚ด๋ถ€์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ ๊ฐ€๋Šฅ
      /> 
      <Hello /> 
      <Hello /> 
    </div>
  );
}

export default App;

 

 


Reference

https://react.vlpt.us/