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

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

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