styled-components의 장단점

Younngg·2023년 1월 20일

면접에서 styled-components의 단점에 대한 질문을 받았다. 장점에 대해선 어느정도 대답할 수 있으나, 단점은 명확히 대답하지 못했다. 그래서 블로그와 공식문서를 읽고 정리를 해보았다.

번역이 정확하지 않고, 나의 경험을 통한 생각이 포함되어 있어 정확하지 않다.

styled-components

장점

공식문서에서 장점을 가져와봤다.

styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users.

Apart from the improved experience for developers, styled-components provides:

  • Automatic critical CSS: styled-components keeps track of which components are rendered on a page and injects their styles and nothing else, fully automatically. Combined with code splitting, this means your users load the least amount of code necessary.
  • No class name bugs: styled-components generates unique class names for your styles. You never have to worry about duplication, overlap or misspellings.
  • Easier deletion of CSS: it can be hard to know whether a class name is used somewhere in your codebase. styled-components makes it obvious, as every bit of styling is tied to a specific component. If the component is unused (which tooling can detect) and gets deleted, all its styles get deleted with it.
  • Simple dynamic styling: adapting the styling of a component based on its props or a global theme is simple and intuitive without having to manually manage dozens of classes.
  • Painless maintenance: you never have to hunt across different files to find the styling affecting your component, so maintenance is a piece of cake no matter how big your codebase is.
  • Automatic vendor prefixing: write your CSS to the current standard and let styled-components handle the rest.

You get all of these benefits while still writing the CSS you know and love, just bound to individual components.

출처: styled-components 공식 문서 https://styled-components.com/docs/basics#motivation

대충 요약하자면…

개발자를 위해 향상된 경험을 제공한다.

  • Automatic critical CSS: styled-components는 페이지에 어떤 구성 요소가 렌더링되는지 추적하고 해당 스타일만 완전히 자동으로 주입한다. 코드 분할 시, 사용자가 필요한 최소한의 코드를 로드한다.
  • No class name bugs: 고유 클래스 명을 생성한다.
  • Easier deletion of CSS: 클래스 명을 어디에서 사용했는지 찾기 어려울 수 있다. styled-components는 스타일이 특정 구성 요소에 연결 되어 있어 구성 요소가 사용되지 않고 삭제되면 스타일 또한 삭제된다. (특정 구성 요소와 연결 되어 있기 때문에 style을 삭제 및 수정하기 쉽다는 얘기 같다.)
  • Simple dynamic styling: 전역 테마 또는 props를 기반으로 구성 요소의 스타일을 간단하고 직관적으로 적용할 수 있다.
  • Painless maintenance: 코드베이스가 크더라도 유지보수하기 쉽다.
  • Automatic vendor prefixing: CSS를 현재 표준에 맞게 작성하면 나머지는 styled-components가 알아서 해준다는 얘기인듯.

이런 얘기다.

내가 생각하는 장점은,

  • class 명과 그에 해당하는 style을 찾기 편해 유지보수가 쉽다는 점
  • component처럼 작성하고 추상화하여 React의 장점을 극대화 시킬 수 있다는 점
  • React와 props를 공유하여 style 확대 가능, 변경이 가능하다는 점
  • 고유한 className을 적용해준다는 점

이다.

단점

대부분의 CSS in JS 라이브러리는 런타임에 하나 이상의 태그를 사용하거나 API를 사용하고 CSSOM 내에서 스타일을 직접 관리하여 DOM에 삽입한다. SSR 중에는 항상 렌더링 된 HTML 내부에 태그로 추가된다.

단점은 번들 크기와 관련이 있다.

  • 동적 스타일을 지정하려면 추가로 런타임 라이브러리가 필요하다. 라이브러리가 추가되면서 번들의 크기가 커진다.
  • SSR에 인라인 된 경우, 기본적으로 캐시가 되지 않아 요청이 있을 때마다 브라우저에 제공 되어야 한다.

출처: https://css-tricks.com/a-thorough-analysis-of-css-in-js/

  • 자바스크립트와 별도로 캐시할 수 없다.
  • CSS in CSS에 비해 느리다.
  • 러닝 커브가 있다.

출처: https://www.cbinsights.com/research/team-blog/css-in-js-traditional-css/

그리고 오히려 Component와 비슷하게 작성되어 코드 상에서 Component와 Style을 구분하기 어려운 단점도 있을 것 같다. 예를 들어 Button 컴포넌트와 styled-components로 작성된 Button 스타일이 존재할 때 구분하기 번거로울 것이라 생각했다. 이름을 다르게 짓거나 import 할 때 이름을 바꾸는 식으로 해야 할 것이다. 작은 프로젝트에선 충분히 구분이 가능하겠지만 대형 프로젝트라면 이런 상황이 발생할 수도 있을 것 같다. 단지 나의 추측이다.

profile
8533283@naver.com

0개의 댓글