Last active
September 7, 2017 00:09
-
-
Save keithnorm/004a26e3342e50abe846fab67327a1fb 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 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