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

108
Vistas
How to clear inputs and close modal after asynchronous submit function call in React Typescript

I'm having maybe what seems to be a basic trouble, but I just couldn't find solution to this problem. I'm passing handleSubmit function, which is asynchronous, but it doesn't return any value, from a parent to a child form component. After It's called in a child component I want to clear inputs as well as close the modal. In my case, input values are cleared and modal is closed before handleSubmit has finished executing. I'm using React with TypeScript in this project. If anyone could help I would be grateful. Thanks in advance

Parent component

  const createVariation = (variationType: string, variationValues: string[]): void => {
setLoading(true);
setTimeout(() => {
  storeProductVariant(product.id, variationType, variationValues, accessToken)
    .then(() => {
      showProductVariants(product.id, accessToken)
        .then((response) => {
          dispatch(actionsProducts.showProductVariants(response.data.data));
        });

      showProductSkus(product.id, accessToken)
        .then((response) => {
          dispatch(actionsProducts.showProductSkus(response.data.data));
        });
      setLoading(false);
    })
    .catch((error) => {
      setError(error.response.data.errors.variant[0]);
      setLoading(false);

      setTimeout(() => {
        setError('');
      }, 3000);
    });
}, 1000);

};

Child Component

  const onSubmit = (event: React.FormEvent<HTMLFormElement>): void => {
event.preventDefault();
if (validateInputs(
  inputValues.variationType,
  inputValues.variationValues,
)) {
  handleSubmit(inputValues.variationType, inputValues.variationValues, variantId);

Problem is here since I can't call .then() because function is void

  setInputValues({
    variationType: '',
    variationValues: [],
    variation: '',
  });
  handleClose();
  setSelectedVariations([]);
}

};

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What you need could be a little complex, however you may achieve this by the following steps:

  1. Keep a state variable in parent component e.g. submissionDone like loading. And pass this to your child component.

  2. Update submissionDone to true when the promise is resolved in parent.

  3. The changed/updated value of submissionDone will be reflected in the child component.

  4. Clear your inputs or anything else in the child component based on this variable.

Since you have not provided full code, I am giving a hint that - this logic in child component can be written inside a useEffect if you are using functional component or componentDidUpdate if you are using class components.

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