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

[React] 12. Styled-components (+ ํ•œ ์ปดํฌ๋„ŒํŠธ์— cssํŒŒ์ผ ์ ์šฉ๋ฒ•)

by KASSID 2023. 10. 4.

๋ชฉ์ฐจ

    728x90

    ์ด์ „ ๊ธ€ โ–ผ

    ๋”๋ณด๊ธฐ

    0. 2023.05.05 - [๐ŸŒŒ | WEB DEV/React] - [React] 0. React๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  & ์ดˆ๊ธฐ ์„ธํŒ…

    1. 2023.05.07 - [๐ŸŒŒ | WEB DEV/React] - [React] 1. JSX

    2. 2023.05.26 - [๐ŸŒŒ | WEB DEV/React] - [React] 2. state (๋ณ€์ˆ˜, array, object)

    2-1. 2023.05.27 - [๐ŸŒŒ | WEB DEV/React] - [React] 2-1. array state ์ •๋ ฌํ•˜๊ธฐ ( .sort()ํ™œ์šฉ)

    3. 2023.05.28 - [๐ŸŒŒ | WEB DEV/React] - [React] 3. ์ปดํฌ๋„ŒํŠธ (Component)

    4. 2023.05.28 - [๐ŸŒŒ | WEB DEV/React] - [React] 4. ๋™์ ์ธ UI ์ œ์ž‘ํ•˜๊ธฐ

    5. 2023.05.28 - [๐ŸŒŒ | WEB DEV/React] - [React] 5. ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„ ์ถ•์•ฝํ•˜๊ธฐ (map)

    6. 2023.05.29 - [๐ŸŒŒ | WEB DEV/React] - [React] 6. props

    7. 2023.07.03 - [๐ŸŒŒ | WEB DEV/React] - [React] 7. ์ด๋ฏธ์ง€ ์‚ฝ์ž…ํ•˜๊ธฐ(css, html, publicํด๋”์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ)

    8. 2023.07.06 - [๐ŸŒŒ | WEB DEV/React] - [React] 8. ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

    9. 2023.07.09 - [๐ŸŒŒ | WEB DEV/React] - [React] 9. ๋ผ์šฐํŒ…-1 (react-router-dom์„ธํŒ…, Routes, Route, Link)

    10. 2023.07.10 - [๐ŸŒŒ | WEB DEV/React] - [React] ๋ผ์šฐํŒ…-2 (useNavigate, 404page, Nested Routes, Outlet)

    11. 2023.10.03 - [๐ŸŒŒ | WEB DEV/React] - [React] 11. ๋ผ์šฐํŒ…-3 ์–‘์‚ฐํ˜• ๋ผ์šฐํŒ…

     

    React ํ˜น์€ React-Native ๊ฐœ๋ฐœ์— ๋งŽ์ด ํ™œ์šฉํ•˜๋Š” styled-components์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž!

     

    1. styled-components๋ž€?

    css ์‚ฌ์šฉ์‹œ ๋ถˆํŽธํ•œ ์ 

    html์— ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  ์‹ถ์„๋•Œ๋Š” css๋ฅผ ์ด์šฉํ•œ๋‹ค.

    ํ•˜์ง€๋งŒ ์ž‘์—…์„ ํ•˜๋‹ค๋ณด๋ฉด ๋ช‡๊ฐ€์ง€ ๋ถˆํŽธํ•จ์„ ๊ฒช์—ˆ์„ ๊ฒƒ์ด๋‹ค.

    [CSSํŒŒ์ผ๋งŒ ์‚ฌ์šฉํ•  ๋•Œ ๋ถˆํŽธํ–ˆ๋˜ ์ ๋“ค..]
    - ํ˜„์žฌ ์ž‘์—…ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์— ์Šคํƒ€์ผ ์ ์šฉ ์ค‘์ด์—ˆ์ง€๋งŒ class๋ช… ์ค‘๋ณต์œผ๋กœ ์ธํ•ด ๊นจ์ง.
    - ๊ทœ๋ชจ๊ฐ€ ์ปค์กŒ์„ ๋•Œ class ๋ช…์„ ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ๊ณ„์† ์ƒ๊ฐํ•ด์ค˜์•ผํ•จ.
    - CSSํŒŒ์ผ์ด ๊ธธ์–ด์กŒ์„ ๋•Œ ์ˆ˜์ •ํ•˜๊ธฐ ๋ฒˆ๊ฑฐ๋กœ์›€.

     

    styled-components๋Š” ์Šคํƒ€์ผ์„ ๋จผ์ € ์ฃผ์ž…ํ•ด์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์—

    ์ด๋Ÿฌํ•œ ๋ถˆํŽธํ•จ์„ ํ•ด์†Œํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    styled-components์˜ ์žฅ๋‹จ์ 

    1) ์žฅ์ 

    - CSSํŒŒ์ผ ์—†์ด JSํŒŒ์ผ์—์„œ ์Šคํƒ€์ผ ์ ์šฉ ๊ฐ€๋Šฅ

    - ์ด ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋ฉด ์Šคํƒ€์ผ์ด ๋‹ค๋ฅธ JSํŒŒ์ผ์— ์ ์šฉ๋˜์ง€ ์•Š์Œ (↔์ผ๋ฐ˜ cssํŒŒ์ผ์€ ๋ชจ๋“  ํŒŒ์ผ์— ์ ์šฉ๋จ)

    - ํŽ˜์ด์ง€ ๋กœ๋”ฉ์‹œ๊ฐ„ ๋‹จ์ถ• ๊ฐ€๋Šฅ (css ํŒŒ์ผ ๋กœ๋”ฉ์„ ์•ˆํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด!)

     

    2) ๋‹จ์ 

    - JS ํŒŒ์ผ์ด ๋ณต์žกํ•ด์ง (์ผ๋ฐ˜ ์ปดํฌ๋„ŒํŠธ์™€ ๊ตฌ๋ถ„ํ•ด์•ผํ•จ)

    - ๋””์ž์ด๋„ˆํŒ€๊ณผ ํ˜‘์—… ์ด์Šˆ

    - ํŒŒ์ผ๊ฐ„ ์Šคํƒ€์ผ ์ค‘๋ณต์ด ๋งŽ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ์˜๋ฏธ ์—†์Œ

     

    +) cssํŒŒ์ผ  ํ•œ ์ปดํฌ๋„ŒํŠธ์—๋งŒ ์ ์šฉํ•˜๊ธฐ (css ๋ชจ๋“ˆํ™”)

    ์ผ๋ฐ˜์ ์ธ cssํŒŒ์ผ์€ ์ „์ฒด ํŒŒ์ผ์— ์ ์šฉ๋œ๋‹ค.

    ํ•˜์ง€๋งŒ ๋ชจ๋“ˆํ™”๋ฅผ ํ•˜๋ฉด ํ•œ ํŒŒ์ผ์—๋งŒ ์ ์šฉํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์ปดํฌ๋„ŒํŠธ๋ช….module.css

     

    ์œ„์™€ ๊ฐ™์ด ํŒŒ์ผ๋ช…์„ ์„ค์ •ํ•˜๋ฉด ์ง€์ •ํ•œ ์ปดํฌ๋„ŒํŠธ๋ช…์„ ๊ฐ€์ง„ jsํŒŒ์ผ์—๋งŒ ์ ์šฉ๋œ๋‹ค.

     

     


    2. ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

    1) ์„ค์น˜ํ•˜๊ธฐ

    npm install styled-components

    ํ„ฐ๋ฏธ๋„์—์„œ npm์„ ์ด์šฉํ•ด ์„ค์น˜ํ•œ๋‹ค.

     

    2) ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ๋‹จ๊ณ„

    [์ƒ์„ฑ ์ ˆ์ฐจ]
    1. import ํ•ด์˜ค๊ธฐ
    2. styled

     

    (1) import ํ•ด์˜ค๊ธฐ

    import styled from "styled-components"

    ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฝ์ž…ํ•  ํŒŒ์ผ์— ์œ„์ฒ˜๋Ÿผ import ํ•œ๋‹ค.

     

    (2) ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

    โ‘  styled.ํƒœ๊ทธ๋ช… ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

        eg.) styled.div, styled.p ๋“ฑ ...

    โ‘ก styled.ํƒœ๊ทธ๋ช… ๋’ค์— ๋‘ ๊ฐœ์˜ ``(๋ฐฑํ‹ฑ) ์•ˆ์— ์Šคํƒ€์ผ์„ ์ ์šฉํ•œ๋‹ค. -> ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ

    โ‘ข ๋ณ€์ˆ˜์— ๋‹ด๊ณ  <๋ณ€์ˆ˜๋ช…>์˜ ํƒœ๊ทธ๋ฅผ ์›ํ•˜๋Š” ๊ณณ์— ์œ„์น˜์‹œํ‚จ๋‹ค. (์ปดํฌ๋„ŒํŠธ ์ด๋ฏ€๋กœ ๋™์ผํ•˜๊ฒŒ ์ด์šฉ)

     

    ์˜ˆ์‹œ

    import styled from "styled-components"
    
    let SkyBox = styled.div`
      background: rgb(0, 149, 212);
      padding: 20px;
      border-radius: 10px;
    `;
    
    
    function Page(){
      return(
        <>
          ...
          <SkyBox/>
        </>
      )
    }

     

    ๊ฒฐ๊ณผ

     

    (3) ์žฌํ™œ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค๊ธฐ

    ์œ„์—์„œ ๋งŒ๋“  div ๋ฐ•์Šค์˜ ์ƒ‰๋งŒ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ 

    ์žฌํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค์–ด๋ณด์ž!

     

    ์ปดํฌ๋„ŒํŠธ props๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ styled-components๋„ props๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    styled.ํƒœ๊ทธ๋ช…`
      ... ${props => props.๋ณ€์ˆ˜๋ช…}
    `;

     

    ์ •์˜ํ•œ ๋ณ€์ˆ˜๋ช…์„ ๊ฐ€์ง„ props๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

    ์•„๋ž˜์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉฐ ํ™•์ธํ•ด๋ณด์ž!

     

    ์˜ˆ์‹œ 1

    import styled from "styled-components"
    
    let SkyBox = styled.div`
      background: ${props => props.background};
      padding: 20px;
      border-radius: 10px;
    `;
    
    
    function Page(){
      return(
        <>
          ...
          <SkyBox background = "gold"/>
          <SkyBox background = "cyan"/>
        </>
      )
    }

     

    ๊ฒฐ๊ณผ 1

     

    background๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ž‘๋ช…ํ•œ props๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ๋‹ค!

     

     

    ์˜ˆ์‹œ 2

    import styled from "styled-components"
    
    let SkyBox = styled.div`
      background: ${props => props.background};
      color : ${props => props.background === "black" ? "#efefef" : "#333"};
      padding: 20px;
      border-radius: 10px;
    `;
    
    
    function Page(){
      return(
        <>
          ...
          <SkyBox background = "black">Hello</SkyBox>
          <SkyBox background = "white">Hello</SkyBox>
        </>
      )
    }

     

    ๊ฒฐ๊ณผ 2

     

    ๋Œ“๊ธ€