Skip to content

Instantly share code, notes, and snippets.

@gregoryanderson
Last active February 7, 2020 21:57
Show Gist options
  • Save gregoryanderson/0e1a84dc5418276341ef0827cd9d44ac to your computer and use it in GitHub Desktop.
Save gregoryanderson/0e1a84dc5418276341ef0827cd9d44ac to your computer and use it in GitHub Desktop.
Form state
import React, { Component } from "react";
import "./Form.css";
class Form extends Component {
constructor(props) {
super(props);
this.state = {
title: "",
description: ""
};
}
handleClick = e => {
e.preventDefault();
let newTitle = this.state.title;
let newDescription = this.state.description;
let newIdea = { title: newTitle, description: newDescription };
this.props.addIdea(newIdea);
};
handleChange = e => {
e.preventDefault();
this.setState({ [e.target.name]: e.target.value });
};
render() {
return (
<form className="form">
<section>
<label for="title">Title</label>
<input
type="text"
name="title"
value={this.state.title}
onChange={this.handleChange}
/>
</section>
<section>
<label for="description">Description</label>
<input
type="text"
name="description"
value={this.state.description}
onChange={this.handleChange}
/>
</section>
<button onClick={e => this.handleClick(e)}>Submit</button>
</form>
);
}
}
export default Form;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment