Created
January 31, 2018 07:53
-
-
Save matthijsgroen/1307ac302f455365ace67425ca84c372 to your computer and use it in GitHub Desktop.
Todo app from React Meetup
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
.done { | |
color: red; | |
} |
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 "babel-polyfill"; | |
import { h, render, Component } from "preact"; | |
import styles from "./myStyle.scss"; | |
const root = document.querySelector("#maji-app"); | |
root.innerHTML = ""; | |
const TodoList = ({ items, onTodoClick }) => ( | |
<ul> | |
{items.map((item, i) => ( | |
<li | |
key={i} | |
class={item.done && styles.done} | |
onClick={() => onTodoClick(item)} | |
> | |
{item.done ? <del>{item.name}</del> : item.name} | |
</li> | |
))} | |
</ul> | |
); | |
const todoItems = [ | |
{ name: "Make todo app", done: false }, | |
{ name: "next item", done: false }, | |
{ name: "hallo", done: true } | |
]; | |
class MyTodoList extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
items: [...todoItems], | |
newItem: "" | |
}; | |
} | |
onInput = e => { | |
this.setState({ | |
newItem: e.target.value | |
}); | |
}; | |
onTodoClick = item => { | |
this.setState({ | |
items: this.state.items.map( | |
stateItem => | |
item === stateItem | |
? { ...stateItem, done: !stateItem.done } | |
: stateItem | |
) | |
}); | |
}; | |
onAdd = () => { | |
this.setState({ | |
items: [...this.state.items, { name: this.state.newItem, done: false }], | |
newItem: "" | |
}); | |
}; | |
render(props, { items, newItem }) { | |
return ( | |
<div> | |
<TodoList items={items} onTodoClick={this.onTodoClick} /> | |
<input type="text" onInput={this.onInput} value={newItem} /> | |
<button onClick={this.onAdd} type="button"> | |
Add Todo | |
</button> | |
</div> | |
); | |
} | |
} | |
render( | |
<div class="Foo"> | |
<MyTodoList /> | |
<strong>Hoi</strong> world | |
<MyTodoList /> | |
</div>, | |
root, | |
root.firstChildElement | |
); | |
// Live reloading | |
if (process.env.NODE_ENV !== "production") { | |
require("preact/devtools"); | |
if (module.hot) { | |
module.hot.accept(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment