Skip to content

Instantly share code, notes, and snippets.

@keithnorm
Created September 7, 2017 00:12
Show Gist options
  • Save keithnorm/3f7ea02970bbc7d8c26d21ff4a1415d7 to your computer and use it in GitHub Desktop.
Save keithnorm/3f7ea02970bbc7d8c26d21ff4a1415d7 to your computer and use it in GitHub Desktop.
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