Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to dynamically load an image in react

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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’)

>Solution :

Try this:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading