Skip to content

Commit 37144e9

Browse files
committed
submit button
1 parent 3e6e5a2 commit 37144e9

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/App.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,60 @@
11
import React, { useState } from 'react';
2+
import uuid from 'uuid/v4';
23

34
const 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

2122
const 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
};

0 commit comments

Comments
 (0)