This repository was archived by the owner on Oct 23, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +49
-71
lines changed Expand file tree Collapse file tree 2 files changed +49
-71
lines changed Original file line number Diff line number Diff line change 1
- import React , { Component } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import Form from './components/Form' ;
3
3
import Todo from './components/Todo' ;
4
4
import './App.css' ;
5
5
6
- export default class App extends Component {
7
- constructor ( props ) {
8
- super ( props ) ;
9
- this . state = {
10
- nextId : 1 ,
11
- todos : [ ] ,
12
- }
13
- this . addTodo = this . addTodo . bind ( this ) ;
14
- this . removeTodo = this . removeTodo . bind ( this ) ;
15
- }
6
+ const App = ( ) => {
7
+ const [ nextId , setNextId ] = useState ( 1 ) ;
8
+ const [ todos , setTodos ] = useState ( [ ] ) ;
16
9
17
- addTodo ( text ) {
10
+ const addTodo = text => {
18
11
const nextTodos = [
19
- ...this . state . todos ,
12
+ ...todos ,
20
13
{
21
- id : this . state . nextId ,
14
+ id : nextId ,
22
15
body : text
23
16
} ,
24
17
] ;
25
18
26
- this . setState ( {
27
- nextId : this . state . nextId + 1 ,
28
- todos : nextTodos ,
29
- } ) ;
30
- }
19
+ setTodos ( nextTodos ) ;
20
+ setNextId ( nextId + 1 )
21
+ } ;
31
22
32
- removeTodo ( target ) {
33
- const idx = this . state . todos . findIndex ( ( { id } ) => id === target ) ;
23
+ const removeTodo = target => {
24
+ const idx = todos . findIndex ( ( { id } ) => id === target ) ;
34
25
const nextTodos = [
35
- ...this . state . todos
26
+ ...todos
36
27
] ;
37
28
nextTodos . splice ( idx , 1 ) ;
38
29
39
- this . setState ( {
40
- todos : nextTodos ,
41
- } )
30
+ setTodos ( nextTodos ) ;
42
31
}
43
32
44
- render ( ) {
45
- return (
46
- < div id = "app" >
47
- < Form handleAdd = { this . addTodo } />
48
- {
49
- this . state . todos . map ( todo => {
50
- return (
51
- < Todo
52
- key = { todo . id }
53
- todo = { todo }
54
- handleRemove = { this . removeTodo }
55
- />
56
- ) ;
57
- } )
58
- }
59
- </ div >
60
- ) ;
61
- }
33
+ return (
34
+ < div id = "app" >
35
+ < Form handleAdd = { addTodo } />
36
+ {
37
+ todos . map ( todo => {
38
+ return (
39
+ < Todo
40
+ key = { todo . id }
41
+ todo = { todo }
42
+ handleRemove = { removeTodo }
43
+ />
44
+ ) ;
45
+ } )
46
+ }
47
+ </ div >
48
+ ) ;
62
49
}
50
+
51
+ export default App ;
Original file line number Diff line number Diff line change 1
- import React , { Component } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
- export default class Form extends Component {
5
- constructor ( props ) {
6
- super ( props ) ;
7
- this . state = {
8
- body : "" ,
9
- } ;
10
- this . handleChnage = this . handleChnage . bind ( this ) ;
11
- this . handleSubmit = this . handleSubmit . bind ( this ) ;
12
- }
4
+ const Form = ( { handleAdd } ) => {
5
+ const [ body , setBody ] = useState ( "" ) ;
13
6
14
- handleChnage ( e ) {
15
- this . setState ( {
16
- body : e . target . value ,
17
- } )
7
+ const handleChnage = e => {
8
+ setBody ( e . target . value ) ;
18
9
}
19
10
20
- handleSubmit ( e ) {
11
+ const handleSubmit = e => {
21
12
e . preventDefault ( ) ;
22
- if ( this . state . body . length === 0 ) return ;
23
- this . props . handleAdd ( this . state . body ) ;
24
- this . setState ( {
25
- body : "" ,
26
- } ) ;
13
+ if ( body . length === 0 ) return ;
14
+ handleAdd ( body ) ;
15
+ setBody ( "" ) ;
27
16
}
28
17
29
- render ( ) {
30
- return (
31
- < form action = "/" method = "post" >
32
- < input type = "text" onChange = { this . handleChnage } value = { this . state . body } />
33
- < button type = "submit" onClick = { this . handleSubmit } > +</ button >
34
- </ form >
35
- ) ;
36
- }
18
+ return (
19
+ < form action = "/" method = "post" >
20
+ < input type = "text" onChange = { handleChnage } value = { body } />
21
+ < button type = "submit" onClick = { handleSubmit } > +</ button >
22
+ </ form >
23
+ ) ;
37
24
}
38
25
39
26
Form . propTypes = {
40
27
handleAdd : PropTypes . func ,
41
28
}
29
+
30
+ export default Form ;
You can’t perform that action at this time.
0 commit comments