【问题标题】:Styled Components: nesting and referring to other components at the same time样式化组件:同时嵌套和引用其他组件
【发布时间】:2018-04-15 17:35:55
【问题描述】:
<FooterLight transparent={true}/>

是否可以在 FooterLight 的定义中有一个嵌套规则,用于评估 props 值透明。然后将'color: white'分配给它的子ChangelogVersion和CopyRight?

接下来的两个问题:

  1. 因为 color: white !important 对于 ChangelogVersion 和 CopyRight 是相同的。可以将它们合并到一个语句中吗?
  2. &&& 可以不使用 !important 吗?

export const FooterLight = styled(Navbar).attrs({fixed: 'bottom'})`
  background-color: ${props => props.transparent
  ? 'transparent'
  : 'white'};

  ${props => props.transparent && ChangelogVersion} {
    color: white !important;
  }

  ${props => props.transparent && CopyRight} {
    color: white !important;
  }
`


export const ChangelogVersion = styled(NavLink)`
  &&& {
    font-size: 14px !important;
    padding-right: .5rem;
  }
`

export const CopyRight = styled.div `
  padding: .5rem 0;
  color: '#777777';
  font-size: 14px;
}

【问题讨论】:

    标签: javascript css reactjs styled-components


    【解决方案1】:

    当然可以……你可以这样做:

    export const FooterLight = styled(Navbar).attrs({fixed: 'bottom'})`
        background-color: ${props => props.transparent
        ? 'transparent'
        : 'white'};
    
        ${ChangelogVersion}, ${CopyRight} {
             ${props => props.transparent && "color: white !important;"}
        }
    `
    

    至于您的第二个陈述,&amp;&amp;&amp; 可能有效,但您最好更好地构建 CSS,因此它不需要首先是 !important...在您的示例中,没有理由任何importants 都在那里,所以很难提供建议。

    【讨论】:

    • 谢谢。按要求工作。我只需要在 FooterLight 之前重新排序元素以显示 ChangelogVersion 和 CopyRight。还有一个问题:你也可以通过FooterLight的prop元素来编辑ChangelogVersion的属性吗?
    猜你喜欢
    • 2020-09-22
    • 2020-09-29
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2021-04-21
    • 2016-06-21
    • 2023-04-09
    • 2021-08-15
    相关资源
    最近更新 更多