英文:
How can I use a different background color for every card of my card list?
问题
我有一个cardList,我想为cardList中的每张卡片使用不同的颜色。我试图自己实现,但变得复杂。
这是cardList:
return (
<Container>
<Row className="row gy-4 mt-4">
{profsData && profsData.data.map((item) => (
<Col key={item.id} className='col-3'>
<Card className='h-100 w-100 py-5'>
<Card.Body>
<Card.Title>{item.username}</Card.Title>
<Card.Text>
{item.bio}
</Card.Text>
{item.college && (<h6 className='fw-bold mt-5'>Collège</h6>)}
{item.lycee && (<h6 className='fw-bold mb-5'>Lycée</h6>)}
<Button variant="primary" onClick={() => handleShow(item.id)}>Rendez-Vous</Button>
</Card.Body>
</Card>
</Col>
))}
</Row>
</Container>
)
这是颜色数组以及选择随机颜色的函数:
const variant = [
'Primary',
'Secondary',
'Success',
'Danger',
'Warning',
'Info',
'Light',
'Dark',
]
const index = Math.floor(Math.random() * variant.length)
const colorPicked = variant[index]
我正在使用map()
函数来显示profsData数组中的数据,我无法在第一个map()
函数内部再使用另一个map()
。
英文:
I have a cardList and I want to use a different color for every card in the cardList. I tried to do it by my self but it became complicated.
Here is the cardList:
return (
<Container>
<Row className="row gy-4 mt-4">
{profsData && profsData.data.map((item) => (
<Col key={item.id} className='col-3'>
<Card className='h-100 w-100 py-5'>
<Card.Body>
<Card.Title>{item.username}</Card.Title>
<Card.Text>
{item.bio}
</Card.Text>
{item.college && (<h6 className='fw-bold mt-5'>Collège</h6>)}
{item.lycee && (<h6 className='fw-bold mb-5'>Lycée</h6>)}
<Button variant="primary" onClick={() => handleShow(item.id)}>Rendez-Vous</Button>
</Card.Body>
</Card>
</Col>
))}
</Row>
</Container>
)
and here is the array of color with the function selecting randomly a color:
const variant = [
'Primary',
'Secondary',
'Success',
'Danger',
'Warning',
'Info',
'Light',
'Dark',
]
const index = Math.floor(Math.random() * variant.length)
const colorPicked = variant[index]
I'm using a map()
function to display the data in the profsData array and I can't use another map()
for the variant array inside the first map()
function.
答案1
得分: 1
以下是翻译好的部分:
如果您想要伪随机颜色,然后为每张映射的卡计算一个随机颜色。
return (
<Container>
<Row className="row gy-4 mt-4">
{profsData?.data.map((item) => {
const randIndex = Math.floor(Math.random() * variant.length)
const randomVariant = variant[randIndex];
return (
<Col key={item.id} className='col-3'>
... 在 JSX 中使用 randomVariant ...
</Col>
);
})}
</Row>
</Container>
);
如果您只想为每张卡片使用不同的颜色,然后使用映射的数据索引并选择一个变种颜色。通过将映射的索引取模 variant
数组长度,确保在数组内获得有效的索引。
return (
<Container>
<Row className="row gy-4 mt-4">
{profsData?.data.map((item, index) => {
const selectedVariant = variant[index % variant.length];
return (
<Col key={item.id} className='col-3'>
... 在 JSX 中使用 selectedVariant ...
</Col>
);
})}
</Row>
</Container>
);
英文:
If you want a pseudo-random color then compute a random color for each card that is mapped.
return (
<Container>
<Row className="row gy-4 mt-4">
{profsData?.data.map((item) => {
const randIndex = Math.floor(Math.random() * variant.length)
const randomVariant = variant[randIndex];
return (
<Col key={item.id} className='col-3'>
... use randomVariant in JSX ...
</Col>
);
})}
</Row>
</Container>
);
If you just want a different color per card then use the mapped data index and select a variant color. Taking the modulus of the mapped index by variant
array length ensures a valid index within the array.
return (
<Container>
<Row className="row gy-4 mt-4">
{profsData?.data.map((item, index) => {
const selectedVariant = variant[index % variant.length];
return (
<Col key={item.id} className='col-3'>
... use selectedVariant in JSX ...
</Col>
);
})}
</Row>
</Container>
);
答案2
得分: 0
const variant = [
'主要',
'次要',
'成功',
'危险',
'警告',
'信息',
'浅色',
'深色',
]
{profsData && profsData.data.map((item, index) => (
<Col key={item.id} className='col-3'>
<Card variant={variant[index] ? variant[index] : variant[0]} className='h-100 w-100 py-5'>
...
</Card>
</Col>
))}
英文:
const variant = [
'Primary',
'Secondary',
'Success',
'Danger',
'Warning',
'Info',
'Light',
'Dark',
]
{profsData && profsData.data.map((item,index) => (
<Col key={item.id} className='col-3'>
<Card variant={variant[index] ? variant[index] : variant[0]} className='h-100 w-100 py-5'>
...
</Card.Body>
</Card>
</Col>
))}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论