0

How to preview selected image in react js? this is my code. I am new to react.js and I still have no idea how to call functions or like this.

const HomepagePage = (props) => {
  const hiddenFileInput = React.useRef(null);
  
  const handleClick = event => {
    hiddenFileInput.current.click();
  };
  const handleChange = event => {
    const fileUploaded = event.target.files[0];
    props.handleFile(fileUploaded);
  };

  return (
    <>
              <Button
                className="common-pointer font-bold lg:ml-[44px] xl:ml-[56px] 2xl:ml-[63px] 3xl:ml-[75px] lg:text-[18px] xl:text-[23px] 2xl:text-[25px] 3xl:text-[31px] text-center tracking-ls1 w-[63%]"
                onClick={handleClick}
                shape="RoundedBorder5"
              >
                Upload Image
              </Button>
              <input type="file"
             ref={hiddenFileInput}
             onChange={handleChange}
             style={{display:'none'}} 
      /> 
              <Img
                src="images/img_11.png"
                className="absolute lg:h-[382px] xl:h-[478px] 2xl:h-[537px] 3xl:h-[645px] w-[100%]"
                alt="Eleven"
              />

The image selected from the button should replace the image 11. Any idea how to do this?

John Michael Tan
  • 129
  • 3
  • 10
  • Does this answer your question? [Preview an image before it is uploaded](https://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded) – junwen-k Nov 17 '22 at 06:20
  • @junwen-k, tried but I dont know how to since the structure is kinda different – John Michael Tan Nov 17 '22 at 06:54

0 Answers0