为映射生成的卡片集合添加计数及Card.Group的itemsPerRow配置问题
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
keyProp: I added akeyto eachCard—React requires this for mapped elements to efficiently update the DOM. Use a unique identifier from yoursensorobject (likesensor.id) if available, otherwise fall back toindex. - Count Integration: The
index + 1gives you a clean 1-based count for each card (sinceindexstarts at 0). Adjust the formatting (likeSensor #{index + 1}) to match your needs. itemsPerRowPlacement: Now that all cards are in one group, this property will control row layout exactly as the documentation specifies.
内容的提问来源于stack exchange,提问作者Harendrra

