gpt4 book ai didi

reactjs - React Bootstrap Cards 和 CardDeck 组件 - 响应式布局

转载 作者:行者123 更新时间:2023-12-04 00:54:25 26 4
gpt4 key购买 nike

我正在使用 React Bootstrap 并使用 Axios 从 API 中提取数据。我正在尝试在引导卡/CarDeck 组件中显示来自该 API 的数据。我的大部分工作都使用下面的代码,但我在布局方面遇到了问题。

我希望卡片组能够响应并根据屏幕大小每行显示灵活数量的卡片。目前,如果响应中有很多项目,则每张卡片都非常薄,无法查看。执行此操作的正确方法是什么?

import React, { useState, useEffect } from "react";
import CardDeck from "react-bootstrap/CardDeck";
import Card from "react-bootstrap/Card";
import axios from "axios";
import Container from "react-bootstrap/Container";

const CardColumns = () => {
const [cardInfo, setData] = useState([]);
console.log(cardInfo);

useEffect(() => {
axios
.get(
"https://webhooks.mongodb-realm.com/api/client/v2.0/app/cards-fvyrn/service/Cards/incoming_webhook/getAllCards"
)
.then((response) => {
setData(response.data);
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}, []);

const renderCard = (card, index) => {
return (
<Card style={{ width: "18rem" }} key={index} className="box">
<Card.Img variant="top" src="holder.js/100px180" src={card.image} />
<Card.Body>
<Card.Title>
{index} - {card.manufacturer}
</Card.Title>
<Card.Text>{card.player}</Card.Text>
</Card.Body>
</Card>
);
};

return <CardDeck>{cardInfo.map(renderCard)}</CardDeck>;
};
export default CardColumns;

最佳答案

最后,这是我在下面采用的解决方案。我认为的关键是:

  1. 将 cardDeck 包装在 <Container> 中然后用 <Col className="container-fluid mt-4"> 包装卡片:

`

const renderCard = (card, index) => {
return (`
<Col className="container-fluid mt-4">
{/* <Card key={index} className="box"> */}
<Card style={{ width: "18rem" }} key={index} className="box">
<Card.Header>
{card.brand} - {card.series}
</Card.Header>
<Card.Img variant="top" src={card.front_image} fluid />
<Card.Body>
<Card.Title>
{card.player} (#
{card.card_number.$numberDouble}) {card.variation}
</Card.Title>
{/* <Card.Text className="d-flex">{card.player}</Card.Text> */}
{/* <Card.Text>{card.player}</Card.Text> */}
</Card.Body>
<ListGroup className="list-group-flush">
<ListGroupItem>
Print Run - {card.print_run.$numberDouble}
</ListGroupItem>
<ListGroupItem>Career Stage - {card.career_stage} </ListGroupItem>
<ListGroupItem>For Trade - {card.forTrade}</ListGroupItem>
</ListGroup>
<Card.Footer className="text-muted">{card.team}</Card.Footer>
</Card>
</Col>

//{/* </Col> */}
);
};

return (
<Container>
<Button variant="primary">Filter By Brand</Button>{" "}
<Button variant="primary">Filter By Player</Button>{" "}
<Button variant="primary">Filter By Team</Button>
<CardDeck>{cardInfo.map(renderCard)}</CardDeck>
</Container>
);`

关于reactjs - React Bootstrap Cards 和 CardDeck 组件 - 响应式布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63748312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com