๐Ÿ”ฎ JSX ์ •๋ฆฌ

๊ถŒ๊ทœ๋ฆฌยท2023๋…„ 5์›” 17์ผ
0

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปFrontEnd

๋ชฉ๋ก ๋ณด๊ธฐ
21/29
post-thumbnail

01. JSX๋ž€?

01. JSX ๋ฌธ๋ฒ•

JavaScript XML์˜ ์•ฝ์ž๋กœ, ๋ฆฌ์•กํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ํ•œ ๋ฌธ๋ฒ•์ด๋‹ค. JSX๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฌธ๋ฒ•์„ ํ™•์žฅํ•ด์ฃผ๋Š” Babel์„ ํ†ตํ•ด JavaScript๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.


02. JSX ๊ทœ์น™

JSX ๊ฐ€ JavaScript ๋กœ ์ œ๋Œ€๋กœ ๋ณ€ํ™˜์ด ๋˜๋ ค๋ฉด ์ง€์ผœ์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ช‡๊ฐ€์ง€ ๊ทœ์น™์ด ์žˆ๋‹ค.

  1. ์นด๋ฉœ์ผ€์ด์Šค ์ค€์ˆ˜
  2. class์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ˆ์•ฝ์–ด๋กœ, className์œผ๋กœ ์ฝ”๋“œ ์ž‘์„ฑ
  3. ํƒœ๊ทธ๋Š” ๊ผญ ๋‹ซ์•„์•ผ ํ•œ๋‹ค.
    <main> /* ์—๋Ÿฌ ๋ฐœ์ƒ */
    
    <main> </main> /* ์ •์ƒ ์ž‘๋™ */

    ๐Ÿ“ Self Closing tag

    ํƒœ๊ทธ ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐ”๋กœ ๋‹ซ์„ ์ˆ˜ ์žˆ๋‹ค.
     <main> </main>  /* ์ •์ƒ ์ž‘๋™ */
    
     <main /> /* ์ •์ƒ ์ž‘๋™ */
  4. ์ตœ์ƒ์œ„ ํƒœ๊ทธ๋Š” ๊ผญ 1๊ฐœ์—ฌ์•ผ ํ•œ๋‹ค.
    ์ด์œ ๋Š”, ๋ฆฌ์•กํŠธ๊ฐ€ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ๋งŒ์„ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    function App() {
    	return (
    		<div>
    			<h1>Hello GYUUUL 1!</h1>
    			<h1>Hello GYUUUL 2!</h1>
    		</div>
    );
    }

03. Fragment

JSX ๊ทœ์น™ ์ค‘ 4๋ฒˆ์€ ๋ณด๋ฉด "์ตœ์ƒ์œ„ ํƒœ๊ทธ๋Š” ๊ผญ 1๊ฐœ์—ฌ์•ผ ํ•œ๋‹ค."๋ผ๋Š” ๊ทœ์น™์ด๋‹ค.
ํ•˜์ง€๋งŒ ๊ฐ์‹ธ๋Š” ๋ชฉ์ ์œผ๋กœ๋งŒ ๊ณ„์† div๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง€๊ณ  table ๊ด€๋ จ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ์• ๋งคํ•ด์ง„๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜์˜จ ๊ฒƒ์ด ๋ฆฌ์•กํŠธ์˜ Fragment์ด๋‹ค.

  <Fragment>

  <header>
    <h1>Hello GYUUUL 1</h1>
  </header>
  <main>
    <h1>Hello GYUUUL 2</h1>
  </main>

</Fragment>
  <>

  <header>
    <h1>Hello GYUUUL 1</h1>
  </header>
  <main>
    <h1>Hello GYUUUL 2</h1>
  </main>

</>

Fragment๋Š” ์ฒซ ๋ฒˆ์งธ์ฒ˜๋Ÿผ Fragment ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋‘ ๋ฒˆ์งธ์ฒ˜๋Ÿผ ๋นˆ ํƒœ๊ทธ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


04. JSX ์Šคํƒ€์ผ ์ ์šฉ

JSX์— CSS ๋ฌธ๋ฒ•์„ ์ ์šฉ์‹œํ‚ฌ ๋• ๋ฐฉ๋ฒ•์ด ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

1. src ํด๋”์— App.css ํŒŒ์ผ ์ƒ์„ฑ
2. JSX์— style ์†์„ฑ ์ ์šฉ

์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•œ๋‹ค๋ฉด ๊ธฐ์กด CSS์— ์Šคํƒ€์ผ ์†์„ฑ ์ž‘์—…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ํ•˜๋ฉด ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ์Šคํƒ€์ผ์„ ์ •์˜ํ•  ๋•Œ '-'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— c์นด๋ฉœ ์ผ€์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

  <div style={{backgroundColor:"black", color:"white"}}></div>

์ด์™€ ๊ฐ™์ด background-color ๋ผ๊ณ  ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  backgroundColor ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•œ๋‹ค.


๐Ÿšจ HTML์— style์„ ์ ์šฉํ•  ๋•Œ์™€ JSX์— style์„ ์ ์šฉํ•  ๋•Œ ์ฐจ์ด์ 

  • HTML์— style์„ ์ ์šฉํ•  ๋•Œ
    <h1 style='background-color:black; color:white' >Hello GYUUUL! </h1>
  • JSX์— style์„ ์ ์šฉํ•  ๋•Œ
    ๐Ÿ“ข ์ค‘๊ด„ํ˜ธ๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•ด์•ผํ•จ
    <h1 style={{backgroundColor:"black", color:"white"}}> Hello GYUUUL! </h1>

