Skip to content

Instantly share code, notes, and snippets.

@pinkmomo027
Created January 30, 2018 23:20
Show Gist options
  • Save pinkmomo027/fd57c9722c2e2c2b9dc63d7a88c4170c to your computer and use it in GitHub Desktop.
Save pinkmomo027/fd57c9722c2e2c2b9dc63d7a88c4170c to your computer and use it in GitHub Desktop.
const List = props => (
<ul>
{
props.items.map( (item, index) => <li key={index}>{item}</li> )
}
</ul>
)
class TodoList extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
term: ''
}
this.handleChange = this.handleChange.bind(this);
}
handleChange = (e) => {
this.setState({
term: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
this.setState({
term: '',
items: [...this.state.items, this.state.term]
});
}
render() {
const buttonStyle = {
height: 19,
marginLeft: 5,
borderRadius: 10
}
return (
<div>
<h4>Todo List</h4>
<form onSubmit={this.handleSubmit}>
<input
type='text'
value={this.state.term}
onChange={this.handleChange}
placeholder="Enter Todo" />
<button type='submit' style={buttonStyle}>Add</button>
</form>
<List items={this.state.items} />
</div>
);
}
}
ReactDOM.render(<TodoList />, document.querySelector('#root'));
// HTML
// <div id='root'></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment