File tree Expand file tree Collapse file tree 4 files changed +75
-38
lines changed
Expand file tree Collapse file tree 4 files changed +75
-38
lines changed Original file line number Diff line number Diff line change 11import "./App.css"
2- import Formulario from "./components/Formulario "
2+ import TodoList from "./components/TodoList "
33
44function App ( ) {
55 return (
66 < div >
7- < h1 > Formulario de nombre</ h1 >
8- < Formulario />
7+ < TodoList />
98 </ div >
109 )
1110}
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ import { useState , useEffect } from "react"
2+
3+ const TodoList = ( ) => {
4+ const [ todos , setTodos ] = useState ( [ ] )
5+
6+ const [ newTodo , setNewTodo ] = useState ( "" )
7+
8+ const handleInputChange = ( e ) => {
9+ setNewTodo ( e . target . value )
10+ }
11+
12+ useEffect ( ( ) => {
13+ const savedTodos = JSON . parse ( localStorage . getItem ( "todos" ) ) || [ ]
14+ setTodos ( savedTodos )
15+ } , [ ] )
16+
17+ useEffect ( ( ) => {
18+ document . title = `Mis Tareas (${ todos . length } )`
19+ } , [ todos ] )
20+
21+ useEffect ( ( ) => {
22+ const handleEnter = ( e ) => {
23+ if ( e . key !== "Enter" ) {
24+ return
25+ }
26+ handleAddTodo ( )
27+ }
28+
29+ window . addEventListener ( "keydown" , handleEnter )
30+
31+ return ( ) => window . removeEventListener ( "keydown" , handleEnter )
32+ } , [ todos , newTodo ] )
33+
34+ const handleAddTodo = ( ) => {
35+ const newTodoItem = {
36+ text : newTodo ,
37+ completed : false ,
38+ }
39+
40+ const newTodos = [ ...todos , newTodoItem ]
41+
42+ setTodos ( newTodos )
43+
44+ addToLocalStorage ( newTodos )
45+
46+ setNewTodo ( "" )
47+ }
48+
49+ const addToLocalStorage = ( arrTodos ) => {
50+ localStorage . setItem ( "todos" , JSON . stringify ( arrTodos ) )
51+ }
52+
53+ return (
54+ < div >
55+ < input
56+ type = "text"
57+ placeholder = "Nueva tarea"
58+ value = { newTodo }
59+ onChange = { handleInputChange }
60+ />
61+
62+ < button onClick = { handleAddTodo } > Agregar</ button >
63+
64+ < ul >
65+ { todos . map ( ( todo , index ) => (
66+ < li key = { index } > { todo . text } </ li >
67+ ) ) }
68+ </ ul >
69+ </ div >
70+ )
71+ }
72+
73+ export default TodoList
You can’t perform that action at this time.
0 commit comments