Created
September 7, 2017 00:12
-
-
Save keithnorm/3f7ea02970bbc7d8c26d21ff4a1415d7 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 TodoApp extends React.Component { | |
constructor({db}) { | |
super(); | |
this.db = db; | |
this.state = { | |
todos: [], | |
text: '', | |
} | |
} | |
async componentWillMount() { | |
const todos = await this.db.collection('todos').find({}).sort({_id: -1}).toArray(); | |
this.setState({ | |
todos | |
}); | |
} | |
onSubmit = async (e) => { | |
e.preventDefault(); | |
await this.db.collection('todos').insert({text: this.state.text}); | |
this.setState({ | |
todos: await this.db.collection('todos').find({}).sort({_id: -1}).toArray(), | |
text: '' | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<form onSubmit={this.onSubmit}> | |
<input value={this.state.text} onChange={(e) => this.setState({text: e.target.value})} type='text'/> | |
<button type='submit'>Add Todo</button> | |
</form> | |
<ul> | |
{this.state.todos.map((todo) => { | |
return <li key={todo._id}>{todo.text}</li> | |
})} | |
</ul> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment