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

134
Visualizações
REACT - auto submit form after files uploaded to state the contains array

I want to submit a form that send a request to server automatically.

This is how I load the files into the state -

  const onUpload = (event: ChangeEvent<HTMLInputElement>) => {
    event.preventDefault();

    const id = event.target.id;
    const fileReader = new FileReader();
    const file = event.target.files![0];

    fileReader.onload = () => {
      setCSVFilesState([...CSVFilesState, { id, file: fileReader.result }]);
    };

    fileReader.readAsDataURL(file);
  };

I'm uploading 2 files to the CSVFilesState both in the form.

I want to fire the request when both files are in the state. How can I wait for the files and only then send the request?

This is my onSubmit function -

  const onSubmit = (event: React.FormEvent) => {
    event.preventDefault();

    backendAPIAxios
      .post("/", CSVFilesState)
      .then((response: AxiosResponse<IServerResponseData>) => {

      })
      .catch((e: AxiosError) => {

      });
  };

Tried to do that (didn't work) -

     const onUpload = (event: ChangeEvent<HTMLInputElement>) => {
        event.preventDefault();
    
        const id = event.target.id;
        const fileReader = new FileReader();
        const file = event.target.files![0];
    
        fileReader.onload = () => {
          setCSVFilesState([...CSVFilesState, { id, file: fileReader.result }]);
        };
    
        fileReader.readAsDataURL(file);

   if (CSVFilesState.length === 1) onSubmit(event);
      };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

useEffect(()=>{
    if(CSVFilesState.length === 2){
        onSubmit()
    }
},[CSVFilesState])

Checked the count of CSVFilesState.

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