【问题标题】:Styled Component Conditionnals not working properly样式化的组件条件不能正常工作
【发布时间】:2019-10-17 21:32:49
【问题描述】:

我希望根据传递给组件的 selected 属性来设置组件的样式。

尽管如此,在 StackOverflow 和 Medium 上的其他答案之后,条件样式不会覆盖基本样式。

我尝试了两种解决方案,我将它们包含在下面的代码中。

Parent.js

 <RoleSelectionCard icon='ux' text='User Experience' selected={true} />

Component.js

import React from "react"
import styled, { css } from "styled-components"
[...]

const Container = styled.div`

  height: 180px;
  width: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 8px;

------- First Variant
  box-shadow: 0 0 10px rgba(74, 134, 232, 0.4); // this one is rendered
  ${props =>
    props.selected &&
    css`
      box-shadow: 0 0 10px rgba(200, 134, 232, 1);
    `};

------- Second Variant
  box-shadow: ${props => {
        if (props.selected === true) {
          return " 0 0 10px rgba(74, 134, 232, 1)"
        }
        return " 0 0 10px rgba(74, 134, 232, 0.4)" // this one is rendered
      }};

  &:hover {
    box-shadow: 0 0 10px rgba(74, 134, 232, 0.8);
    transition: 0.3s;
  }


`
const Icon = styled.img`
  [...]
`
const Text = styled.span`
  [...]
`

class RoleSelectionCard extends React.Component {

  render() {
    console.log(this.props.selected) // outputs true
    const { text, icon } = this.props
    const iconToRender = [...]

    return (
      <div style={{ padding: 50 }}>
        <Container>
          <Icon src={iconToRender} />
          <Text>{text}</Text>
        </Container>
      </div>
    )
  }
}
export default RoleSelectionCard

我没有看到一个愚蠢的错误吗?

提前致谢!

【问题讨论】:

    标签: reactjs styled-components


    【解决方案1】:

    我看到您正在尝试在&lt;Container&gt; 组件样式中使用该属性。要做到这一点,您应该将selected 属性传递给前面提到的&lt;Container&gt; 组件。
    检查下面编辑的代码sn-p:

    import React from "react"
    import styled, { css } from "styled-components"
    [...]
    
    const Container = styled.div`
    
      height: 180px;
      width: 180px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      border-radius: 8px;
    
    ------- First Variant
      box-shadow: 0 0 10px rgba(74, 134, 232, 0.4); // this one is rendered
      ${props =>
        props.selected &&
        css`
          box-shadow: 0 0 10px rgba(200, 134, 232, 1);
        `};
    
      &:hover {
        box-shadow: 0 0 10px rgba(74, 134, 232, 0.8);
        transition: 0.3s;
      }
    
    
    `
    const Icon = styled.img`
      [...]
    `
    const Text = styled.span`
      [...]
    `
    
    class RoleSelectionCard extends React.Component {
    
      render() {
        console.log(this.props.selected) // outputs true
        const { text, icon, selected } = this.props
        const iconToRender = [...]
    
        return (
          <div style={{ padding: 50 }}>
            <Container selected={selected}>
              <Icon src={iconToRender} />
              <Text>{text}</Text>
            </Container>
          </div>
        )
      }
    }
    export default RoleSelectionCard
    

    【讨论】:

    • 确实如此。非常感谢你 !我想我现在要休息了……
    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 2018-08-03
    • 2020-06-09
    • 1970-01-01
    • 2021-10-28
    • 2019-04-21
    • 2021-07-02
    相关资源
    最近更新 更多