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?

8
  • And it's a bound function? Commented Sep 17, 2019 at 7:24
  • @rrd You don't need to bind as it's a fat arrow function. Also, logging to console doesn't need binding. Commented Sep 17, 2019 at 7:24
  • Have you got the onSubmit inside render() or outside render()? Can you show the full function, please? Commented Sep 17, 2019 at 7:25
  • Move the onSubmit={this.onSubmit} to the button's onClick handler, does it invoke then? Commented Sep 17, 2019 at 7:26
  • 1
    The way you want use is also hacky bro. Officially, submit button should be located between form tags Commented Sep 17, 2019 at 8:25

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

My purpose is how to submit a form bu outside button, is it possible?
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.