Home > Enterprise >  How to dynamically load an image in react
How to dynamically load an image in react

Time:11-30

How can I correctly load this image?

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())
        }})
    }

When I do console.log(this.state.files[0])

I get

backend_address: Blob {size: 736023, type: 'image/png'} image: "/static/media/1.46b58831.png" name: "1.png" [[Prototype]]: Object

When I do the following

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

I get

TypeError: Cannot read properties of undefined (reading 'image')

CodePudding user response:

Try this:

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