[React] ๐ŸŽ ๊ธฐ์ดˆ์ •๋ฆฌ

๋…ธํ˜ธ์ค€ยท2023๋…„ 2์›” 22์ผ
0

๐Ÿฆ‹JSX

  • JS์•ˆ์—์„œ HTML์„ ์‰ฝ๊ฒŒ ์“ธ์ˆ˜์žˆ๋Š” ๋ถ€๊ฐ€๊ธฐ๋Šฅ

๐Ÿฆ‹ JSX๋ฌธ๋ฒ• 3๊ฐ€์ง€

function App() {

  let post =  '๊ฐ•๋‚จ ์šฐ๋™ ๋ง›์ง‘';

  return (
    <div className="App">
      <div className="black-nav">
        <h4 style={{color:'red',fontSize:'16px'}}>๋ธ”๋กœ๊ทธ์ž„</h4>
      </div>
      <h4>{post}</h4>
    </div>
  );
}

export default App;

1.class๋Œ€์‹  className์œผ๋กœ ์“ด๋‹ค.
2.html์— ๋ฐ์ดํ„ฐ๊ฝ‚์•„๋„ฃ์„ ๋•Œ(๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ)๋Š” {๋ณ€์ˆ˜๋ช…}
3.style๋„ฃ์„๋•Œ๋Š” style={{color:'red',...}}

  • return์•ˆ์—๋Š” ๋ณ‘๋ ฌ๋กœ ํƒœ๊ทธ 2๊ฐœ์ด์ƒ ๊ธฐ์ž…๊ธˆ์ง€ return(<></><></>) ์ด๋Ÿฌ๋ฉด ์•ˆ๋จ

๐Ÿฆ‹ state

function App() {

  let post =  '์—ญ์‚ผ ์šฐ๋™ ๋ง›์ง‘';
  let titleArr = ['๋‚จ์ž์ฝ”ํŠธ ์ถ”์ฒœ','๊ฐ•๋‚จ ์šฐ๋™๋ง›์ง‘','ํŒŒ์ด์ฌ๋…ํ•™']
  let [title,setTitle] = useState(titleArr);
  let [logo, setLogo] = useState('ReactBlog');

return (
    <div className="App">
      <div className="black-nav">
        <h4>ReactBlog</h4>
      </div>
        {title.map(el => 
          <div className='list'>
            <h4>{el}</h4>
            <p>2์›” 17์ผ ๋ฐœํ–‰</p>
          </div>
        )}
    </div>
  );
}
  • ์ž๋ฃŒ๋ฅผ ์ž ๊น ์ €์žฅํ• ๋• ๋ณ€์ˆ˜๋ฅผ ์“ด๋‹ค.
  • ๊ทผ๋ฐ ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค๊ณ  return์•ˆ์— html์ด ๋ฆฌ๋ Œ๋”๋ง๋˜์ง„ ์•Š๋Š”๋‹ค.
  • ๋ณ€์ˆ˜๋ฅผ ๋ฐ”๊ฟ€๋•Œ๋งˆ๋‹ค html์ด ๋ฆฌ๋ Œ๋”๋ง๋˜๊ฒŒ ํ•˜๊ณ ์‹ถ์œผ๋ฉด state๋ฅผ ์จ์•ผํ•œ๋‹ค.
  • App() ์•ˆ์—์„œ useState ์น˜๊ณ  ์—”ํ„ฐ์น˜๋ฉด ์•Œ์•„์„œ import๋œ๋‹ค.

๐Ÿฆ‹ onClick, setState

/* eslint-disable */ 
import logo from './logo.svg';
import './App.css';
import { useState } from 'react';

function App() {
  let post =  '์—ญ์‚ผ ์šฐ๋™ ๋ง›์ง‘';
  let [๊ธ€์ œ๋ชฉ,๊ธ€์ œ๋ชฉ๋ณ€๊ฒฝ] = useState(['๋…๊ฐ','๊ฐ•๋‚จ ์šฐ๋™๋ง›์ง‘','ํŒŒ์ด์ฌ๋…ํ•™']);
  let [๋”ฐ๋ด‰,๋”ฐ๋ด‰๋ณ€๊ฒฝ] = useState(0);
// state๋ณ€๊ฒฝํ•จ์ˆ˜ ํŠน์ง• : ๊ธฐ์กด state์™€ ์‹ ๊ทœ state๊ฐ€ ๊ฐ™์œผ๋ฉด ๋ณ€๊ฒฝ์•ˆํ•ด์คŒ
// array/object ํŠน์ง• : ๋ณ€์ˆ˜์—” ์ฃผ์†Œ๊ฐ’๋งŒ ์ €์žฅ๋จ. ๊ฐ’์€ ram์— ์ €์žฅ๋จ
return (
    <div className="App">
      <div className="black-nav">
        <h4>ReactBlog</h4>
      </div>

      <button>๊ฐ€๋‚˜๋‹ค์ˆœ์ •๋ ฌ</button>
      <button onClick={() => {
        let copy = [...๊ธ€์ œ๋ชฉ];
        copy[0] = '์—ฌ์ž์ฝ”ํŠธ ์ถ”์ฒœ';
        ๊ธ€์ œ๋ชฉ๋ณ€๊ฒฝ(copy);
      }}>๊ธ€์ˆ˜์ •</button>
      <div className='list'>
        <h4>{๊ธ€์ œ๋ชฉ[0]} <span onClick={() => { ๋”ฐ๋ด‰๋ณ€๊ฒฝ(๋”ฐ๋ด‰+1)}}>๐Ÿ‘</span> {๋”ฐ๋ด‰} </h4>
        <p>2์›” 17์ผ ๋ฐœํ–‰</p>
      </div>
      <div className='list'>
        <h4>{๊ธ€์ œ๋ชฉ[1]}</h4>
        <p>2์›” 17์ผ ๋ฐœํ–‰</p>
      </div>
      <div className='list'>
        <h4>{๊ธ€์ œ๋ชฉ[2]}</h4>
        <p>2์›” 17์ผ ๋ฐœํ–‰</p>
      </div>
      <div className="modal">
        <h4>์ œ๋ชฉ</h4>
        <p>๋‚ ์งœ</p>
        <p>์ƒ์„ธ๋‚ด์šฉ</p>
      </div>

    </div>
  );
}

export default App;
  1. eslint-disable ๋กœ warning ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. ๋ˆŒ๋ €์œผ๋ฉด ์ข‹๊ฒ ๋Š” ํƒœ๊ทธ์•ˆ์— onClick์„ ๋„ฃ๋Š”๋‹ค
  • onClick={}์•ˆ์—” ํ•จ์ˆ˜์ด๋ฆ„์„ ๋„ฃ์–ด์•ผํ•œ๋‹ค.
  • onClick์•ˆ์—๋‹ค๊ฐ€ ํ•จ์ˆ˜๋งŒ๋“ค๊ธฐ ๊ท€์ฐฎ์„ ๋• () => {js์ฝ”๋“œ} ๋กœ ๋„ฃ๊ณ ๋Š” ํ•จ
  1. state๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด setState(์ƒˆ๋กœ์šดstate) (๋”ฐ๋ด‰=1) ์ด๋Ÿฐ ์žฌํ• ๋‹น์€ ์•ˆ๋จ, state๋ฅผ ๊ฐˆ์•„์น˜์šด๋‹ค๋Š” ๋Š๋‚Œ์œผ๋กœ
  2. state๋ณ€๊ฒฝํ•จ์ˆ˜ ํŠน์ง• : ๊ธฐ์กด state์™€ ์‹ ๊ทœ state๊ฐ€ ๊ฐ™์œผ๋ฉด ๋ณ€๊ฒฝ์•ˆํ•ด์คŒ
  3. array/object ํŠน์ง• : ๋ณ€์ˆ˜์—” ์ฃผ์†Œ๊ฐ’๋งŒ ์ €์žฅ๋จ. ๊ฐ’์€ ram์— ์ €์žฅ๋จ

๐Ÿฆ‹ Component ๋ฌธ๋ฒ•

      <div className="modal">
        <h4>์ œ๋ชฉ</h4>
        <p>๋‚ ์งœ</p>
        <p>์ƒ์„ธ๋‚ด์šฉ</p>
      </div>

์ด๋Ÿฐ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด๊ธธ์–ด์ง€๋ฉด html์ด ๋”๋Ÿฌ์›Œ์ง„๋‹ค.

	<Modal></Modal>

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ์ข‹๊ฒ ๋‹ค

