reactjs 如何在react中显示对象列表中的第一个图像

eblbsuwk  于 2022-11-22  发布在  React
关注(0)|答案(2)|浏览(99)

我正在传递一个名为product的道具,它在变量images中有一个对象列表。我想在前端显示id号为1的第一个图像。另外,如果我想遍历图像,最好的方法是什么。我已经将在postman上看到的对象图像作为照片附加。我正在尝试在card〈Card.Img src={product.images} /〉中显示这一行

{
    function Product({ product }) {
    return (
        
        <Card className="my-3 p-3 rounded">
            <Link to={`/product/${product._id}`}>
                <Card.Img src={product.images} />
            </Link>

            <Card.Body>
                <Link to={`/product/${product._id}`}>
                    <Card.Title as="div">
                        <strong>{product.name}</strong>
                    </Card.Title>
                </Link>

                <Card.Text as="div">
                    <div className="my-3">
                        <Rating value={product.rating} text={`${product.numReviews} reviews`} color={'#f8e825'} />
                    </div>
                </Card.Text>

                <Card.Text as="h3">
                    ${product.price}
                </Card.Text>
            </Card.Body>
        </Card>
    )
}

svmlkihl

svmlkihl1#

试试这个,{产品.图像[0].图像}

u91tlkcl

u91tlkcl2#

<Card.Img src={product.images[0].image} />

将是显示第一个图像的解决方案。
第二,如果我没理解错的话,你应该试着把一个循环

<Link to={`/product/${product._id}`}>
    <Card.Img src={product.images} />
</Link>

并相应地呈现数据

相关问题