Skip to content

Commit 9010691

Browse files
committed
Add functionality to add todo dynamically
1 parent 4c53be9 commit 9010691

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

react-todos/src/App.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function App() {
1111
];
1212
const [todos, setTodos] = useState(seededTodos);
1313

14-
const addTodo = () => {
14+
const addTodo = (description, assigned) => {
1515
if (seededTodos.length > 0) {
1616
const newTodo = {
1717
rowNumber: todos.length + 1,
18-
rowDescription: "New todo",
19-
rowAssigned: "User Three",
18+
rowDescription: description,
19+
rowAssigned: assigned,
2020
};
2121
setTodos((todos) => [...todos, newTodo]);
2222
}
@@ -28,7 +28,7 @@ function App() {
2828
<div className="card-header">Your Todo's</div>
2929
<div className="card-body">
3030
<TodoTable todos={todos} />
31-
<NewTodoForm />
31+
<NewTodoForm addTodo={addTodo} />
3232
</div>
3333
</div>
3434
</div>

react-todos/src/components/NewTodoForm.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { useState } from "react";
22

3-
export default function NewTodoForm() {
3+
export default function NewTodoForm(props) {
44
const [description, setDescription] = useState("");
55
const [assigned, setAssigned] = useState("");
66

7+
const submitTodo = () => {
8+
if (description !== "" && assigned !== "") {
9+
props.addTodo(description, assigned);
10+
setDescription("");
11+
setAssigned("");
12+
}
13+
};
14+
715
return (
816
<div className="mt-5">
917
<form>
@@ -27,7 +35,11 @@ export default function NewTodoForm() {
2735
value={description}
2836
/>
2937
</div>
30-
<button type="button" className="btn btn-primary mt-3">
38+
<button
39+
type="button"
40+
className="btn btn-primary mt-3"
41+
onClick={submitTodo}
42+
>
3143
Add ToDo
3244
</button>
3345
</form>

0 commit comments

Comments
 (0)