【问题标题】:How to supply background color as a prop for cards using Material UI in React JS?如何在 React JS 中使用 Material UI 为卡片提供背景颜色作为道具?
【发布时间】:2018-07-31 22:54:14
【问题描述】:

我正在使用Material UI next 并围绕卡片组件构建一个包装器。这允许我自定义组件。 我能够扩展组件,以便卡片内的标题和图像可以作为道具发送。但是,使用 JS in CSS 技术注入到 classes 属性中的背景颜色,我无法找到将背景颜色作为道具发送的方法。

使用JSS技术设置的类如下:

    const styles = {
      card: {
             maxWidth: 345,
             backgroundColor: '#hexcodehere'
      },

组件如下图:

    const { classes,label } = props;        
    <Card className={classes.card}
      label={label}
    >
      <CardText />
      <CardMedia />
    </Card>

如何使用道具设置背景颜色?

【问题讨论】:

    标签: javascript css reactjs material-ui jss


    【解决方案1】:

    使用 classnames 包在 React 组件上实现自定义样式。

    import classnames from 'classnames';
    
    const { classes, label, backgroundColor } = props;  
    <Card className={classnames(classes.card)} style={{ backgroundColor }}
       label={label}
    >
      <CardText />
      <CardMedia />
    </Card>
    

    这个backgroudColor 属性可以是 CSS 支持的任何字符串。 例如:

    • '#f0f' (#rgb)
    • '#ff00ff' (#rrggbb)
    • 'rgb(255, 0, 255)'
    • 'rgba(255, 255, 255, 1.0)'

    【讨论】:

    • 你提到的是对的,但是我的问题是你如何将背景颜色作为道具传递(我将标签名称作为道具传递的方式,所以在这种情况下可能是十六进制代码作为道具)将此作为独立组件与其他组件一起使用。
    • 这将是任何支持 css 的字符串。我更新了我的答案
    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2018-07-17
    • 2020-04-27
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多