简体   繁体   English

使用样式参数的样式组件与功能组件

[英]Styled-Components vs functional component using the style parameter

I just asked myself what the reason would be to use styled-components over a custom functional component in react other than saving a few LOC.我只是问自己,除了节省一些 LOC 之外,在响应中使用样式组件而不是自定义功能组件的原因是什么。 Example code:示例代码:

const StyledButton = (props) => (
  <button
    style={{
      color: '#E8E8E8',
      padding: '8px 16px',
    }}
    {...props}
  />
);

const jsx = () => <div><StyledButton>Click Me!</StyledButton></div>;
const StyledButton = styled.button`
  color: '#E8E8E8';
  padding: '8px 16px';
`;

const jsx = () => <div><StyledButton>Click Me!</StyledButton></div>;

Kind regards, Thomas亲切的问候,托马斯

With styled-components you can leverage the full power of css, which means you can:借助 styled-components,您可以利用 css 的全部功能,这意味着您可以:

  • Overwrite the css of child tags and classes覆盖子标签和类的 css
  • Use pseudo classes such as :visited , :first etc.使用伪类,例如:visited:first等。
  • Use hover and other ui states使用hover等ui状态
  • Use css variables easily轻松使用 css 变量

Just as a few examples, but obviously anything else that css can do by default that would require extra javascript to do with just plain styles.仅举几个例子,但显然 css 默认可以做的任何其他事情都需要额外的 javascript 来处理普通的 styles。

Also you can read more about the motivation behind styled-components here .你也可以在这里阅读更多关于 styled-components 背后的动机

暂无
暂无

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

相关问题 样式组件未将样式应用于自定义功能反应组件 - styled-components not applying style to custom functional react component 使用 styled-components 从父组件设置子组件的样式 - Style child component from parent component using styled-components 如何在 styled-components 中扩展组件样式 - How to extend component style in styled-components 如何使用样式化组件设置嵌套功能组件的样式 - How to style a nested functional component using styled components 使用带有 cypress 的 styled-components 或自定义组件 - using styled-components or custom component with cypress Styled-components vs Emotion - 如何在 Styled-components 上重新应用 css`style` function - Styled-components vs Emotion - How to reapplicate css`style` function on Styled-components 在样式化组件中的子组件的输入焦点上设置父组件 - Style parent component on input focus of child component in styled-components 使用 styled-components 更改其 classList 动态更改的组件的样式 - Changing the style of a component that has its classList dynamically changed, using styled-components 样式化的组件不会重新样式化组件 - styled-components not restyling a component 当尝试使用 typescript 定义功能组件以与样式组件反应时,会出现“没有重载匹配此调用”的错误。 - when try to define a functional component in react with styled-components using typescript get error of “No overload matches this call.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM