Skip to content

Instantly share code, notes, and snippets.

@keithnorm
Last active September 7, 2017 00:09
Show Gist options
  • Save keithnorm/004a26e3342e50abe846fab67327a1fb to your computer and use it in GitHub Desktop.
Save keithnorm/004a26e3342e50abe846fab67327a1fb to your computer and use it in GitHub Desktop.
import Zango from 'zangodb';
let db = new Zango.Db('todo_app', { todos: [] });
class TodoApp extends React.Component {
constructor() {
super();
this.state = {
todos: [],
text: '',
}
}
async componentWillMount() {
const todos = await db.collection('todos').find({}).sort({_id: -1}).toArray();
this.setState({
todos
});
}
onSubmit = async (e) => {
e.preventDefault();
await db.collection('todos').insert({text: this.state.text});
this.setState({
todos: await 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