I am learning react and I am trying to fetch a image file from a retsfull API and display the image. I am converting the responce file in a blob format and try to read it with file reader and then set the result on a state variable, when I do that I get a broken image in react. Bellow is the code:
getData() {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => {
var myblob = new Blob([xhr.response], {
type: ["image/png", "image/jpeg", "image/gif"]
});
let reader = new FileReader()
reader.onloadend = () => {
this.setState({
postPicture: myblob,
postPicturePreview: reader.result
})
}
reader.readAsDataURL(myblob)
})
xhr.open('GET','https://DigitalSelfassuredCrash.kirilkuzmanovsk.repl.co')
xhr.send()
}
Can someone check this, I have tried to find a solution but I am stuck.
thanks