Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 871 Bytes

2-4-react-forms.md

File metadata and controls

40 lines (32 loc) · 871 Bytes

Ironhack Logo

React | Forms

Basic Example with 1 input

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  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.handleChange} />`
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}