简体   繁体   English

Material UI CardMedia 不显示图像

[英]Material UI CardMedia is not showing images

I'm using CardMedia to display images on the screen but it is not showing it.我正在使用 CardMedia 在屏幕上显示图像,但它没有显示出来。

I have gone through the similar question on Stack Overflow.我已经在 Stack Overflow 上完成了类似的问题。 There the solution give is to import the image and then use it.解决方案是导入图像然后使用它。 But here I'm fetching image url from backend using API call.但在这里我使用 API 调用从后端获取图像 url 。

I have also tried changng heights but it didn't work.我也试过changng heights,但没有用。

Can anyone explain what is the issue?谁能解释是什么问题?

import React from "react";
import axios from "axios";

import Card from "@material-ui/core/Card";
import CardHeader from "@material-ui/core/CardHeader";
import CardMedia from "@material-ui/core/CardMedia";
import CardContent from "@material-ui/core/CardContent";
import CircularProgress from "@material-ui/core/CircularProgress";

import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles((theme) => ({
  root: {
    maxWidth: 345,
  },
  media: {
    height: 0,
    paddingTop: "56.25%",
  },
}));

function ProductList(props) {
  const [productList, setProductList] = React.useState([]);
  const [loading, setLoading] = React.useState(true);
  const classes = useStyles();

  const url = `https://api.growcify.com/dev/product/list/${props.listId}`;

  const getList = () => {
    axios.get(url).then((res) => {
      console.log(res.data);
      setProductList(res.data);
      setLoading(false);
    });
  };

  React.useEffect(() => {
    getList();
  }, []);

  return !loading ? (
    productList.length > 0 ? (
      productList.map((list) => (
        <Card className={classes.root}>
          <CardContent>{list.name}</CardContent>
          {list.photos.map((img) => {
            img !== null && (
              <Card className={classes.root}>
                {console.log(img)}
                <CardMedia
                  image={img}
                  component="img"
                  title="Some title"
                  className={classes.media}
                />
              </Card>
            );
          })}
        </Card>
      ))
    ) : (
      <h2>No Data Available </h2>
    )
  ) : (
    <CircularProgress />
  );
}

export default ProductList;

In the provided Screenshot, You can see in the console that I'm getting image url but image is not showing there.在提供的屏幕截图中,您可以在控制台中看到我正在获取图像 url 但图像没有显示在那里。

在此处输入图像描述

Remove height: 0, from media style
image={"http://localhost:3000/yourPath/"+this.state.your-data.your-variable name} 

Dont forget to padding top Tell me if it works:D不要忘记填充顶部告诉我它是否有效:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM