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

74
Views
React and uploady with custom function that sends a form after showFileUpload

I have a form that have an onSubmit, in that callback I have a uploady.showFileUpload(), but the code after uploady.showFileUpload() is executed.

Now the question is how can I wait for uploady, and then execute the rest of the code?

  const handleSubmit2 = useCallback((e)=> {
  uploady.showFileUpload(); //(HERE SHOULD WAIT FOR IT TO FINISH FILE SELECT)
  //OTHER CODE
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This codesandbox should be helpful in the case of using Uploady with a form: https://codesandbox.io/s/react-uploady-inside-form-ys1wx

The idea is that you show the file selection prompt separately from submitting the form:

import React, { useState, useCallback, useMemo, forwardRef } from "react";
import Uploady, {
  useUploadyContext
} from "@rpldy/uploady";
import { asUploadButton } from "@rpldy/upload-button";


const MyUploadField = asUploadButton(
  forwardRef(({ onChange, ...props }, ref) => {
    
    return (
      <div {...props} ref={ref} id="form-upload-button" title={text}>
      Select file
      </div>
    );
  })
);

const MyForm = () => {
  const [fields, setFields] = useState({});
  const [fileName, setFileName] = useState(null);
  const uploadyContext = useUploadyContext();

  const onSubmit = useCallback(() => {
    uploadyContext.processPending({ params: fields });
  }, [fields, uploadyContext]);

  const onFieldChange = useCallback(
    (e) => {
      setFields({
        ...fields,
        [e.currentTarget.id]: e.currentTarget.value
      });
    },
    [fields, setFields]
  );

  return (
    <form>
      <MyUploadField autoUpload={false} />     
      <input
        onChange={onFieldChange}
        id="field-name"
        type="text"
        placeholder="your name"
      />                
      <SubmitButton
        id="form-submit"
        type="button"
        onClick={onSubmit}       
      >
        Submit Form
      </SubmitButton>
    </form>
  );
};

<Uploady
        clearPendingOnAdd
        destination={{ url: "[upload-url]" }}
        multiple={false}
      >       
        <MyForm />
      </Uploady>

Selection is achieved by using the asUploadButton HOC. You can of course do so yourself as you did with uploady.showFileUpload();.

Then, the submit button uses uploady's processPending method to start uploading.

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!