TIL 25_styled-components ๐ŸŽ€

dudgus5766ยท2021๋…„ 9์›” 13์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
7/8
post-thumbnail

ํ˜„์กดํ•˜๋Š” CSS in js ๊ด€๋ จ ๋ฆฌ์•กํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ค‘ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฉด์„œ CSSํŒŒ์ผ์„ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ์ปดํฌ๋„ŒํŠธ๋‚ด์—์„œ ๋ฐ”๋กœ CSS์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์ ์ด์–ด์„œ ์ ์  ์ธ๊ธฐ๊ฐ€ ๋งŽ์•„์ง€๋Š” styled-components์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž!

styled components

styled components๋Š” javascript์—์„œ css๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์Šคํƒ€์ผ๋ง ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. React Component์— ํŠน์ • ์Šคํƒ€์ผ๋ง์„ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋” ๋†’์ผ ์ˆ˜ ์žˆ๊ณ , javascript์— ์˜ํ–ฅ์„ ๋ฐ›๋Š” ์Šคํƒ€์ผ๋ง์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์šฉ ์˜ˆ์‹œ

  • ๊ธฐ์กด ๋ฐฉ์‹ (CSS)

const divStyle = {
    backgroundColor: "black",
    width: "100px",
    height: "100px",
  };

  return <div style={divStyle}></div>
  • styled-components ๋ฐฉ์‹

const StyledDiv = styled.div`
    background-color: black;
    width: 100px;
    height: 100px;
  `;

  return <StyledDiv></StyledDiv>

์žฅ์  ๐Ÿ‘

1. component ๋‹จ์œ„ ์Šคํƒ€์ผ๋ง

styled-components๋กœ ์ƒ์„ฑ๋œ Components๋“ค์„ ๋นŒ๋“œํ•˜๋ฉด ์ž„์˜์˜ ํด๋ž˜์Šค ๋„ค์ž„์— ์Šคํƒ€์ผ์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. styled-components๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ํŠน์ • ํด๋ž˜์Šค ๋„ค์ž„์„ ์ƒ์„ฑํ•ด ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํด๋ž˜์Šค ๋„ค์ž„์ด ์ค‘๋ณต๋˜๊ฑฐ๋‚˜ selector์˜ ์šฐ์„  ์ ์šฉ ์ˆœ์œ„ ๋•Œ๋ฌธ์— css ์Šคํƒ€์ผ๋ง์ด ํ˜ผ์„ ์„ ์ผ์œผํ‚ค๋Š” ์‚ฌ๊ณ ๋ฅผ ๋ฐฉ์ง€ ํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ๋ง

const StyledDiv = styled.div`
    background-color: black;
    width: 100px;
    height: 100px;
    ${({ login }) => {
      return login ? `display: none` : null;
    }}
  `;

  return <StyledDiv login={true}></StyledDiv>;

styled-components๋Š” Component์˜ props๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋‚ด์—์„œ javascript๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํ˜•์‹์ด๋ฉฐ, ๋‚ด๋ถ€์—์„œ ์„ ์–ธ๋œ ํ•จ์ˆ˜๋Š” props๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‹คํ–‰๋œ๋‹ค.

3. ํ™•์žฅ ์Šคํƒ€์ผ๋ง

const Container = styled.div`
    max-width: 600px;
    width: 100%;
    height: 100px;
  `;

  const BlackContainer = styled(Container)`
    background-color: black;
  `;

  const RedContainer = styled(Container)`
    background-color: red;
  `;

  return (
    <>
      <BlackContainer />
      <RedContainer />
    </>
  );

styled-components๋Š” ์ƒˆ๋กœ์šด Component๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๊ธฐ์กด์˜ Component์— ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ™•์žฅ ์Šคํƒ€์ผ๋ง์„ ์‚ฌ์šฉํ•˜๋ฉด ์ค‘๋ณต๋œ ์ฝ”๋“œ ์–‘์„ ์ค„์ด๊ณ , ๋ถ„์‚ฐ๋œ ์Šคํƒ€์ผ์„ ์ผ๊ด€์ ์œผ๋กœ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์–ด ์œ ์ง€๋ณด์ˆ˜ ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

4. ์ค‘์ฒฉ ์Šค์ฝ”ํ”„

const StyledDiv = styled.div`
    background-color: black;
    width: 100px;
    height: 100px;
    p {
      color: white;
    }
  `;

  return (
    <>
      <StyledDiv>
        <p>Title</p>
      </StyledDiv>
      <p>content</p>
    </>
  );

SASS์˜ ์ค‘์ฒฉ ์Šค์ฝ”ํ”„ ๊ทœ์น™์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋•๋ถ„์— ๋‚ด๋ถ€์˜ ๋ชจ๋“  component๋ฅผ styled-components๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ๋งŒ ์ ์šฉํ•˜๊ณ  ์‹ถ์€ ์Šคํƒ€์ผ์„ ์Šค์ฝ”ํ”„ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

โš ๏ธ ์ฃผ์˜: ๋ชจ๋“  SASS ๋ฌธ๋ฒ•์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง„ ์•Š๋‹ค!


์ถœ์ฒ˜ ๐Ÿ“š


https://dkje.github.io/2020/10/13/StyledComponents/

profile
RN App Developer

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