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

Upload and display image file through via input tag in react-redux

I have an input tag inside a component that uploads a singe image.

<input 
      type="file"
      accept=".png,.jpeg"
      onChange={e => dispatch(setProfileImage(e.target.files[0]))}
/>

From what I can see, the image gets saved in the state after I dispatch an action and save it to the state through a reducer.

My problem is this: In another component, I access the image in the state via useSelector and use it as the source in an image tag, but the image appears broken.
Basically, I just want to be able to use the image saved in the state as the source of an image tag.

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

Any explanation, tutorial, or link be greatly appreciated. I’m trying to avoid using backend as the application I am working on is very small and there is no need for one but am willing to if that’s the easiest answer.

Thank you!

>Solution :

create a url for image the put it in src of img tag :

   const ImageUrl =  URL.createObjectURL(file);

or

<img src={URL.createObjectURL(file)}  alt={file.name} />
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