๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/React

[React] 3. ์ปดํฌ๋„ŒํŠธ (Component)

by KASSID 2023. 5. 28.

๋ชฉ์ฐจ

    728x90

    ์ด์ „ ๊ธ€ โ–ผ

    1. Component ๋ฌธ๋ฒ•

    html์„ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋ฉด ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋ธ”๋ก ๋‹จ์œ„๋กœ ์ œ์ž‘ํ•˜๊ณ  ์‹ถ์€ ๋ถ€๋ถ„์ด ์žˆ๋‹ค.

     

    React์—์„œ๋Š” ๊ธด HTML ๋ธ”๋ก์„ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์น˜ํ™˜ํ•˜๋„๋ก ํ•˜๋Š” ๋ฌธ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

    ์ด๊ฒƒ์ด ๋ฐ”๋กœ Component์ด๋‹ค.

     

    ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ HTML ๋ฉ์–ด๋ฆฌ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์น˜ํ™˜ํ•˜๊ณ  ์›ํ•˜๋Š” ๊ณณ์— ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

     

    ์žฅ์ 

    1. ๋‹ค๋ฅธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ถ€๋ถ„์ธ์ง€ ํ•œ ๋ˆˆ์— ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. (๋ฌผ๋ก  ์ž์‹ ์ด ๋‹ค์‹œ ๋ณผ ๋•Œ๋„ ์œ ์šฉํ•˜๋‹ค!)

    2. ๋ฐ˜๋ณต์ ์ธ html์„ ์ถ•์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

    3. ํฐ ํŽ˜์ด์ง€ ํ•˜๋‚˜๋ฅผ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (์ƒ์„ธ ํŽ˜์ด์ง€ ๋“ฑ)

    4. ๋งค์šฐ ์ž์ฃผ ๋ณ€๊ฒฝ๋  UI๋ฅผ ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด ์„ฑ๋Šฅ์ƒ ์ด์ ์„ ์ทจํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    5. ํ˜‘์—… ์‹œ ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„๋กœ ๋ถ„๋ฐฐํ•˜๋ฉด ํŽธํ•˜๋‹ค!

     

    ๋‹จ์ 

    state๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. (๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ๋งŒ๋“  local ๋ณ€์ˆ˜๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ!)

    ์ด๋•Œ์—๋Š” props๋กœ ์ „๋‹ฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

     

    ์ปดํฌ๋„ŒํŠธ๋Š” ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค!

    1) Component ์ œ์ž‘

    (1) ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ์ •์˜

    function ์ปดํฌ๋„ŒํŠธ๋ช…(){
      return(
        ... //ํ•˜๋‚˜์˜ ํƒœ๊ทธ
      );
    }

    ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•œ๋‹ค. (function, arrow function ๋ฌด๊ด€!)

     

    ์˜ˆ์‹œ

    function Modal(){
      return(
        <>  // fragment ๋ฌธ๋ฒ•
          ...
        </>
      );
    }

     

     

    (2) html ๋ธ”๋ก์„ return๊ฐ’์œผ๋กœ

    ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ง„ html ๋ธ”๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

     

    ์˜ˆ์‹œ

    function Modal(){
      return(
        <>
          <div className="modal-container">
              <div className="modal">
    
                <div className="modal-title-container modal-inner-container">
                  <span>์ œ๋ชฉ</span>
                  <div className="modal-title">Title</div>
                </div>
                <div className="modal-singer-container modal-inner-container">
                  <span>๊ฐ€์ˆ˜</span>
                  <div className="modal-singer">Singer</div>
                </div>
                <div className="modal-detail-container modal-inner-container">
                  <span>์„ธ๋ถ€์‚ฌํ•ญ</span>
                  <div className="modal-detail">Detail</div>
                </div>
              </div>
            </div>
        </>
      );
    }

     

     

    (3) ์›ํ•˜๋Š” ์œ„์น˜์— <์ปดํฌ๋„ŒํŠธ๋ช…></์ปดํฌ๋„ŒํŠธ๋ช…>

    ๊ธฐ์กด์— ์ž‘์„ฑํ•˜๋˜ App()์˜ return() ๋‚ด๋ถ€์—์„œ

    ์›ํ•˜๋Š” ์œ„์น˜์— <์ปดํฌ๋„ŒํŠธ๋ช…></์ปดํฌ๋„ŒํŠธ๋ช…>์˜ ํ˜•ํƒœ๋กœ ์‚ฝ์ž…ํ•œ๋‹ค.

     

    ์˜ˆ์‹œ

    function App(){
      return(
        <div>
          ...
          <Modal></Modal>
          
        </div>
      )
    }

     

     

    Component ์ œ์ž‘ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

    - ์ž‘๋ช…์€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘
    - return() ์•ˆ ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ๊ธฐ
    - App(){} ์™ธ๋ถ€์—์„œ ์ •์˜  (App() ์—ญ์‹œ ์ปดํฌ๋„ŒํŠธ์ž„!)
    - <์ปดํฌ๋„ŒํŠธ></์ปดํฌ๋„ŒํŠธ> & <์ปดํฌ๋„ŒํŠธ/> ๋‘˜๋‹ค ์‚ฌ์šฉ๊ฐ€๋Šฅ

     

    ๋Œ“๊ธ€