简体   繁体   English

React组件样式困境

[英]React components styling dilemma

I start making new project using React and Redux. 我开始使用React和Redux制作新项目。 I'm new to React and not sure about what kind of styling mehtod I should use. 我是React的新手,不确定我应该使用哪种样式的方法。 Apparently I want to use the one that will not decrease performance, give all features of CSS3, and be maintainable. 显然,我想使用不会降低性能,提供CSS3的所有功能并且易于维护的功能。 So the choices are: 所以选择是:

  1. Inline styling using Radium 使用Radium进行内联样式
  2. SASS 萨斯
  3. CSS Modules CSS模块

I've long experience working with SASS and CSS3.Nevertheless Inline Styling really impressed me but it does not provide autoprefixer and some other nice automation features. 我有使用SASS和CSS3的丰富经验,尽管Inline Styling确实给我留下了深刻的印象,但它不提供自动前缀和其他一些不错的自动化功能。 Obviously there's no ultimate choice, but what is the most efficient one? 显然,没有最终选择,但是最有效的选择是什么?

I would personally recommend a combination of glamor and glamorous . 我个人会推荐魅力魅力的结合。 Glamorous is build on glamor so you can leverage both for different purposes. 魅力在于魅力,因此您可以将两者用于不同的目的。

With glamor, you can inject global styles into the page like: 借助魅力,您可以将全局样式注入页面,例如:

css.global('h1', { fontSize: '3rem' })

With glamorous, you can create React components that make styling things more intuitive, like: 魅力四射,您可以创建React组件,使样式设计更加直观,例如:

glamorous.Div({ padding: '2rem', background: 'red' })

return <Div>Some text</Div>

Personally I prefer to use CSS-in-JS versus a pre-processor like Sass/LESS or using some sort of CSS-like tool. 我个人更喜欢使用CSS-in-JS而不是像Sass / LESS这样的预处理器,或者使用某种类似CSS的工具。 I find trying to write CSS in a template tag or in another file is cumbersome and annoying. 我发现尝试在模板标签或另一个文件中编写CSS既麻烦又烦人。 Leveraging JavaScript directly for styling is actually quite enjoyable once you get used to it. 一旦习惯了,直接利用JavaScript进行样式实际上非常有趣。

Shameless plug: I've created a helper library for CSS-in-JS project that automatically generates helpful styling classes like what you find in Bootstrap (eg .text-center , .ml-auto , .display-block etc). 无耻的插件:我为CSS-in-JS项目创建了一个帮助程序库,该程序库会自动生成有用的样式类,例如您在Bootstrap中找到的样式类(例如.text-center.ml-auto.display-block等)。 It's called Euphoria , check it out! 这就是幸福感 ,快来看看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM