Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
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 Respuestas
Responde la pregunta

0

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

Checked the count of CSVFilesState.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda