简体   繁体   English

图像未显示在列表中,反应

[英]Image not displaying inside List, React

I am creating a simple React component that displays images in a list/array.我正在创建一个简单的 React 组件,它在列表/数组中显示图像。

The source is shown below:源码如下图:

import React from "react";
import styled from "styled-components";
import burgerTop from "../../assets/images/burgerTop.png";
import burgerBottom from "../../assets/images/burgerBottom.png";

const BurgerComponent = props => {
  console.log(JSON.stringify(props));
  const BurgerDiv = styled.div`
    width: 500px;
    margin: 0 auto;
    text-align: center;
    display: flex;
    flex-direction: column;
  `;
  return (
    <BurgerDiv>
      {props.burger.map(burgerItem => {
        console.log(burgerItem);
        const imgSrc = burgerItem.img;
        console.log(imgSrc);
        return <img key={burgerItem.id} src={imgSrc} />;
      })}
    </BurgerDiv>
  );
};

export default BurgerComponent;

Props are shown as below道具如下图

{
  "burger":[
    {
      "type":"top",
      "img":"burgerTop",
      "value":1,
      "id":"sss2",
      "addable":false
    },
    {
      "type":"bottom",
      "img":"burgerBottom",
      "value":1,
      "id":"aaa7",
      "addable":false
    }
  ]
}

But the image path is not taken from import.但是图像路径不是从导入中获取的。

Not sure what I am doing wrong.不知道我做错了什么。

Please help.请帮忙。

img 's source property should be a URL, or base64 string. img的 source 属性应该是一个 URL,或者 base64 字符串。 Your burger prop doesn't have a source property like that.你的burger道具没有这样的源属性。

I noticed that you are importing burgerTop and burgerBottom from assets folder.我注意到您正在从assets文件夹中导入burgerTopburgerBottom Maybe you can use those images like this:也许你可以像这样使用这些图像:

{props.burger.map(burgerItem => {
        const imgSrc = burgerItem.img === "burgerTop" ? burgerTop : burgerBottom ;
        return <img key={burgerItem.id} src={imgSrc} />;
      })}

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

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