Skip to content
This repository was archived by the owner on Oct 23, 2019. It is now read-only.

Commit 50c4f3c

Browse files
committed
refactored components using react hooks
1 parent 48bb003 commit 50c4f3c

File tree

2 files changed

+49
-71
lines changed

2 files changed

+49
-71
lines changed

src/App.js

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,51 @@
1-
import React, { Component } from 'react';
1+
import React, { useState } from 'react';
22
import Form from './components/Form';
33
import Todo from './components/Todo';
44
import './App.css';
55

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([]);
169

17-
addTodo(text) {
10+
const addTodo = text => {
1811
const nextTodos = [
19-
...this.state.todos,
12+
...todos,
2013
{
21-
id: this.state.nextId,
14+
id: nextId,
2215
body: text
2316
},
2417
];
2518

26-
this.setState({
27-
nextId: this.state.nextId + 1,
28-
todos: nextTodos,
29-
});
30-
}
19+
setTodos(nextTodos);
20+
setNextId(nextId + 1)
21+
};
3122

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);
3425
const nextTodos = [
35-
...this.state.todos
26+
...todos
3627
];
3728
nextTodos.splice(idx, 1);
3829

39-
this.setState({
40-
todos: nextTodos,
41-
})
30+
setTodos(nextTodos);
4231
}
4332

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+
);
6249
}
50+
51+
export default App;

src/components/Form.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
import React, { Component } from 'react';
1+
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
33

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("");
136

14-
handleChnage(e) {
15-
this.setState({
16-
body: e.target.value,
17-
})
7+
const handleChnage = e => {
8+
setBody(e.target.value);
189
}
1910

20-
handleSubmit(e) {
11+
const handleSubmit = e => {
2112
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("");
2716
}
2817

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+
);
3724
}
3825

3926
Form.propTypes = {
4027
handleAdd: PropTypes.func,
4128
}
29+
30+
export default Form;

0 commit comments

Comments
 (0)