Created
June 3, 2018 04:23
-
-
Save qswitcher/09523966560cb7836a358bd7f92b129d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
todos: [ | |
{ text: 'Walk the Dog', priority: 'High' }, | |
{ text: 'Ride bike', priority: 'Low' }, | |
{ text: 'Get a haircut', priority: 'Med' } | |
] | |
}; | |
} | |
addTodo = todo => { | |
const todos = this.state.todos; | |
// simulate an AJAX request | |
setTimeout(() => { | |
this.setState({ todos: todos.concat(todo) }); | |
}, 0); | |
}; | |
deleteTodo = value => { | |
const todos = this.state.todos; | |
const index = todos.indexOf(value); | |
if (index > -1) { | |
this.setState({ todos: todos.filter((item, i) => i !== index) }); | |
} | |
}; | |
render() { | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<h1 className="App-title">React Todos</h1> | |
</header> | |
<div className="App-body"> | |
<TodoForm addTodo={this.addTodo} /> | |
<TodoList todos={this.state.todos} deleteTodo={this.deleteTodo} /> | |
</div> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment