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

382
Views
Why is onSubmit function not called during render even if we don't use an inline function to call it but onCLick functions are called during render?

I am trying to create a simple form using React using the below code:

<div className="wrapper">
  <h1>How About Them Apples</h1>
  <form onSubmit={handleSubmit}>
    <fieldset>
      <label>
        <p>Name</p>
        <input name="name" />
      </label>
    </fieldset>
    <button type="submit">Submit</button>
  </form>
</div>

Now, what I want know is we are not using any inlien function to call the on-submit handler, but it is not being called during render like it would be called if I had done the same for an on-click-handler like:

<button onClick={onClickHandler}>Button</button>

Am I missing something ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Does this make what you want?

function onClickhandler(event) {
  event.preventDefault()
  console.log('Submit!')
}
<div className="wrapper">
  <h1>How About Them Apples</h1>
  <form>
    <fieldset>
      <label>
        <p>Name</p>
        <input name="name" />
      </label>
    </fieldset>
    <br />
    <button onClick={onClickhandler(event)}> Submit</button>
  </form>
</div>

Or you can do the same with type=submit button

function handleSubmit(event) {
  event.preventDefault()
  console.log('Submit!')
}
<div className="wrapper">
  <h1>How About Them Apples</h1>
  <form onSubmit={handleSubmit(event)}>
    <fieldset>
      <label>
            <p>Name</p>
            <input name="name" />
          </label>
    </fieldset>
    <br />
    <button type="submit"> Submit</button>
  </form>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

The onSubmit is not working because you didnot create a function. A function in a simple definition is what you define to do something for you. if you create a function like this:

const onClickHandler =()=>{
console.log('You clicked me')
}

And then you call it in your button like this Button, it will log 'You clicked me to the console' whenever you clicked on that button. This is because you create a function called onClickHandler which will do something for you and i.e log "You clicked me" to the console.

But if you call it like this Button without creating a function that will do something. Then the button will do nothing, it will not work.

To do an inline function on the button so that it will do something for you, you have to do it like this: <button onClick={()=> console.log('You clicked me')}>Button

And it will work because we define the function directly inside the button instead of defining the function before creating a button.

All this also apply with onSubmit. If you didnot create a function that will do something for you or if you didnot use the function directly by adding ()=> then your onSubmit form will not submit all your input. Forthe onSubmit to work, you should do something like this:

<form onSubmit={()=> console.log('You submitted your form')}>
  //All input goes here
</form>

Note: If what you want to do inside the function is more than, then dont forget to do both inline function and seperate function like this: for inline

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

()=>{
console.log('abc....')
console.log('abc....')
}  

for seperate function

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

const onSubmit()=>{
console.log('abc...')
console.log('abc...')
}

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!