React:为什么不只使用样式对象而不是样式组件?

big*_*ato 5 reactjs styled-components

我之前用 React Native 编写过应用程序,现在我即将开始我的第一个 React 项目。我注意到一个名为 Styled Components 的工具:https://www.styled-components.com/docs/basics#motivation

但是,我没有看到它有任何明显的好处,除了我可以在样式定义中执行媒体查询(所有这些都在与我的组件相同的文件中)。

但假设我有这个按钮:

import React from 'react';

const StyledButton = ({ children }) => {
  return (
    <button type="button" style={styles.button}>
      { children }
    </button>
  );
}

const styles = {
  button: {
    backgroundColor: '#6BEEF6',
    borderRadius: '12px',
    border: 'none',
    boxShadow: '0 5px 40px 5px rgba(0,0,0,0.24)',
    color: 'white',
    cursor: 'pointer',
    fontSize: '15px',
    fontWeight: '300',
    height: '57px',
    width: '331px',
  }
}

export default StyledButton;
Run Code Online (Sandbox Code Playgroud)

在样式组件中编写此内容会有什么不同?是否只有我的某些样式依赖于某些props样式组件的表现?

例如,这在反应中不起作用:

const StyledButton = ({ children, primary }) => {
  return (
    <button type="button" style={[styles.button, { color: primary ? 'green' : 'red' }]}>
      { children }
    </button>
  );
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*upt 2

您在使用纯内联样式时会遇到的一个早期障碍是缺乏伪选择器等:hover:active就像您提到的那样,您也不能使用媒体查询。

样式化组件很棒。还可以查看《阿佛洛狄忒》或《魅力四射》。

这是其中一些库的一个很好的比较https://medium.com/seek-blog/a-unified-styling-language