๐Ÿ“š Study/JS, JQuery

JSX๋ž€? (์ •์˜, ์žฅ์ , ๋ฌธ๋ฒ•)

kkh1902 2022. 1. 24. 11:45
728x90
๋ฐ˜์‘ํ˜•

์•„๋ž˜ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ์‚ดํŽด๋ด…์‹œ๋‹ค.

const element = <h1>Hello, world!</h1>;

์œ„์— ํฌํ•œํ•œ ํƒœ๊ทธ ๋ฌธ๋ฒ•์€ ๋ฌธ์ž์—ด๋„, HTML๋„ ์•„๋‹™๋‹ˆ๋‹ค.

JSX๋ผ ํ•˜๋ฉฐ JavaScript๋ฅผ ํ™•์žฅํ•œ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. UI๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒจ์•ผ ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด React์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. JSX๋ผ๊ณ  ํ•˜๋ฉด ํ…œํ”Œ๋ฆฟ ์–ธ์–ด๊ฐ€ ๋– ์˜ค๋ฅผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, JavaScript์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

JSX๋Š” React “์—˜๋ฆฌ๋จผํŠธ(element)” ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” DOM์— ์–ด๋–ป๊ฒŒ ๋ Œ๋”๋งํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋ฅผ ๋ณด๋ฉด JSX๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ธฐ๋ณธ์‚ฌํ•ญ์„ ์ฐพ์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. JSX๋ž€?

React์—์„œ๋Š” ๋ณธ์งˆ์ ์œผ๋กœ ๋ Œ๋”๋ง ๋กœ์ง์ด UI ๋กœ์ง(์ด๋ฒคํŠธ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฐฉ์‹, ์‹œ๊ฐ„์— ๋”ฐ๋ผ state๊ฐ€ ๋ณ€ํ•˜๋Š” ๋ฐฉ์‹, ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ค€๋น„๋˜๋Š” ๋ฐฉ์‹ ๋“ฑ)๊ณผ ์—ฐ๊ฒฐ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ฐ›์•„๋“ค์ž…๋‹ˆ๋‹ค.

React๋Š” ๋ณ„๋„์˜ ํŒŒ์ผ์— ๋งˆํฌ์—…๊ณผ ๋กœ์ง์„ ๋„ฃ์–ด ๊ธฐ์ˆ ์„ ์ธ์œ„์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋Œ€์‹ , ๋‘˜ ๋‹ค ํฌํ•จํ•˜๋Š” “์ปดํฌ๋„ŒํŠธ”๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋Š์Šจํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋œ ์œ ๋‹›์œผ๋กœ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์„น์…˜์—์„œ ๋‹ค์‹œ ์ปดํฌ๋„ŒํŠธ๋กœ ๋Œ์•„์˜ค๊ฒ ์ง€๋งŒ, JS์— ๋งˆํฌ์—…์„ ๋„ฃ๋Š” ๊ฒŒ ์ต์ˆ™ํ•ด์ง€์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด ์ด์•ผ๊ธฐ๊ฐ€ ํ™•์‹ ์„ ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

React๋Š” JSX ์‚ฌ์šฉ์ด ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ๋žŒ์€ JavaScript ์ฝ”๋“œ ์•ˆ์—์„œ UI ๊ด€๋ จ ์ž‘์—…์„ ํ•  ๋•Œ ์‹œ๊ฐ์ ์œผ๋กœ ๋” ๋„์›€์ด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ React๊ฐ€ ๋”์šฑ ๋„์›€์ด ๋˜๋Š” ์—๋Ÿฌ ๋ฐ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

์ผ๋‹จ ํ•œ๋ฒˆ ์‹œ์ž‘ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

JSX์— ํ‘œํ˜„์‹ ํฌํ•จํ•˜๊ธฐ

์•„๋ž˜ ์˜ˆ์‹œ์—์„œ๋Š” name์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ํ›„ ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ JSX ์•ˆ์— ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

const name = 'Josh Perez';const element = <h1>Hello, {name}</h1>;
ReactDOM.render(
  element,
  document.getElementById('root')
);

JSX์˜ ์ค‘๊ด„ํ˜ธ ์•ˆ์—๋Š” ์œ ํšจํ•œ ๋ชจ๋“  JavaScript ํ‘œํ˜„์‹์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2 + 2, user.firstName ๋˜๋Š” formatName(user) ๋“ฑ์€ ๋ชจ๋‘ ์œ ํšจํ•œ JavaScript ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์‹œ์—์„œ๋Š” JavaScript ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ๊ฒฐ๊ณผ์ธ formatName(user)์„ <h1> ์—˜๋ฆฌ๋จผํŠธ์— ํฌํ•จํ–ˆ์Šต๋‹ˆ๋‹ค.

function formatName(user) {
  return user.firstName + ' ' + user.lastName;
}

const user = {
  firstName: 'Harper',
  lastName: 'Perez'
};

const element = (
  <h1>
    Hello, {formatName(user)}!  </h1>
);

ReactDOM.render(
  element,
  document.getElementById('root')
);

CodePen์—์„œ ์‹คํ–‰ํ•˜๊ธฐ

๊ฐ€๋…์„ฑ์„ ์ข‹๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด JSX๋ฅผ ์—ฌ๋Ÿฌ ์ค„๋กœ ๋‚˜๋ˆด์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ž๋™ ์„ธ๋ฏธ์ฝœ๋ก  ์‚ฝ์ž…์„ ํ”ผํ•˜๊ณ ์ž ๊ด„ํ˜ธ๋กœ ๋ฌถ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

JSX๋„ ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค

์ปดํŒŒ์ผ์ด ๋๋‚˜๋ฉด, JSX ํ‘œํ˜„์‹์ด ์ •๊ทœ JavaScript ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ๋˜๊ณ  JavaScript ๊ฐ์ฒด๋กœ ์ธ์‹๋ฉ๋‹ˆ๋‹ค.

์ฆ‰, JSX๋ฅผ if ๊ตฌ๋ฌธ ๋ฐ for loop ์•ˆ์— ์‚ฌ์šฉํ•˜๊ณ , ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ , ์ธ์ž๋กœ์„œ ๋ฐ›์•„๋“ค์ด๊ณ , ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function getGreeting(user) {
  if (user) {
    return <h1>Hello, {formatName(user)}!</h1>;  }
  return <h1>Hello, Stranger.</h1>;}

JSX ์†์„ฑ ์ •์˜

์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— ๋”ฐ์˜ดํ‘œ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const element = <div tabIndex="0"></div>;

์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— JavaScript ํ‘œํ˜„์‹์„ ์‚ฝ์ž…ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

const element = <img src={user.avatarUrl}></img>;

์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— JavaScript ํ‘œํ˜„์‹์„ ์‚ฝ์ž…ํ•  ๋•Œ ์ค‘๊ด„ํ˜ธ ์ฃผ๋ณ€์— ๋”ฐ์˜ดํ‘œ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ๋งˆ์„ธ์š”. ๋”ฐ์˜ดํ‘œ(๋ฌธ์ž์—ด ๊ฐ’์— ์‚ฌ์šฉ) ๋˜๋Š” ์ค‘๊ด„ํ˜ธ(ํ‘œํ˜„์‹์— ์‚ฌ์šฉ) ์ค‘ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๊ณ , ๋™์ผํ•œ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— ๋‘ ๊ฐ€์ง€๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

๊ฒฝ๊ณ 

JSX๋Š” HTML๋ณด๋‹ค๋Š” JavaScript์— ๊ฐ€๊น๊ธฐ ๋•Œ๋ฌธ์—, React DOM์€ HTML ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ด๋ฆ„ ๋Œ€์‹  camelCase ํ”„๋กœํผํ‹ฐ ๋ช…๋ช… ๊ทœ์น™์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, JSX์—์„œ class๋Š” className๊ฐ€ ๋˜๊ณ  tabindex๋Š” tabIndex๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

JSX๋กœ ์ž์‹ ์ •์˜

ํƒœ๊ทธ๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด XML์ฒ˜๋Ÿผ /> ๋ฅผ ์ด์šฉํ•ด ๋ฐ”๋กœ ๋‹ซ์•„์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

const element = <img src={user.avatarUrl} />;

JSX ํƒœ๊ทธ๋Š” ์ž์‹์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const element = (
  <div>
    <h1>Hello!</h1>
    <h2>Good to see you here.</h2>
  </div>
);

JSX๋Š” ์ฃผ์ž… ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค

JSX์— ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ์€ ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

const title = response.potentiallyMaliciousInput;
// ์ด๊ฒƒ์€ ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.
const element = <h1>{title}</h1>;

๊ธฐ๋ณธ์ ์œผ๋กœ React DOM์€ JSX์— ์‚ฝ์ž…๋œ ๋ชจ๋“  ๊ฐ’์„ ๋ Œ๋”๋งํ•˜๊ธฐ ์ „์— ์ด์Šค์ผ€์ดํ”„ ํ•˜๋ฏ€๋กœ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ช…์‹œ์ ์œผ๋กœ ์ž‘์„ฑ๋˜์ง€ ์•Š์€ ๋‚ด์šฉ์€ ์ฃผ์ž…๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ•ญ๋ชฉ์€ ๋ Œ๋”๋ง ๋˜๊ธฐ ์ „์— ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด XSS (cross-site-scripting) ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JSX๋Š” ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Babel์€ JSX๋ฅผ React.createElement() ํ˜ธ์ถœ๋กœ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ๋‘ ์˜ˆ์‹œ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

const element = (
  <h1 className="greeting">
    Hello, world!
  </h1>
);
const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, world!'
);

React.createElement()๋Š” ๋ฒ„๊ทธ๊ฐ€ ์—†๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋„๋ก ๋ช‡ ๊ฐ€์ง€ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

// ์ฃผ์˜: ๋‹ค์Œ ๊ตฌ์กฐ๋Š” ๋‹จ์ˆœํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค
const element = {
  type: 'h1',
  props: {
    className: 'greeting',
    children: 'Hello, world!'
  }
};

์ด๋Ÿฌํ•œ ๊ฐ์ฒด๋ฅผ “React ์—˜๋ฆฌ๋จผํŠธ”๋ผ๊ณ  ํ•˜๋ฉฐ, ํ™”๋ฉด์—์„œ ๋ณด๊ณ  ์‹ถ์€ ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œํ˜„์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. React๋Š” ์ด ๊ฐ์ฒด๋ฅผ ์ฝ์–ด์„œ, DOM์„ ๊ตฌ์„ฑํ•˜๊ณ  ์ตœ์‹  ์ƒํƒœ๋กœ ์œ ์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

์ฐธ์กฐ : https://ko.reactjs.org/docs/hello-world.html

 

Hello World – React

A JavaScript library for building user interfaces

ko.reactjs.org

# React ๊ฐœ๋… ์ž์Šต์„œ๊ฐ€ ์žˆ๋‹ค. ์ด๊ฑธ๋กœ ๊ธฐ๋ณธ ๊ฐœ๋… ๊ณต๋ถ€ ํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•