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

151
Visualizações
Trouble clearing react.useState with clear button

I am finding it difficult to clear this file input that I am setting to setImportFile useState. This is the useState I declared

const [importFile, setImportFile] = React.useState<File | null>();
const [fileError, setFileError] = React.useState<string>("");

I clear two useState variables in this const method but only one gets cleared which is setFileError.

  const clearModal = () => {
        setImportFile(null);
        setFileError("");
    };

I use setImportFile in this method

 const onChangeImportFile = (e: React.ChangeEvent<HTMLInputElement>) => {
        e.preventDefault();
        setImportFile(e.currentTarget.files ? e.currentTarget.files[0] : null);
        setSaving(true);
    };

This is my render in which I use the clear button and only the error would clear out but the file input will not clear out. I want to empty everything in the modal

 <Form.Control
     type="file"
     accept='.csv, .xls, .xlsx'
     required
     className="primary mb-3"
     name=""
     onChange={onChangeImportFile}
/>
  <Button variant="secondary" onClick={clearModal} > Clear </Button>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think you need to pass the state value to the Form.Control, to make it controlled (otherwise it's not getting its value from the state).

<Form.Control
  type="file"
  accept='.csv, .xls, .xlsx'
  required
  className="primary mb-3"
  name=""
  value={importFile}
  onChange={onChangeImportFile}
/>
about 4 years ago · Juan Pablo Isaza Relatório

0

You don't need to set File | null as SetStateAction will be set to undefined.

Try this:

const [importFile, setImportFile] = React.useState<File>();
setImportFile(undefined);
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