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

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

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 Relatório

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