Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { useReducer } from 'react';
- const reducer = (state, action) => {
- switch(action.type) {
- case 'add': {
- return [
- ...state,
- {
- id: Date.now(),
- text: '',
- completed: false
- }
- ]
- }
- default: {
- return state;
- }
- }
- }
- function Todo() {
- const [state, dispatch] = useReducer(reducer, []);
- return (
- <>
- <h1>Todo App</h1>
- <button onClick={() => dispatch({ type: 'add' })}>Create</button>
- { state.map(item => (
- <div key={item.id}>{item.id}</div>
- ))}
- </>
- );
- }
- export default Todo;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement