-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskForm.jsx
More file actions
33 lines (29 loc) · 871 Bytes
/
TaskForm.jsx
File metadata and controls
33 lines (29 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { useState } from "react";
function TaskForm({ setTasks }) {
const [title, setTitle] = useState("");
const addTask = async (e) => {
e.preventDefault();
const newTask = { title };
const res = await fetch("http://localhost:5000/tasks", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newTask),
});
const data = await res.json();
setTasks((prev) => [...prev, data]);
setTitle("");
};
return (
<form onSubmit={addTask} className="mb-4">
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="New task"
className="border p-2 w-full"
/>
<button type="submit" className="bg-blue-500 text-white p-2 w-full mt-2">Add Task</button>
</form>
);
}
export default TaskForm;