Skip to content

Instantly share code, notes, and snippets.

@rricard
Last active December 12, 2016 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rricard/cd111be1f5796a50c15cd08b2754e965 to your computer and use it in GitHub Desktop.
Save rricard/cd111be1f5796a50c15cd08b2754e965 to your computer and use it in GitHub Desktop.
function Todo({todo, onToggle}) {
return (
<li>
<input
type="checkbox"
onClick={onToggle}
checked={todo.done}
/>
{todo.done ?
<span style={{textDecoration: 'line-through'}}>{todo.title}</span> :
todo.title}
</li>
);
}
class TodoApplication extends React.Component {
constructor(props) {
super(props);
this.state = {
todos: [
{title: 'Write a TODO app without React', done: true},
{title: 'Write the rest of the article', done: false},
],
};
}
render() {
return (
<ul>
{this.state.todos.map((todo, key) => (
<Todo
todo={todo}
key={key}
onToggle={() => this.setState({
todos: this.state.todos.map((t, i) => i === key ? {...t, done: !t.done} : t)
})}
/>
))}
</ul>
);
}
}
ReactDOM.render(<TodoApplication />, document.getElementById('app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment