Created
March 4, 2017 04:20
-
-
Save zwhitchcox/210c627acbe6429cae611a91663af5eb to your computer and use it in GitHub Desktop.
Gundb React Todo App Example
This file contains hidden or 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 { render } from 'react-dom' | |
| import Gun from 'gun' | |
| const gun = Gun().get('todos') | |
| const formatTodos = todos => Object.keys(todos) | |
| .map(key => ({ key, val: todos[key] })) | |
| .filter(t => Boolean(t.val) && t.key !== '_') | |
| class Todos extends Component { | |
| constructor() { | |
| super() | |
| this.state = {newTodo: '', todos: []} | |
| } | |
| componentWillMount() { | |
| gun.on(todos => this.setState({ | |
| todos: formatTodos(todos) | |
| })) | |
| } | |
| add = _ => { | |
| gun.path(Gun.text.random()).put(this.state.newTodo) | |
| this.setState({newTodo: ''}) | |
| } | |
| del = key => gun.path(key).put(null) | |
| handleChange = e => this.setState({ newTodo: e.target.value}) | |
| render() { | |
| return <div> | |
| <input value={this.state.newTodo} onChange={this.handleChange} /> | |
| <button onClick={this.add}>Add</button> | |
| <br /> | |
| <ul> | |
| {this.state.todos.map(todo => <li key={todo.key} onClick={_=>this.del(todo.key)}>{todo.val}</li>)} | |
| </ul> | |
| </div> | |
| } | |
| } | |
| render(<Todos />, document.getElementById('app')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment