浅析 styled-components

css in js

  • emotion

  • styled-components

使用 ES6 的字符串模板

webComponent 的概念出现了。其中最出名的是 custom Element 什么是 custom element 我们可以自定义 html Element

class HelloCustomElement extends HTMLElement {}
customElements.define("hello-custom-element", HelloCustomElement);

使用

<hello-custom-ele>
  <h1>Original H1</h1>
</hello-custom-ele>

看起來什麼事情都沒發生,就像是亂寫一個 HTML tag 而已,那個 h1 還是正常的顯示出來,但是其實 已經是一個可以客製行為的 html 元件,我們來驗證一下:

class HelloCustomElement extends HTMLElement {
  constructor() {
    super();
    debugger;
  }
}

CSS in JS

使用 styled 变量中包含的函数可以表示所有已知的 HTML 元素

需要注意的是只有 React 可以使用 Styled-Components

Props

模板字符串中用箭头函数提供 props 变量.你可以给 css 样式传入任意值.

CSS 代码片段

styled-components 用法

styled-components 有如下优点

  1. 样式写在 js 文件里,降低 js 对 css 文件的依赖.

  2. 样式可以使用变量,更加灵活。

  3. SSR 类框架处理 CSS Modules 变量相当棘手,所以使用 styled-components 作方案

使用技巧

  • 避免过度组件化 使用 Styled-component 的原因是想通过它对样式进行组件化,避免像传统 css 那样的全局污染.

  • 嵌套元素 不能创建一个返回超过一个 HTML 子元素的 Styled-component 组件. 我们需要对复杂组件,进行拆分.

最后更新于

这有帮助吗?