Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import React, { useEffect, useState } from 'react';
  2. import { useActions, useStore } from 'easy-peasy';
  3.  
  4. export default function App() {
  5. return (
  6. <div>
  7. <h1 style={{ margin: 0 }}>Todo</h1>
  8. <Todos />
  9. <AddTodo />
  10. </div>
  11. );
  12. }
  13.  
  14. function AddTodo() {
  15. const [text, setText] = useState('');
  16. const save = useActions(actions => actions.add);
  17. const handleAddClick = () => {
  18. save(text);
  19. setText('');
  20. };
  21. const handleTextChange = e => setText(e.target.value);
  22. return (
  23. <div>
  24. <input
  25. value={text}
  26. onChange={handleTextChange}
  27. placeholder="What to do next"
  28. />
  29. {text && <button onClick={handleAddClick}>Add</button>}
  30. </div>
  31. );
  32. }
  33.  
  34. function Todos() {
  35. const todos = useStore(state => state.todos);
  36. const { toggle, del } = useActions(actions => ({
  37. toggle: actions.toggle,
  38. del: actions.delete,
  39. }));
  40. const todosList = Object.values(todos);
  41. return todosList.map(todo => (
  42. <Todo key={todo.id} todo={todo} toggle={toggle} del={del} />
  43. ));
  44. }
  45.  
  46. function Todo({ todo: { id, text, done }, toggle, del }) {
  47. return (
  48. <div
  49. onClick={() => toggle(id)}
  50. style={{
  51. cursor: 'pointer',
  52. textDecoration: done ? 'line-through' : undefined,
  53. }}
  54. >
  55. {text}
  56. <button
  57. onClick={e => {
  58. e.stopPropagation();
  59. del(id);
  60. }}
  61. style={{ marginLeft: 10 }}
  62. >
  63. Delete
  64. </button>
  65. </div>
  66. );
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement