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

[React] 4. ๋™์ ์ธ UI ์ œ์ž‘ํ•˜๊ธฐ

by KASSID 2023. 5. 28.

๋ชฉ์ฐจ

    728x90

    ์ด์ „ ๊ธ€ โ–ผ

     

     

    ์ „ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃจ์—ˆ๋˜ ์ปดํฌ๋„ŒํŠธ์˜ ์˜ˆ์‹œ๋กœ ๋ชจ๋‹ฌ(Modal)์ฐฝ์„ ์ œ์ž‘ํ•˜์˜€๋‹ค.

    ์ด๋ฅผ ์™„์„ฑํ•˜๋ฉฐ ๋™์ ์ธ UI๋ฅผ ์ œ์ž‘ํ•ด๋ณด์ž!

     

    1. ๋™์ ์ธ UI ๋งŒ๋“ค๊ธฐ

    ์œ ์ €์˜ ์กฐ์ž‘์— ๋”ฐ๋ผ ํ˜•ํƒœ๊ฐ€ ๋ฐ”๋€Œ๋Š” UI๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ผ์„œ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค!

    1) html  ๋ธ”๋ก ๋งŒ๋“ค๊ธฐ
    2) UI์˜ ์ƒํƒœ๋ฅผ state๋กœ ๋งŒ๋“ค๊ธฐ
    3) state์— ๋”ฐ๋ฅธ UI์˜ ํ˜•ํƒœ๋ฅผ ์กฐ๊ฑด๋ฌธ ๋“ฑ์œผ๋กœ ์ž‘์„ฑ
    4) state ์กฐ์ž‘ํ•˜๊ธฐ

     

    1) html ๋ธ”๋ก ๋งŒ๋“ค๊ธฐ

    function Modal(){
      return(
        <>
          <div className="modal-container">
            <div className="modal">
              <div className="bar"></div>
              <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>
        </>
      );
    }

    ์กฐ์ž‘ํ•  UI์˜ html๊ณผ css๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. (์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๊ด€๊ณ„ ์—†์Œ)

     

     

    2) UI์˜ ์ƒํƒœ๋ฅผ state๋กœ ๋งŒ๋“ค๊ธฐ

    UI์˜ ์ƒํƒœ์ •๋ณด๋ฅผ state๋กœ ์ €์žฅํ•œ๋‹ค.

    ์ฆ‰, ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’์„ ์ฐธ์กฐํ•˜์—ฌ ์กฐ์ž‘์„ ํ•  ๊ฒƒ์ด๋ฏ€๋กœ ํ˜„ ์ƒํƒœ๋ฅผ ์‹๋ณ„ํ•  ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

     

    const [modal, setModal] = useState('closed');

     

    ์ด์ฒ˜๋Ÿผ ์ž„์˜๋กœ ์ƒํƒœ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.

    ๋ฌผ๋ก  state ์ •์˜ ์‹œ์—๋Š” ํ˜„ ์ƒํƒœ๋ฅผ ์ €์žฅํ•ด์•ผํ•œ๋‹ค.

     

     

    3) state์— ๋”ฐ๋ฅธ UI์˜ ํ˜•ํƒœ๋ฅผ ์กฐ๊ฑด๋ฌธ ๋“ฑ์œผ๋กœ ์ž‘์„ฑ

    state์— ๋‹ด๊ธด ๊ฐ’์„ ์ฐธ์กฐํ•˜์—ฌ UI์˜ ํ˜•ํƒœ๋ฅผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์กฐ์ž‘ํ•œ๋‹ค.

     

    ์ด๋•Œ ์ค‘๊ด„ํ˜ธ{ }๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ธ”๋ก์„ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— ์กฐ๊ฑด๋ฌธ์„ ๋„ฃ์–ด์ค€๋‹ค.

     

    function App(){
      return(
        ...
        
        {
          ์กฐ๊ฑด๋ฌธ
        }
        
        ...
      )
    }

     

    ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ ํ˜„์žฌ html ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑ์ค‘์ด๋ฏ€๋กœ 

    if ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

     

     

    {
      modal === 'closed' ? null : <Modal></Modal>
    }

     

     

    4) state ์กฐ์ž‘ํ•˜๊ธฐ

    ์ด์ œ state๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋œ๋‹ค.

     

    ...
    
    <div className="music-list" 
      onClick={()=>{modal === 'closed' ? setModal('open'):setModal('closed')}}>
    
    ...

    ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” onClick ์†์„ฑ์—์„œ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค.

     

    ๋”๋ณด๊ธฐ
    // ๋งŒ์•ฝ ์กฐ์ž‘ํ•  state๊ฐ€ boolean์ผ ๊ฒฝ์šฐ
    <button onClick={ ()=>{ setModal(!modal) } }> CHANGE </button>

     

     

    ๋Œ“๊ธ€