【问题标题】:React + Styled Components: Mixing CSS and JS style object?React + Styled Components:混合 CSS 和 JS 样式对象?
【发布时间】:2017-11-19 04:30:55
【问题描述】:

我有一些这样的样式组件:

import styled from 'styled-components';

const Button = styled.button`
  background-color: ${props => props.color};
  border-radius: 12px;
  border: none;
  box-shadow: 0 5px 40px 5px rgba(0,0,0,0.24);
  color: white;
  cursor: pointer;
  font-size: 15px;
  font-weight: 300;
  height: 57px;
  width: 331px;
`;

export default Button;

那里没什么特别的。就像它应该的样式一样。

但是,当我在由其他小组件组成的更大的哑组件中实际使用它们时,我发现自己使用样式对象来设置它们的样式。这是为了添加额外的边距、定位和大小。

const AuthenticationForm = ({
  visible,
}) => {
  return (
    <CenteredOverlay style={{ display: visible ? 'flex' : 'none' }}>
      <Box style={styles.box}>
        <img src={require('../images/logo.png')}
          style={styles.logo}
          alt="logo"
          width="60"
          height="60"
        />
        <TextInput placeholder='email' />
        <TextInput placeholder='password' />
        <PrimaryButton active>
          SIGN IN
        </PrimaryButton>
        <Hr />
        <div style={styles.facebookContainer}>
          <span style={styles.alternateLoginText}>
            You can also sign in with
          </span>
          <Button color='#003D82' style={styles.facebookButton}>
            Facebook
          </Button>
        </div>
      </Box>
    </CenteredOverlay>
  );
}

const styles = {
  alternateLoginText: {
    fontSize: '12px',
    color: '#FFFFFF',
    opacity: '0.6',
    marginRight: '15px',
  },
  facebookContainer: {
    padding: '12px',
    display: 'flex',
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'center',
    alignItems: 'center',
  },
  facebookButton: {
    width: '222px',
    height: '42px',
  },
  logo: {
    position: 'absolute',
    top: -30,
  },
  box: {
    position: 'relative',
    width: '447px',
  },
  container: {
    textAlign: 'center',
    alignSelf: 'center',
  },
}

export default AuthenticationForm;

使用样式化的组件感觉很奇怪而且有点适得其反,这应该让我的生活更轻松?现在我必须用两种方式写东西?

如果我做错了事,请赐教。

【问题讨论】:

    标签: reactjs styled-components


    【解决方案1】:

    是的,您错过了如何实际使用样式化组件,如果您想使用样式化组件设置任何其他组件的样式,您可以使用以下模式:

    const ButtonStyle = styled(Button)`
        color: #003D82;
        width: '222px';
        height: '42px';
    `;
    
    <ButtonStyle>
         Facebook
     </ButtonStyle>
    

    照常使用

    更多here

    【讨论】:

    • 但是定位呢?就像有时我希望组件具有margin-bottom:一个组件中的10px,宽度:另一个组件中的100px,等等
    • 是的,您可以使用这种方法来做到这一点,在这种情况下,如果您将margin-bottom: 10px 提供给Link,它将将此属性应用于组件的根元素,在这种情况下为a
    • 但是我不是必须 1) 每次我想更改边距时创建一个特殊的 Link 组件还是 2) 公开一个 margin-bottom 道具?它们似乎都不比使用style={styles.link} 方法更好
    • 不,为什么要创建Link 组件,您只是将其与我的示例混淆了,在您的情况下,您可以使用const ButtonStyle = styled(Button)
    • 是的,但我的意思是,如果我有 10 个不同的按钮,并且它们的唯一区别是 margin-top 每个按钮 1px,我将如何构建它?
    【解决方案2】:

    您可以使用extend 方法,无需在两种方式之间混合设置组件样式!

    import styled from 'styled-components';
    
    const Button = styled.button`
      background-color: ${props => props.color};
      ...etc
    `;
    
    export default Button;
    

    const FacebookButton = Button.extend`
       width: '222px';
       height: '42px';
       color='#003D82'
    `;
    const AuthenticationForm = ({
      visible,
    }) => (
      <FacebookButton>
        Facebook
      </FacebookButton>
    )
    

    【讨论】:

    • 我关心的不是 facebook 按钮,而是必须一半使用 JS 中的样式对象和一半使用 CSS 来设置样式
    • 是的,我只是向您展示了如何仅使用一种方式的示例!不用混,什么都适用!
    猜你喜欢
    • 2021-08-26
    • 2018-10-20
    • 2019-12-21
    • 2019-12-21
    • 1970-01-01
    • 2021-04-13
    • 2019-10-16
    • 2020-08-31
    • 2021-11-04
    相关资源
    最近更新 更多