3
 class Index extends React.Component {
    state = {isLoading: false}

    onSubmit = (event) => {
       console.log('here we go');
       event.preventDefault();
       // this.checkEmailExistance();
    };

    render() {
      return (
       <>
         <form id="myForm" onSubmit={this.onSubmit} noValidate>
            <CredentialsInputs />
         </form>
         <footer>
            <Button type="submit" form="myForm" isPrimary isDisabled={!isValid}>   
               Continue
            </Button>
          </footer>
        </>
     )}
 }

onSubmit function is not invoked

Note: the props (type and form) was passed well and check using the console elements

Is the problem something related to react?

sanduniYW
  • 723
  • 11
  • 19
Khaled Ramadan
  • 812
  • 1
  • 10
  • 26

3 Answers3

0

You can set a ref to the form, and then in the "Button onClick", you do ref.submit().

In that situation, you set an id to the form... So, if you want to make a really really ugly implementation, you could do something like document.getElementByid('myForm').submit()

If you want to do something better, you should do something like...

<form ref={ref => this.formRef = ref} ...
<button onClick={() => this.formRef.submit()}

Still... not super beautiful. I would recommend Hooks + useRef.

Broda Noel
  • 1,760
  • 1
  • 19
  • 38
-1

You have written you submit button outside of form. You should move your submit button inside of form,

<form id="myForm" onSubmit={this.onSubmit} noValidate>
     <CredentialsInputs />
     <footer>
         <Button type="submit" form="myForm" isPrimary isDisabled={!isValid}>
           Continue
         </Button>
     </footer>    
</form>    

If you don't want to move footer inside of form, then you should have onClick on your Button

<form id="myForm" noValidate>
     <CredentialsInputs />
</form> 

<footer>
    <Button type="submit" form="myForm" isPrimary isDisabled={!isValid} onClick={this.onSubmit}>
        Continue
    </Button>
</footer>        
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
-1

Put the button inside form. it will work.

You should always include a button element inside form to trigger onSubmit method automatic, Else you can call the method manually with onClick event of the button.