File tree Expand file tree Collapse file tree 1 file changed +24
-5
lines changed
Expand file tree Collapse file tree 1 file changed +24
-5
lines changed Original file line number Diff line number Diff line change 11import React , { useState } from 'react' ;
2+ import uuid from 'uuid/v4' ;
23
34const initalTodos = [
45 {
5- id : 'a' ,
6+ id : uuid ( ) ,
67 task : 'Learn React' ,
78 complete : true ,
89 } ,
910 {
10- id : 'b' ,
11+ id : uuid ( ) ,
1112 task : 'Learn Firebase' ,
1213 complete : true ,
1314 } ,
1415 {
15- id : 'c' ,
16+ id : uuid ( ) ,
1617 task : 'Learn GraphQL' ,
1718 complete : false ,
1819 } ,
1920] ;
2021
2122const App = ( ) => {
23+ const [ todos , setTodos ] = useState ( initalTodos ) ;
2224 const [ task , setTask ] = useState ( '' ) ;
2325
2426 const handleChangeInput = event => {
2527 setTask ( event . target . value ) ;
2628 } ;
2729
30+ const handleSubmit = event => {
31+ if ( task ) {
32+ setTodos ( todos . concat ( { id : uuid ( ) , task, complete : false } ) ) ;
33+ }
34+
35+ setTask ( '' ) ;
36+
37+ event . preventDefault ( ) ;
38+ } ;
39+
2840 return (
2941 < div >
3042 < ul >
31- { initalTodos . map ( todo => (
43+ { todos . map ( todo => (
3244 < li key = { todo . id } >
3345 < label > { todo . task } </ label >
3446 </ li >
3547 ) ) }
3648 </ ul >
3749
38- < input type = "text" value = { task } onChange = { handleChangeInput } />
50+ < form onSubmit = { handleSubmit } >
51+ < input
52+ type = "text"
53+ value = { task }
54+ onChange = { handleChangeInput }
55+ />
56+ < button type = "submit" > Add Todo</ button >
57+ </ form >
3958 </ div >
4059 ) ;
4160} ;
You can’t perform that action at this time.
0 commit comments