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

256
Visualizações
I have this child component where you can submit a form, however, e.preventDefault() does not work

From the parent component. There are other forms, hence, there is a conditional form where it will used the child component's form.

A user may click the button to show the form from the child component.

  const [isTrue, setIsTrue] = useState(false);
   <ButtonForm onClick={() => setIsTrue(true)}>
        Click for the Form
    </ButtonForm>

 {isTrue == true ? (
    <Form
        data={entryData}
        items={items}
        names={names}
            />
  ) : (
      ></>
    )}

This is the child component. The problem here is that, everytime I'll submit it, it will reload the page:

const Form = ({ entryData, items, names }) => {
  const handleSubmit = (e) => {
    e.preventDefault();
    try {
      console.log(" saved");
    } catch (err) {
      console.log(err);
    }
  };

  return (
    <>
    
      <form onSubmit={handleSubmit}>
        //some codes here
        <Grid item>
          <TextField
            type="text"
            label="Item Number"
            variant="outlined"
            fullWidth
            required
          />
        </Grid>
       //some codes here
        <Grid item>
          <Button type="submit" fullWidth>
            Submit
          </Button>
        </Grid>
      </form>
      <br /> <br />
    </>
  );
};

export default Form;

This is the sample parent component: most where just forms

<div className="App">
       <Card className={classes.root}>
      <CardContent>
            {users &&
              users.map((user) => (
                <li style={{ listStyle: "none" }}>
               
            <Button onClick={() => setIsTrue(true)}>
                    Click for the Form
            </Button>

                  {//if user condition is true here == true ? (
                 <div>
                     //form here with the textfields
                    </div>
                  ) : (
                   
                    <div>
                     //form here with the textfields
                    </div>
                  )}
                </li>
              ))}
          </>

          {isTrue == true ? (
    <Form
        data={entryData}
        items={items}
        names={names}
            />
  ) : (
      ></>
    )}
      </CardContent>
    </Card>
    </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

// on click trigger submit in first and second forms
<form>
 <form>
  <button type="submit" />
 </form>
 <button type="submit" />
</form>;

//solution
<form onSubmit={(e) => e.preventDefault()}>
    <form onSubmit={(e) => e.preventDefault()}>
      <button type="submit" /> // on click trigger submit in first and second form
    </form>
   <button type="submit" />
</form>;
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