Style in React

前端模块化以后,CSS也能向模块同样导入, 以前提倡JS 与CSS分离,如今JS和CSS混合的方式又开始流行了。css

CSS与JS分离

CSS Modules前端

  • No more conflicts 没有名称冲突
  • No global scope
  • Composing from other files
  • Composing from global class names

compose from other files

compose from global

CSS in JSS

JSS

  • Theming 利用 ThemeProvider 和 theme 封装组件
  1. withTheme

clipboard.png

  1. ThemeProvider

clipboard.png

Benefitsreact

  • Theming support out of the box.
  • Critical CSS extraction.
  • Lazy evaluation - sheet is created only when component will mount.
  • Auto attach/detach - sheet will be rendered to the DOM when component is about to mount and will be removed when no element needs it.
  • A Style Sheet gets shared between all elements.

Styled-component

  • 经过props 修改style

clipboard.png

  • extend 继承

clipboard.png

  • Theming
  1. styled-components has full theming support by exporting
  2. a <ThemeProvider> wrapper component. 
  3. Function themes
  4. Getting the theme without styled components

clipboard.png

styled-component 上提供了个对比 https://github.com/styled-com...git

参考文献
https://glenmaddern.com/artic...
https://speakerdeck.com/vjeux...
http://www.alloyteam.com/2017...
https://hackernoon.com/the-co...github

相关文章
相关标签/搜索