Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
After removing the image cannot input same image again

I created an image upload, and remove the uploaded image. Once upload the image we can remove it again and input another image. But I can't input the same image agin.

const [image, setImage] = useState<any>("noImage");
const [isImageUploaded, setIsImageUploaded] = useState<boolean>(false);

const handleImageChange = (event: any) => {
  setImage(URL.createObjectURL(event.target.files[0]));
  setIsImageUploaded(true);
};

const handleOnImageRemoveClick = () => {
  setIsImageUploaded(false);
  setImage("noImage");
};
<span>
  <input type="file" className="d-none" onChange={handleImageChange} disabled={isImageUploaded} />
  {isImageUploaded ? (
    <div>
      <div className="d-flex justify-content-center">
        <Button variant="warning" onClick={handleOnImageRemoveClick}>
          Remove Image
        </Button>
      </div>
    </div>
  ) : (
    <div>
      <p>Click to upload the image</p>
    </div>
  )}
</span>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are not emptying correctly your input. I would recommend you to use a ref with useRef. Also you can shorten your code by only have image state, like so:

const [image, setImage] = useState<any>("noImage");
const inputRef = useRef<any>(null); // import useRef from react

const handleImageChange = (event: any) => {
  setImage(URL.createObjectURL(event.target.files[0]));
};

const handleOnImageRemoveClick = () => {
  setImage("noImage");
  inputRef.current.value="";
};
<span>
  <input ref={inputRef} type="file" className="d-none" onChange={handleImageChange} disabled={image !== "noImage"} />
  {image !== "noImage" ? (
    <div>
      <div className="d-flex justify-content-center">
        <Button variant="warning" onClick={handleOnImageRemoveClick}>
          Remove Image
        </Button>
      </div>
    </div>
  ) : (
    <div>
      <p>Click to upload the image</p>
    </div>
  )}
</span>
about 4 years ago · Juan Pablo Isaza Relatório

0

I think you should reset currentTarget.value like this.

<input
  ...
  onClick={(event)=> { 
    event.currentTarget.value = null
  }}
/>

// TS
onClick={(event) => {
  (event.target as HTMLInputElement).value = '';
}}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda