gpt4 book ai didi

javascript - ReactJs 中的内联 CSS 样式

转载 作者:行者123 更新时间:2023-11-30 14:23:22 25 4
gpt4 key购买 nike

我在 React App 中设置内联 CSS 样式时遇到了麻烦。因为我想根据数组中的数据更改卡片的背景颜色,但没有这样做。我正在使用一个具有 switch 语句的变量并以 as Inline 样式传递它,但背景颜色没有改变。任何人都可以帮助我解决这个问题或告诉我另一种解决方法。谢谢。

这是我的 Card.js

import React , { Component } from 'react'

export default class Card extends Component {
render(){
const {title , date , description , color } = this.props.node
let cardClass = "card-wrapper"
let myColor = ""
switch(color) {
case 'red':
myColor = 'color--red'
break;
case 'blue':
myColor = 'color--blue'
break;
case 'yellow':
myColor = 'color--yellow'
break;
case 'darkBlue':
myColor = 'color--darkBlue'
break;
default:
break;
}

return (
<div style={{ backgroundColor: myColor }}>
<div className={cardClass}>
<div className="card">
<h5 className="card-title">{title}</h5>
<p>Created on {date}</p>
<p className="card-text">{description}</p>
</div>
</div>
</div>
)
}
}

这是我的 App.css 文件

.card-wrapper {
width: 209px;
margin: .4%;
float: left;
background-color: #5cb85c;
color: white;
padding: 16px;
border-radius: 1px;
}

.color--red {
background-color: #d9534f;
}
.color--blue{
background-color: #5bc0de;
}
.color--darkBlue{
background-color: #428bca;
}
.color--yellow {
background-color: #FFD333;
}

这是我的 data.js 文件

export const data = [
{
title : 'title',
date : '1537032686201',
description : 'Some quick example text to build on the card title and make up the bulk of the cards content.',
color:'red'
},
{
title : 'title',
date : '1537032686202',
description : 'Some quick example text to build on the card title and make up the bulk of the cards content.',
color:'blue'
},
{
title : 'title',
date : '1537032686203',
description : 'Some quick example text to build on the card title and make up the bulk of the cards content.',
color:'darkBlue'
},
{
title : 'title',
date : '1537032686204',
description : 'Some quick example text to build on the card title and make up the bulk of the cards content.',
color: 'yellow'
},
]

最佳答案

您应该使用className 而不是style:

return (
<div className={myColor}>

关于javascript - ReactJs 中的内联 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52357438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com