代码之家  ›  专栏  ›  技术社区  ›  Joseph Adam

如何在react中动态加载图像

  •  0
  • Joseph Adam  · 技术社区  · 4 年前

    如何正确加载此图像?

    import image_1 from "../../static_media/1.png"
    
    handleClick = async (e) => {
        
        this.loadImage({
            "0": {
                name: "1.png",
                image: image_1,
                backend_address: await fetch(image_1).then(res => res.blob())
            }})
        }
    

    当我这样做的时候 console.log(this.state.files[0])

    我明白了

    “/static/media/1.46b58831.png”名称:“1.png”[[Prototype]]:对象

    当我做以下事情时

    <Image src={this.state.files[0].image} />
    

    TypeError:无法读取未定义的属性(读取“图像”)

    1 回复  |  直到 4 年前
        1
  •  1
  •   Alen Vlahovljak    4 年前

    试试这个:

      <Image src={this.state.files[0]?.image} />
    
    推荐文章