I want to submit a React form after a click on a link.
To do so I need to submit the form programmatically if the link is clicked.
my problem is : onSubmit
handler is not being fired after the form submit .
Here is a code snipped that I made for this purpose:
var MyForm = React.createClass({
handleSubmit: function(e){
console.log('Form submited');
e.preventDefault();
},
submitForm : function(e){
this.refs.formToSubmit.submit();
},
render: function() {
return (
<form ref="formToSubmit" onSubmit={this.handleSubmit}>
<input name='myInput'/>
<a onClick={this.submitForm}>Validate</a>
</form>);
}
});
ReactDOM.render(
<MyForm name="World" />,
document.getElementById('container')
);
The handleSubmit
is not invoked and the default behavior is executed (the form being submitted).
Is this a ReactJs bug or a normal behavior?
Is there a way to get the onSubmit handler invoked ?
You shouldn't expect a form ref to attach an additional callback to
submit
for you by default. This would be bad design of the framework when working with programmatically set things such as DOM elementrefs
.You should use the
ref
to instantiate all of the events you need in any order you want instead of relying on an untold internal implementation of aref
[when instantiated by aform
element].This works:
Actual
submit
should always occur last.You would prefer to use
dispatchEvent(new Event('submit'))
rather thansubmit()
. Just replace correspondingly.Your current
onSubmit
is bound to the triggering of a React SyntheticEvent and not the native form submit event. That explains whythis.refs.formToSubmit.submit();
is not triggering your handler. As far as I know, trying to manually trigger that SyntheticEvent is not a recommended or worthwhile pursuit.I believe the idiomatic way to accomplish this is to structure the JSX/HTML to use a
<button>
or<input>
of type submit. Either of those will trigger yourhandleSubmit
handler. In my opinion, it will reduce complexity, consolidate your handlers, and seems to be your best solution.e.g.
<input type="submit" value="Validate" />
<button type="submit">Validate</button>
I had this same issue but couldn't fix it. No matter what I did, doing
document.querySelector('form').submit()
would not trigger theonSubmit
handler. I tried including an invisible<input type="submit">
, but still.submit()
would not trigger it. So I just changed to keeping the invisible submit (style="display:none"
) and then doing.click()
on that invisible submit button, surprisingly it works even withdisplay:none
. I tested only in Firefox though.You should be using input tag with type submit instead of anchor tag, Since anchor tag does not trigger an submit event.
I have success with this. This triggers the handleSubmit upon clicking. Hope this helps.
Found from here: https://github.com/facebook/react/issues/6796