Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import { render } from "react-dom";
  2. import React, { Component } from "react";
  3.  
  4. let id = 0;
  5.  
  6. const Todo = props => (
  7. <li>
  8. <input
  9. type="checkbox"
  10. checked={props.todo.checked}
  11. onClick={props.onToggle}
  12. />
  13. <button onClick={props.onDelete}>Delete</button>
  14. <span>{props.todo.text}</span>
  15. </li>
  16. );
  17.  
  18. class App extends Component {
  19. constructor() {
  20. super();
  21. this.state = {
  22. todos: []
  23. };
  24. }
  25.  
  26. render() {
  27. return (
  28. <div>
  29. <div>Todo Counter: {this.state.todos.length}</div>
  30. <div>Unchecked Count: {this.state.todos.filter(todo => !todo.checked).length}</div>
  31. <button onClick={() => this.addTodo()}>Add Todo</button>
  32. <ul>
  33. {this.state.todos.map(todo => (
  34. <Todo
  35. todo={todo}
  36. onDelete={() => this.removeTodo(todo.id)}
  37. onToggle={() => this.toggleTodo(todo.id)}
  38. />
  39. ))}
  40. </ul>
  41. </div>
  42. );
  43. }
  44.  
  45. addTodo() {
  46. const text = prompt("TODO text please!");
  47. this.setState({
  48. todos: [...this.state.todos, { id: id++, text: text, checked: false }]
  49. });
  50. }
  51.  
  52. removeTodo(id) {
  53. this.setState({
  54. todos: this.state.todos.filter(todo => todo.id !== id)
  55. });
  56. }
  57.  
  58. toggleTodo(id) {
  59. this.setState({
  60. todos: this.state.todos.map(todo => {
  61. if (todo.id !== id) return todo;
  62. return {
  63. ...todo,
  64. checked: !todo.checked
  65. };
  66. })
  67. });
  68. }
  69. }
  70.  
  71. render(<App />, document.getElementById("root"));
Add Comment
Please, Sign In to add comment