[英]Change the color of a card while mapping through an array based on it's value in react?
我想根據當前插槽的值更改卡的顏色,該值是數組中的 object。 因此,當我通過卡片進行映射時,我想根據值更改卡片的背景顏色。 我該怎么做? 我目前的方法似乎不起作用。
slots.map(slot =>
<Card key ={slot._id} style={{backgroundColor:slot.OccupiedStatus?'black':'teal'}}>
<Typography>{slot.category}</Typography>
</Card>
由於Card
是 React 組件,因此在您的情況下, style
不被視為屬性,而是將傳遞給Card
組件的道具。
您可以在Card
組件中捕獲該style
道具並使用它。
const Card = (style, ...otherProps) => {
return <div style={style} >hello</div>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.