You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为映射生成的卡片集合添加计数及Card.Group的itemsPerRow配置问题

Fixing itemsPerRow={4} for Your Card Group + Adding Card Count

Got it, let's break down why your itemsPerRow={4} isn't working and fix this up, plus add that card count you mentioned.

The Problem with Your Current Code

Right now, you're generating a separate Card.Group for every single sensor inside your map loop. Each group only contains one card, so the itemsPerRow property has no rows to manage—it can't split a single card into rows! That's why it feels like the property isn't doing anything.

The Fix: Wrap All Cards in One Card.Group

Move the Card.Group outside of the map function, so all your mapped cards live inside a single group. That way, itemsPerRow={4} will correctly handle how many cards show up per row. We'll also add the card count using the index parameter from the map function.

Here's the updated code:

<div>
  <Card.Group itemsPerRow={4}>
    {this.props.sensors.map((sensor, index) => {
      return (
        <Card key={sensor.id || index}> {/* Always add a unique key for mapped elements! */}
          <Card.Content>
            <Card.Header>Hi #{index + 1}</Card.Header> {/* Added 1-based count here */}
            {/* Add more sensor-specific content here if needed */}
          </Card.Content>
        </Card>
      );
    })}
  </Card.Group>
</div>

Key Notes:

  • Unique key Prop: I added a key to each Card—React requires this for mapped elements to efficiently update the DOM. Use a unique identifier from your sensor object (like sensor.id) if available, otherwise fall back to index.
  • Count Integration: The index + 1 gives you a clean 1-based count for each card (since index starts at 0). Adjust the formatting (like Sensor #{index + 1}) to match your needs.
  • itemsPerRow Placement: Now that all cards are in one group, this property will control row layout exactly as the documentation specifies.

内容的提问来源于stack exchange,提问作者Harendrra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:02:38