Some words about my situation: I'm building the form using ReactJS and if it has <input type="submit">
element it works fine: forms submits by pressing enter in input[type="text"]
and by pressing submit element (And there are also working checkings by ReactJS when form is not submitted if nothing has changed).
But if I replace input[type="submit"]
with <button>ButtonLabel</button>
i try to use 2 ways:
Get form DOMNode element and call .submit() method which is not ok because it doesn't use internal ReactJS logic
Pass params to button like
<button type="submit" form="form-id">
but it still doesn't use ReactJS checkings (i don't want to submit the form if nothing has changed)
So I would really appreciate if someone will suggest me how to submit the form in ReactJS correctly using BUTTON element.
Thanks!
If you're trying to make a form with just that one button, you can do that by just skipping that input element. Of course, there will be no data in that post request, but you can add the action attribute to the containing form and set it equal to a URL where you would like to redirect the user on button click.
You can pass the onSubmit click handler as a props as follows: import React, { Component } from "react"; import ReactDOM from "react-dom"; class CustomForm extends Component { render() { return ( <form onSubmit={this. props.
To get input values on form submit in React:Store the values of the input fields in state variables. Set the onSubmit prop on the form element. Access the values of the input fields in your handleSubmit function.
The button element should work exactly as you expect providing the type is set to a submit button and the form has an onsubmit
handler.
<form ref="form" onSubmit={this.handleSubmit}>
<button type="submit">Do the thing</button>
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With