How are forms created in React?

React forms are similar to HTML forms. But in React, the state is contained in the state property of the component and is only updated via setState(). Thus the elements can’t directly update their state and their submission is handled by a JavaScript function. This function has full access to the data that is entered by the user into a form.

handleSubmit(event) {
    alert('A name was submitted:
' + this.state.value);
    event.preventDefault();
}

render() {
    return (        
 <form onSubmit={this.handleSubmit}>
     <label>
       Name:
<input type="text"
 value={this.state.value}
onChange={this.handleSubmit} />
  </label>
<input type="submit" value="Submit"/>
        </form>
    );
}

Comments