如何为我的卡片列表中的每张卡片使用不同的背景颜色?

huangapple go评论68阅读模式
英文:

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 (
  &lt;Container&gt;
    &lt;Row className=&quot;row gy-4 mt-4&quot;&gt;
      {profsData &amp;&amp; profsData.data.map((item) =&gt; (
        &lt;Col key={item.id} className=&#39;col-3&#39;&gt;
          &lt;Card className=&#39;h-100 w-100 py-5&#39;&gt;
            &lt;Card.Body&gt;
              &lt;Card.Title&gt;{item.username}&lt;/Card.Title&gt;
              &lt;Card.Text&gt;
                {item.bio}
              &lt;/Card.Text&gt;
              {item.college &amp;&amp; (&lt;h6 className=&#39;fw-bold mt-5&#39;&gt;Coll&#232;ge&lt;/h6&gt;)}
              {item.lycee &amp;&amp; (&lt;h6 className=&#39;fw-bold mb-5&#39;&gt;Lyc&#233;e&lt;/h6&gt;)}
              &lt;Button variant=&quot;primary&quot; onClick={() =&gt; handleShow(item.id)}&gt;Rendez-Vous&lt;/Button&gt;
            &lt;/Card.Body&gt;
          &lt;/Card&gt;
        &lt;/Col&gt;
      ))}
    &lt;/Row&gt;
  &lt;/Container&gt;
)

and here is the array of color with the function selecting randomly a color:

const variant = [
  &#39;Primary&#39;,
  &#39;Secondary&#39;,
  &#39;Success&#39;,
  &#39;Danger&#39;,
  &#39;Warning&#39;,
  &#39;Info&#39;,
  &#39;Light&#39;,
  &#39;Dark&#39;,
]

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 (
  &lt;Container&gt;
    &lt;Row className=&quot;row gy-4 mt-4&quot;&gt;
      {profsData?.data.map((item) =&gt; {
        const randIndex = Math.floor(Math.random() * variant.length)
        const randomVariant = variant[randIndex];

        return (
          &lt;Col key={item.id} className=&#39;col-3&#39;&gt;
            ... use randomVariant in JSX ...
          &lt;/Col&gt;
        );
      })}
    &lt;/Row&gt;
  &lt;/Container&gt;
);

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 (
  &lt;Container&gt;
    &lt;Row className=&quot;row gy-4 mt-4&quot;&gt;
      {profsData?.data.map((item, index) =&gt; {
        const selectedVariant = variant[index % variant.length];

        return (
          &lt;Col key={item.id} className=&#39;col-3&#39;&gt;
            ... use selectedVariant in JSX ...
          &lt;/Col&gt;
        );
      })}
    &lt;/Row&gt;
  &lt;/Container&gt;
);

答案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 = [
    &#39;Primary&#39;,
    &#39;Secondary&#39;,
    &#39;Success&#39;,
    &#39;Danger&#39;,
    &#39;Warning&#39;,
    &#39;Info&#39;,
    &#39;Light&#39;,
    &#39;Dark&#39;,
  ]    
{profsData &amp;&amp; profsData.data.map((item,index) =&gt; (
          &lt;Col key={item.id} className=&#39;col-3&#39;&gt;
            &lt;Card variant={variant[index] ? variant[index] : variant[0]} className=&#39;h-100 w-100 py-5&#39;&gt;
              ...
              &lt;/Card.Body&gt;
            &lt;/Card&gt;
          &lt;/Col&gt;
        ))}

huangapple
  • 本文由 发表于 2023年1月9日 18:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定