next.js - React Bootstrap Card component is vertical when mapping data
问题描述
I am using React/NextJS and have created a component that is only showing the cards vertically on full-screen.
How can I have the Cards group horizontally rather than vertical when mapping the data (there are 4 inputs, and i'd like 3 per row.)
I have tried to use and but with no luck. Perhaps I am doing it in the wrong place?
Card Component:
import React from "react";
import { Card } from "react-bootstrap";
function VendorCard(props) {
return (
<Card style={{ width: "18rem" }}>
<Card.Body>
<Card.Title>{props.name}</Card.Title>
<Card.Text>{props.country}</Card.Text>
</Card.Body>
<Card.Footer>
<small className='text-muted'>{props.isActive}</small>
</Card.Footer>
</Card>
);
}
export default VendorCard;
List Component:
function VendorList(vendor) {
return (
<div>
<VendorCard
key={vendor.id}
name={vendor.name}
country={vendor.country}
classification={vendor.classification}
riskLevel={vendor.riskLevel}
signedDocuments={vendor.signedDocuments}
isActive={vendor.isActive}
/>
</div>
);
}
export default VendorList;
HomePage:
import VendorList from "../components/vendor-list";
import vendors from "../dummy-data";
import { Container } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.css";
function Home() {
return (
<Container style={{ justifyContent: "center" }}>
<div>{vendors.map(VendorList)}</div>
</Container>
);
}
export default Home;
Any assistance would be much appreciated!
解决方案
In react-boostrap
you must use the <Row>
and <Col>
components. Col with value 4
makes the card item get 1/3 of row space, then automatically accommodates 3 in each row.
Home
import React from "react";
import VendorCard from "./VendorCard";
import vendors from "./dummy-data";
import { Container, Row } from "react-bootstrap";
function App() {
return (
<Container>
<Row className="justify-content-md-center">
{vendors.map((vendor) => (
<VendorCard key={vendor.name} {...vendor} />
))}
</Row>
</Container>
);
}
export default App;
VendorCard
import React from "react";
import { Card, Col } from "react-bootstrap";
function VendorCard(props) {
return (
<Col xs="4">
<Card>
<Card.Body>
<Card.Title>{props.name}</Card.Title>
<Card.Text>{props.country}</Card.Text>
</Card.Body>
<Card.Footer>
<small className="text-muted">{props.isActive}</small>
</Card.Footer>
</Card>
</Col>
);
}
export default VendorCard;
Dummy data
module.exports = [
{
name: "Cassio",
country: "Brazil",
isActive: false
},
{
name: "Paul",
country: "Brazil",
isActive: false
},
{
name: "Ben",
country: "Brazil",
isActive: false
},
{
name: "Jonnas",
country: "Brazil",
isActive: false
}
];
推荐阅读
- sql - 用于替换字符串并将字符串的其余部分转换为 Int 的 SQL 查询
- angular - 无法使用角度材料以角度自定义网格图块
- ios - Playground 中的引用计数
- amazon-web-services - EC2 用户数据 - 在用户数据中满足所有条件后禁用它
- excel - 在 Excel 中使用 VBA,如何在 Outlook 中粘贴表格然后将表格转换为文本?
- smartsheet-api - Smartsheet - 使用 c# sdk 在单元格中添加具有多个值的行
- flutter - 导航过渡在未来期间挂起
- excel - 选择范围顶部的单元格
- python - Pandas 循环遍历 Excel 工作表并附加到 df
- php - 如何将一天中的时间分成 3 个时段:早上、下午和晚上?