简体   繁体   English

样式反应组件

[英]Styling react components

I'm a big react fan. 我是一个很反应的粉丝。 It's a really awesome tool for building UI but there are some problems which are difficult to get rid of without trouble. 它是构建UI的一个非常棒的工具,但是有些问题很难摆脱而没有麻烦。

One of them is styling components, the original 'react-way' is using inline styles, but this solution has some negative qualities, ex: 其中一个是造型组件,原始的“反应方式”使用内联样式,但这个解决方案有一些负面的品质,例如:

  • It seems not to be a good solution from designer's point of view 从设计师的角度来看,这似乎不是一个好的解决方案
  • Is it customizable enough? 它可以定制吗?
  • I cannot use some of the awesome tools like autoprefixer or sass/less, which make my styles more maintainable 我不能使用一些很棒的工具,如autoprefixer或sass / less,这使我的风格更易于维护

But there are some positive ones: 但有一些积极的:

  • Components become very cohesive and reusable, styles are shipped together with it. 组件变得非常有凝聚力和可重复使用,样式与它一起运输。
  • When inline styles are used in some library, I don't need to require any stylesheets, the style is all in my code 当在某些库中使用内联样式时,我不需要任何样式表,样式都在我的代码中

Other "WriteYourStylesInJS" practices have the same weaknesses, and the biggest is the reduced set of styling tools (Yes, I'm too lazy to write prefixes). 其他“WriteYourStylesInJS”实践具有相同的弱点,最大的是减少的样式工具集(是的,我懒得写前缀)。

As for my actual question, how do you make your react components and css (or scss/sass or less or any other language) styles cohesive? 至于我的实际问题,你如何使你的反应组件和css(或scss / sass或更少或任何其他语言)风格具有凝聚力?

How do you make styles customizable and maintainable? 如何使样式可定制和可维护?

Is my prepossesion against inline styles reasonable? 我对内联样式的预设是否合理?

I'm using inline styles and I had some problems at the beginning but you know what? 我正在使用内联样式,我在开始时遇到了一些问题,但你知道吗? I love it know... 我喜欢它知道......

  • I don't have conflicts of style. 我没有风格冲突。 In all of my previous non-react projects I had them... easy to fix but I don't need to worry about it now 在我之前的所有非反应项目中,我都有它们......很容易修复,但我现在不需要担心
  • If I need to edit the style of a component. 如果我需要编辑组件的样式。 I just go to the component and edit it. 我只是去组件并编辑它。 Before... I had to check the classes of the element and then go to the css file to edit it.. and maybe I'm breaking the style of another element with the same class! 之前......我必须检查元素的类,然后转到css文件进行编辑..也许我打破了同一个类的另一个元素的风格!
  • I love style with js objects. 我喜欢js对象的风格。 Js everywhere! 到处都是Js! Thanks to that we don't need to use preprocessors as SCSS to have variables (just an example) 多亏了我们不需要使用预处理器作为SCSS来获取变量(只是一个例子)

But you're right and you can have headaches with pseudo selectors like :hover... or vendor prexing, even media queries. 但是你是对的,你可能会对伪选择器感到头疼:像hover ...或供应商prexing,甚至是媒体查询。

So.. if you need all of that, maybe you should check Radium 所以..如果你需要所有这些,也许你应该检查Radium

stay foolish! 保持愚蠢!

In my project, i have used separated css files for styling, with same name like my components. 在我的项目中,我使用了分离的css文件进行样式设置,其名称与我的组件相同。 In production it will be anyway minified and splitted in one js/css file, but you can easily copy/paste your components from project to project. 在生产中,它无论如何都会在一个js / css文件中缩小和分割,但是您可以轻松地将组件从一个项目复制/粘贴到项目中。

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

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