Sourav Das
Sourav Das

Reputation: 125

React form handeling?

When using forms or input tags in react I use useState for the value of the form.

const [value,setvalue]=useState("");
onInput(e)=>{setvalue(e.target.value)};

But with this each time the user fills the form the component gets rerendered. Is there any better way to do that?

Upvotes: 1

Views: 37

Answers (1)

Deepak Gupta
Deepak Gupta

Reputation: 658

if you are using a form you can use onSubmit

<form onSubmit={this.handleSubmit}>
  <input type="text" name="input1" />
  <button type="submit">Submit</button>
</form>

and in handleSubmit function:

    handleSubmit(event) {
        event.preventDefault();
        var input1 = event.currentTarget.input1.value;
        // Do the rest
      }

Upvotes: 1

Related Questions