/* eslint-disable */ 
import './App.css';
import { useState } from 'react';

function App() {
  
  let [๊ธ€์ œ๋ชฉ,๊ธ€์ œ๋ชฉ๋ณ€๊ฒฝ] = useState(['๋…๊ฐ','๊ฐ•๋‚จ ์šฐ๋™๋ง›์ง‘','ํŒŒ์ด์ฌ๋…ํ•™']);
  let [๋”ฐ๋ด‰,๋”ฐ๋ด‰๋ณ€๊ฒฝ] = useState(0);

return (
    <div className="App">
      <div className="black-nav">
        <h4>ReactBlog</h4>
      </div>

      <button>๊ฐ€๋‚˜๋‹ค์ˆœ์ •๋ ฌ</button>
      <button onClick={() => {
        let copy = [...๊ธ€์ œ๋ชฉ];
        copy[0] = '์—ฌ์ž์ฝ”ํŠธ ์ถ”์ฒœ';
        ๊ธ€์ œ๋ชฉ๋ณ€๊ฒฝ(copy);
      }}>๊ธ€์ˆ˜์ •</button>
      <div className='list'>
        <h4>{๊ธ€์ œ๋ชฉ[0]} <span onClick={() => { ๋”ฐ๋ด‰๋ณ€๊ฒฝ(๋”ฐ๋ด‰+1)}}>๐Ÿ‘</span> {๋”ฐ๋ด‰} </h4>
        <p>2์›” 17์ผ ๋ฐœํ–‰</p>
      </div>
      <div className='list'>
        <h4>{๊ธ€์ œ๋ชฉ[1]}</h4>
        <p>2์›” 17์ผ ๋ฐœํ–‰</p>
      </div>
      <div className='list'>
        <h4>{๊ธ€์ œ๋ชฉ[2]}</h4>
        <p>2์›” 17์ผ ๋ฐœํ–‰</p>
      </div>
      <Modal></Modal>
    </div>
  );
}

function Modal(){
  return (
    <div className="modal">
      <h4>์ œ๋ชฉ</h4>
      <p>๋‚ ์งœ</p>
      <p>์ƒ์„ธ๋‚ด์šฉ</p>
    </div>
  )
}

export default App;

Component ๋งŒ๋“œ๋Š”๋ฒ•
1. function๋งŒ๋“ค๊ณ (๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ ๋ฐ–์— ๋งŒ๋“ค์–ด์•ผํ•จ)
2. return()์•ˆ์— html๋‹ด๊ธฐ
3. <ํ•จ์ˆ˜๋ช…></ํ•จ์ˆ˜๋ช…> ์“ฐ๊ธฐ <ํ•จ์ˆ˜๋ช… />๋„ ๊ฐ€๋Šฅ

์–ด๋–ค๊ฑธ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค๋ฉด ์ข‹์€๊ฐ€
0. const Modal = () => {return ~} ๋กœ ๋งŒ๋“ค์–ด๋„ ๋จ
1. ๋ฐ˜๋ณต์ ์ธ html ์ถ•์•ฝํ•  ๋•Œ
2. ํฐ ํŽ˜์ด์ง€ ํ•˜๋‚˜๋ฅผ ์ปดํฌ๋„ŒํŠธํ™” ํ• ๋•Œ
3. ์ž์ฃผ๋ณ€๊ฒฝ๋˜๋Š” ui๊ฐ™์€๊ฒƒ

๊ตณ์ด html์„ ๋ณ‘๋ ฌ๊ธฐ์ž…ํ•˜๋ ค๋ฉด <> </>์œผ๋กœ div๋ฅผ ๋ณ‘๋ ฌ๋กœ ๋‘˜์ˆ˜์žˆ์Œ
์ปดํฌ๋„ŒํŠธํ™”ํ•˜๋Š”๊ฒŒ ๋‹จ์ ์€ ์žˆ์Œ
App()์—์„œ ์„ ์–ธํ•œ state๋ฅผ Modal()์—์„  ๋ชป์”€
๋„ˆ๋ฌด ๋ณต์žกํ•˜๋‹ค ์‹ถ์€๊ฒƒ๋งŒ ์ปดํฌ๋„ŒํŠธํ™”ํ•˜๋„๋ก ํ•˜์ž

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