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

66
Visualizações
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 Respostas
Responde à pergunta

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 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