์ธ๋ผ์ธ ์Šคํƒ€์ผ

in-line ์Šคํƒ€์ผ๋กœ ์ ์šฉํ•  ๋•Œ ์˜ค๋ธŒ์ ํŠธ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์˜ค๋ธŒ์ ํŠธ ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด key์™€ value๋ฅผ ์ง์„ ์ง€์–ด ์ž‘์„ฑํ•œ๋‹ค.

function App() {
  const name = 'react';
  const style = {
    backgroundColor: 'black',
    color: 'aqua',
    fontSize: 24, // ๊ธฐ๋ณธ ๋‹จ์œ„ px
    padding: '1rem' // ๋‹ค๋ฅธ ๋‹จ์œ„ ์‚ฌ์šฉ ์‹œ ๋ฌธ์ž์—ด๋กœ ์„ค์ •
  }

05. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ

JSX์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ณ€์ˆ˜๋ช…์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ค˜์•ผ ํ•œ๋‹ค.

function App() {
	const name = 'GYUUUL';
  return (
		<div>
			<h1 style={{backgroundColor:"black", color:"white"}}>
				Hello {name} !
			</h1>
		</div>
  );
}

export default App;

// Hello GYUUUL ! ์ถœ๋ ฅ

์Šคํƒ€์ผ๋„ ๋ณ€์ˆ˜์— ๋‹ด์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import './App.css';

function App() {
  const name = 'GYUUUL';
  const someStyle = {backgroundColor:"black", color:"white"}; //์นด๋ฉœ์ผ€์ด์Šค ์œ ์ง€
  return (
    <div>
      <h1 style={someStyle}>
        Hello {name} !
      </h1>
  </div>
  );
}
export default App;

์ง์ ‘ ํ•ด๋ณด๊ธฐ ( 1 )

๐Ÿ“ ์ƒˆ๋กœ๊ณ ์นจ ํ•  ๋•Œ๋งˆ๋‹ค ํ˜„์žฌ์‹œ๊ฐ„ ํ‘œ์‹œ ๋˜๋Š” ๊ฑธ JSX๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ

import './App.css';

function App() {
 const someStyle = {backgroundColor:"black", color:"white"};

  let now = new Date();	// ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„
  let year = now.getFullYear();
  let month = now.getMonth()+1;	
  let date = now.getDate();
  let hours = now.getHours();
  let minutes = now.getMinutes()
  let seconds = now.getSeconds();

  return (
  <div>
      <h1 style={{color:'red'}}>๋…„: {year} </h1 >
      <h1 style={someStyle}>์›”/์ผ: {month}/{date}</h1>
      <h1 style={someStyle}>์‹œ๊ฐ„: {hours}์‹œ {minutes}๋ถ„ {seconds}์ดˆ</h1>
  </div>
  );
}

export default App;
  • npm start ํ›„ localhost:3000 ์ ‘์† ๊ฒฐ๊ณผ

์ง์ ‘ ํ•ด๋ณด๊ธฐ ( 2 )

๐Ÿ“ ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— ์žˆ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ’์ด true๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰ ์ง€์ •ํ•ด๋ณด๊ธฐ

  1. src ํด๋”์— components ํด๋” ์ƒ์„ฑ ํ›„ TripList.jsx์™€ TripList.css ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

  2. TripList.jsx์™€ TripList.css์— ๊ฐ๊ฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

  • TripList.jsx
    import './TripList.css'
    
    let list = [
        { no: 1, area: "๋Œ€์ „", visited: false },
        { no: 2, area: "๋ถ€์‚ฐ", visited: true },
        { no: 3, area: "๋ชฉํฌ", visited: false },
        { no: 4, area: "์ œ์ฃผ๋„", visited: true },
    ];
    
    function TripList(){
    
        let areas = list.map((item, index)=>{
            return(
            // mapํ•œ ๊ฒฐ๊ณผ๊ฐ€ areas์— ์ €์žฅ๋œ ํ›„ ๋ฐ˜ํ™˜
            <li key={index} className={item.visited? "list-area-item active":"list-area-item"}>{item.area}</li>
            )// ํ˜•์ œ ์š”์†Œ๋“ค์€ ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ํ‚ค๊ฐ’์„ ๊ฐ€์ ธ์•ผํ•จ= ์ตœ์ ํ™” ๊ด€๋ จ
            // visited๊ฐ€ true๋ฉด ํด๋ž˜์Šค๋ช…์ด list-area-item active, false๋ฉด list-area-item์ด๋‹ค.
        });
    
        return <ul className="list-area">{areas}</ul>   
    }
    
    export default TripList;

  • TripList.css
    .list-area {
        padding: 0;
        list-style: none;
    }
    .list-area-item {
        border: 1px solid black;
    }
    /* ์„ ์ด ๊ฒน์ณ์„œ 2px์ด ๋˜๋Š” ๊ฑธ ๋ง‰์•„์คŒ */
    .list-area-item+.list-area-item {
        border-top: none;
    } 
    .list-area-item.active {
        background-color: royalblue;
        color: aliceblue;
    }

  • App.js
    import './App.css';
    import TripList from './components/tripList/TripList'
    //์›นํŒฉ๋•Œ๋ฌธ์— cssํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Œ
    
    function App() {
    
      return (
        <div>
          <TripList />
      	</div>
      );
    }
    export default App;

  • npm start ํ›„ localhost:3000 ์ ‘์† ๊ฒฐ๊ณผ

profile
๊ธฐ๋ก์žฅ ๐Ÿ“

0๊ฐœ์˜ ๋Œ“๊ธ€