Frontend/CSS

[ styled-components ] ํƒœ๊ทธ๋Š” ๋‹ค๋ฅด์ง€๋งŒ css๋Š” ๋™์ผ ํ•  ๋•Œ css๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ

YWTechIT 2022. 6. 3. 06:01
728x90

๐Ÿ“ styled-component์—์„œ ํƒœ๊ทธ๊ฐ€ ๋‹ค๋ฅด์ง€๋งŒ css๋Š” ๋™์ผํ•  ๋•Œ css๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ

styled-component๋กœ ํƒœ๊ทธ๋ฅผ ๊พธ๋ฏธ๋˜ ์ค‘ ํƒœ๊ทธ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๋‚˜๋จธ์ง€ ์Šคํƒ€์ผ์€ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ๋‹ค. ๊ทธ๋ƒฅ ์Šคํƒ€์ผ์„ ์ค‘๋ณตํ•ด์„œ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ ๊ทธ๊ฒƒ๋ณด๋‹ค ๋™์ผํ•œ css๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜์—ฌ ๊ฐ๊ฐ ์ ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์ค‘๋ณตํ•ด์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

import styled from 'styled-components'

// before
const divWrapper = styled.div`
  font-size: 14px;
  background-color: red;
`

const SectionWrapper = styled.section`
  font-size: 14px;
  background-color: red;
`

// after
import styled, { css } from 'styled-components'

const cssVariable = css`
  font-size: 14px;
  background-color: red;
`

const divWrapper = styled.div`
  ${cssVariable}
`

const SectionWrapper = styled.section`
  ${cssVariable}
`
๋ฐ˜์‘ํ˜•