Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. import React, { useReducer } from 'react';
  2.  
  3. const reducer = (state, action) => {
  4. switch(action.type) {
  5. case 'add': {
  6. return [
  7. ...state,
  8. {
  9. id: Date.now(),
  10. text: '',
  11. completed: false
  12. }
  13. ]
  14. }
  15. default: {
  16. return state;
  17. }
  18. }
  19. }
  20.  
  21. function Todo() {
  22.  
  23. const [state, dispatch] = useReducer(reducer, []);
  24.  
  25. return (
  26. <>
  27. <h1>Todo App</h1>
  28. <button onClick={() => dispatch({ type: 'add' })}>Create</button>
  29. { state.map(item => (
  30. <div key={item.id}>{item.id}</div>
  31. ))}
  32. </>
  33. );
  34. }
  35.  
  36. export default Todo;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement