Created
June 3, 2018 04:17
-
-
Save qswitcher/3e92a3ccba7cd09598bea77aaad70dfa 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
import React, { Component } from 'react'; | |
import TodoList from './TodoList'; | |
import TodoForm from './TodoForm'; | |
import logo from './logo.svg'; | |
import './App.css'; | |
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> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment