简体   繁体   English

React.js | 通过 Props 进行样式与编写 CSS-in-JS

[英]React.js | Styling via Props vs. Writing CSS-in-JS

Various libraries offer styling solutions via props of components, while the "standard" way for a long time has been to write CSS, separately.各种库通过组件的 props 提供样式解决方案,而“标准”方式长期以来一直是单独编写 CSS。

With the invention of CSS-in-JS, it's now possible to have some benefits we didn't have before (eg string literals, conditional classes, plugins to extend functionality, etc.), and on the separation level, it can be used like CSS style tags in HTML, where it's possible to define in the "main" file (HTML, in the case of CSS) an environment to write the code in, and we have more flexibility, but with JSS, for example, it's a common practice, from my understanding, to centralize the styling code in a classes object.随着 CSS-in-JS 的发明,现在可以有一些我们以前没有的好处(例如字符串文字、条件类、扩展功能的插件等),并且在分离级别上,它可以使用就像 HTML 中的 CSS 样式标签一样,可以在“主”文件(HTML,在 CSS 的情况下)中定义一个环境来编写代码,我们有更大的灵活性,但是对于 JSS,例如,它是一个根据我的理解,通常的做法是将样式代码集中在classes对象中。

On the other hand, we can write not just inline CSS in components, but various libaries offer styling solutions as props of components such as Material-UI .另一方面,我们不仅可以在组件中编写内联 CSS,而且各种库都提供样式解决方案作为组件的道具,例如Material-UI

So, my question is: what advantages pros and cons do you think there are for writing CSS-inJS compared to writing styling code using component props?所以,我的问题是:与使用组件 props 编写样式代码相比,您认为编写 CSS-inJS 有哪些优点和缺点?

*Disclaimer: I don't have a lot of experience with CSS-in-JS and styling in React in general, so I might have a wrong impression of what things are like generally/in the bigger picture. *免责声明:总的来说,我对 CSS-in-JS 和 React 中的样式没有很多经验,所以我可能对事物的总体情况/大局观有错误的印象。

*Do notice that I'm not asking about inline-CSS in components and that this is not an inline vs non-inline question, but a more specific one. *请注意,我不是在问组件中的内联 CSS,这不是内联与非内联的问题,而是一个更具体的问题。

You ask about the Pros/Cons for those cases:您询问这些情况的优缺点:

Dynamic className动态className

const useStyles = makeStyles({
  root: {
    background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
    border: 0,
    borderRadius: 3,
    boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
    color: 'white',
    height: 48,
    padding: '0 30px',
  },
});

export default function App() {
  const classes = useStyles();
  return <Button className={classes.root}>My Button</Button>;
}

Which is just a nicer implementation of:这只是一个更好的实现:

import './myStyle.css'

export default function App({ styleName }) {
  return <Button className={styleName}>My Button</Button>;
}

Pros:优点:

  • Simple and straight forward.简单直接。

Cons:缺点:

  • Writing CSS-like object (not real CSS).编写类 CSS 对象(不是真正的 CSS)。
  • Such implementation is decoupled to the UI library这样的实现与UI库解耦
    • There may be a library that can be used across projects, but still, why just not using CSS-in-JS with it is the case?可能有一个可以跨项目使用的库,但是,为什么不使用 CSS-in-JS 呢?

CSS-in-JS CSS-in-JS

const PrettyButton = styled(Button)`
  background: linear-gradient(45deg, #fe6b8b 30%, #ff8e53 90%);
  border: 0;
  borderradius: 3;
  boxshadow: 0 3px 5px 2px rgba(255, 105, 135, 0.3);
  color: white;
  height: 48;
  padding: 0 30px;
`;

export default function App() {
  return <PrettyButton>My Button</Button>;
}

Pros:优点:

  • Writing a real CSS (you get auto-complete, CSS formatting, style-linting, not writing string literals which are error-prone, etc.)编写一个真正的 CSS(你会得到自动完成、CSS 格式化、样式检查,而不是编写容易出错的字符串文字等)
  • CSS-in-JS benefits (Google it). CSS-in-JS 的好处(谷歌它)。

Cons:缺点:

  • The debate of why not to use CSS-in-JS (Google it).关于为什么不使用 CSS-in-JS 的争论(谷歌它)。

Pass the classes around.把课传过去。 Passing CSS like props to component is not gonna work .将类似 props 的 CSS 传递给组件是行不通的。

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

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