Skip to content

Instantly share code, notes, and snippets.

@xavierartot
Created January 17, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xavierartot/7b3ed01b075cb1c9953b56708a62a4b8 to your computer and use it in GitHub Desktop.
Save xavierartot/7b3ed01b075cb1c9953b56708a62a4b8 to your computer and use it in GitHub Desktop.
//Improvment from this tutorial: https://medium.com/@aghh1504/1-simple-react-todo-list-52186b62976b
import React from 'react';
import List from './List';
export default class TodoList extends React.Component {
constructor(props) {
super(props);
this.onChangeList = this.onChangeList.bind(this);
this.state = {
term : '',
listItem: [],
message: 'To Do List'
}
}
onChangeList(event) {
this.setState({
term: event.target.value
});
}
onSubmit = (event) => {
event.preventDefault()
if (this.state.term) {
this.setState({
listItem: [...this.state.listItem, this.state.term],
term: '',
message: 'To Do List'
});
} else {
let warning = 'you need to fill the field';
this.setState({
message: warning
});
}
}
render() {
console.log(...this.state.listItem);
return (
<div className='TodoList'>
<form className="form" onSubmit={this.onSubmit}>
<h1>{this.state.message}</h1>
<input type="text" onChange={this.onChangeList} value={this.state.term} />
<button>add</button>
</form>
<List listItem={this.state.listItem} />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment