Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form in ReactJS using BUTTON element

Tags:

reactjs

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:

  1. Get form DOMNode element and call .submit() method which is not ok because it doesn't use internal ReactJS logic

  2. 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!

like image 715
eh0t Avatar asked May 29 '14 08:05

eh0t


People also ask

Can I submit form with button?

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.

How do you submit form from a button outside that component in React?

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.

How do I get form data on submit in Reactjs?

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.


1 Answers

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>
like image 94
i_like_robots Avatar answered Sep 23 '22 13:09

i_like_robots