Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button input type submit not submitting in React with React Router

I added the React.js context cause of the NavLink reference (from React Router) in the button I am trying to use. Either way, when I want to submit data with:

submitData(event) {
    event.preventDefault();
    alert("Submitted");
}

This will work:

<FormGroup>
  <input type="submit" />
</FormGroup>

But this will not:

<FormGroup>
    <Button type="submit" id="submitButton" color="primary"><NavLink to="/Confirm">Submit</NavLink></Button>
</FormGroup>

Not sure why the second won't work, but it is my preferred method. Any ideas or thoughts as to why? I know <button> is newer, but adding type="input" should force it ton work all the same.

like image 822
Mark Avatar asked Apr 17 '26 07:04

Mark


1 Answers

The NavLink component is already listening to it's own submit/click event, which isn't propagating to your own form's onSubmit.

The easy solution would simply to bind the NavLink's onClick to your own handler:

<FormGroup>
    <Button type="submit" id="submitButton" color="primary">
        <NavLink to="/Confirm" onClick={this.submitData.bind(this)}>Submit</NavLink>
    </Button>
</FormGroup>
like image 178
Charles Crete Avatar answered Apr 19 '26 20:04

Charles Crete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!