【问题标题】:How can i show my products cards in horizontally?如何横向展示我的产品卡片?
【发布时间】:2020-11-19 15:14:19
【问题描述】:

产品卡片

import React from 'react'
import { Card, Container, Row, Col, ListGroup, Button} from 'react-bootstrap'

function ProductCard(props){
    return(
        <div>
            <Container>
                <Row>
                    <Col md={3} xs={6}>
                        <Card border="primary" style={{widht:'14rem'}}>
                            <Card.Img variant="top" src={props.product.image}/>
                            <Card.Body>
                                <Card.Title>{props.product.name}</Card.Title>
                                <Card.Text>Price: ${props.product.price}, Quantity: {props.product.quantity}
                                </Card.Text>
                                <Button variant="primary">Add to Cart</Button>
                            </Card.Body>
                        </Card>
                    </Col>
                </Row>
            </Container>
            
        </div>
    )
}

export default ProductCard

首页

import Axios from 'axios';
import React, { useEffect, useState } from 'react';
import {Container, Row, Col, Image, Card, Button, CardDeck} from 'react-bootstrap';
import axios from 'axios';
import ProductCard from './ProductCard';

    enter code here


function Home(){
    const url = 'http://localhost:8888/ProgWeb/public/api/products'
    const [product, setProduct]=useState({
        loading: false,
        data: null,
        error: false
    })

    useEffect(() => {
        setProduct({
            loading:true,
            data: null,
            error: false
        })
        axios.get(url)
        .then(response => {
            setProduct({
                loading: false,
                data: response.data,
                error: false
            })
        })
        .catch(() =>{
            setProduct({
                loading: false,
                data: null,
                error: true
            })
        })



    }, [url])

    let content = null

    if(product.data){
        content = 
        product.data.map((product, key) => 
            <div  key={product.id}>
                <ProductCard
                product={product} 
                />
            </div>
        )
    }
  
    return (
        <div>
        {content}
        </div>
    );
}

export default Home;

现在这是我第一次使用 react-bootstrap。所以我这里没有太多线索。

我想要的是以连续三张卡片的方式生成卡片。

现在这是我到目前为止所做的代码,但我对如何将卡片设置为水平感到困惑,我已经输入了引导程序,但我仍然可以垂直获取产品

【问题讨论】:

标签: css reactjs react-bootstrap


【解决方案1】:
  1. 在您的 ProductsCard 组件中,给您的 div 容器一个 id,它可以是任何东西。示例:
function ProductCard(props){
   return(
       <div id=“unique-name”>
           <Container>
               <Row>
                    ...
               </Row>
           </Container>
           
       </div>
   )
}
  1. 现在转到您的 css 文件并添加以下内容:
#unique-name {
    display: flex;
    flex-direction: row;
    justify-content: space-around

}

我们正在将 Container 设置为 flexbox 并将方向更改为水平。 参考:https://css-tricks.com/almanac/properties/f/flex-direction/

或者:

您可以像这样简单地将 class="row" 添加到 div 元素中:


if(product.data){
        content = 
        product.data.map((product, key) => 
            <div class=“row” key={product.id}>
   
                <ProductCard
                product={product} 
                />
   
            </div>
        )
    }

如果这有帮助,请告诉我!

【讨论】:

  • 然后我知道最终的解决方案,但是它有点工作。我以前用过它,它可以在这里工作:stackoverflow.com/questions/62533219/…
  • 您的控制台似乎记录了错误,您能分享一下错误吗?
猜你喜欢
  • 2014-11-26
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多