๐Ÿ“ JSX

JSX๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ XML์˜ ์•ฝ์ž.
์ฆ‰, JSX๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ™•์žฅํ•œ ๋ฌธ๋ฒ•์ด๋ฉฐ, ๊ทธ๋Ÿฐ ๋งŒํผ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ์‚ฌ์šฉ๋ฒ•์ด ๋งŽ์ด ๋น„์Šทํ•˜๋‹ค.
HTML์„ React์—์„œ ์‰ฝ๊ฒŒ ์“ฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

React๋Š” JSX ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด ๋งˆํฌ์—… ์–ธ์–ด์™€ ์ฝ”๋“œ์˜ ๋กœ์ง์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š๊ณ , ๋‘ ๊ฐ€์ง€๋ฅผ ํฌํ•จํ•œ "์ปดํฌ๋„ŒํŠธ"๋ฅผ ์‚ฌ์šฉ

 

๐Ÿ’ก ํ‘œํ˜„์‹

JSX์—์„œ๋Š” "์ค‘๊ด„ํ˜ธ( { } )"๋ฅผ ์ด์šฉํ•ด, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ‘œํ˜„์‹๋“ค์ด ์žˆ๋‹ค.

  • ๋จผ์ €, ์ค‘๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด, HTML ๋‚ด์— ๋ณ€์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
const name = 'elice';
const element = <h1> Hello, {name}! </h1>;
  • ์ค‘๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด, ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ๋„ฃ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
function greeting(){
  return "Hello, elice!";
}

const element = <h1>{greeting()}</h1>;
  • ํ•จ์ˆ˜ ๋‚ด์—์„œ ํ‘œํ˜„์‹์„ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
function formatGreeting(name){
  return "Hello" + ' ' + name;
}

function getGreeting(user) {
  return <h1>Hello, {formatGreeting("elice!")}!</h1>;
}

 

๐Ÿ’ก ์†์„ฑ

  • ํฐ ๋”ฐ์˜ดํ‘œ๋ฅผ ์ด์šฉํ•ด JSX์˜ ์†์„ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
const element = <a href = "https://kdt.elice.io/explore">์—˜๋ฆฌ์Šค๋กœ ์ด๋™</a>;

 

๐Ÿ’ก ์ž์‹ ์ •์˜

  • ์ž์‹ ํƒœ๊ทธ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ํฌํ•จ๋œ ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ์ž์‹ ํƒœ๊ทธ๋ฅผ ๋ถ€๋ชจ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค.
const element = (
  <div>
    <h1>Hello,</h1>
    <h2>elice!</h2>
  </div>
);

// <div> = ๋ถ€๋ชจ ํƒœ๊ทธ
// <h1>,<h2> = ์ž์‹ ํƒœ๊ทธ
  • ๋ชจ๋“  ํƒœ๊ทธ๋Š” ๋ฐ˜๋“œ์‹œ ๋‹ซํ˜€์•ผ ํ•œ๋‹ค. ๋‹ค๋งŒ, ์›๋ž˜ ํƒœ๊ทธ ์ด๋ฆ„์„ ๋งž์ถฐ์„œ </input> ์ฒ˜๋Ÿผ ๋‹ซ์•„์•ผ ํ•˜์ง€๋งŒ
    ์ด๋ฆ„์€ ์ƒ๋žตํ•˜๊ณ  />๋งŒ ์ด์šฉํ•ด์„œ ๋‹ซ์•„๋„ ๊ดœ์ฐฎ๋‹ค.
const element = <input type="text" />;

 

๐Ÿ’ก ๊ฐ์ฒด ํ‘œํ˜„

React.createElement( ) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด, JSX ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ , ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋‘ ์ฝ”๋“œ๋Š” ๋ฌธ๋ฒ•์ด ๋‹ค๋ฅด์ง€๋งŒ, ๊ฐ™์€ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋‹ค.

  • HTML ์ง์ ‘์ƒ์„ฑ
const element = (
  <h1 className="greeting">
    Hello, world!
  </h1>
);
  • React.createElement( ) ๋กœ ์ƒ์„ฑ
const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, world!'
);

 

๐Ÿ“ ์—˜๋ฆฌ๋จผํŠธ ๋ Œ๋”๋ง

Element ( ์—˜๋ฆฌ๋จผํŠธ ) : React ์•ฑ์˜ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„, ์ปดํฌ๋„ŒํŠธ(Component์˜ ๊ตฌ์„ฑ์š”์†Œ)

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, HTML ์š”์†Œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

Rendering( ๋ Œ๋”๋ง ) : ์ด๋Ÿฌํ•œ element๋ฅผ ํ™”๋ฉด์— ๊ทธ๋ฆฌ๋Š” ๊ฒƒ

 

๐Ÿ’ก ReactDOM๊ณผ ๋ Œ๋”๋ง

  • React๋Š” ๊ฐ€์ƒ(Virutal) DOM๊ณผ ์‹ค์ œ๋กœ, ํ‘œ์‹œ๋˜๋Š” DOM์„ ์œ ์ง€ํ•œ๋‹ค.
  • React๋Š” ์‹ค์ œ DOM์„ ์ถ”์ƒํ™”ํ•˜์—ฌ, ๊ฐ€์ƒ DOM์— ๋งŒ๋“ค์–ด๋‘๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜๋ฉด ํ•œ ๋ฒˆ์— ๋ Œ๋”๋งํ•œ๋‹ค.
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ, ๊ณ„์†ํ•ด์„œ DOM์„ ๋ Œ๋”๋งํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.

ReactDOM.render( ) : ๋ Œ๋”๋ง ํ•จ์ˆ˜

  • HTML ํŒŒ์ผ ์•ˆ์— Id๊ฐ€ root์ธ ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž.
<div id="root"></div>
  • ํ•ด๋‹น HTML์— ํ…์ŠคํŠธ๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด, ReactDOM.render( )์„ ์ด์šฉํ•ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ Œ๋”๋ง ํ•œ๋‹ค.
const element = <h1>Hello, elice</h1>;
ReactDOM.render(element, document.getElementById('root'));

// ReactDOM์— ๋ Œ๋”๋ง ํ• ๊บผ๋‹ค. element๋ฅผ, id = root ์ธ ์š”์†Œ์—๋‹ค๊ฐ€ ~

 

๐Ÿ’ก ์—˜๋ฆฌ๋จผํŠธ ์—…๋ฐ์ดํŠธ

  • ์—˜๋ฆฌ๋จผํŠธ๋Š” ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด, ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ถˆ๋ณ€ ๊ฐ์ฒด์ด๋‹ค.
  • ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์œผ๋ฉด, ์ƒˆ๋กœ์šด ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์—…๋ฐ์ดํŠธ ํ•ด์•ผ ํ•œ๋‹ค.

๋งŒ์•ฝ, ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๋งค ์ดˆ๋งˆ๋‹ค ๋ Œ๋”๋ง์„ ํ•ด์ค˜์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

function tick() {
  const element = (
    <div>
      <h1>{new Date().toLocaleTimeString()}</h1>
    </div>
  );
  ReactDOM.render(element, document.getElementById('root'));
}

setInterval(tick, 1000);

Date ๊ฐ์ฒด์˜ toLocaleTimeString( ) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด, ์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š”๋ฐ, ์‹ค์ œ๋กœ ์‹œ๊ฐ„์ด ๋ณ€ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” setInterval( ) ์ฝœ๋ฐฑ์„ ์ด์šฉํ•ด tick์„ ๊ณ„์† ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.
setInterval( )์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ 1000์€ 1์ดˆ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts