Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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 answers
Answer question

0

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

Checked the count of CSVFilesState.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!