-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathTaskForm.jsx
More file actions
39 lines (32 loc) · 898 Bytes
/
Copy pathTaskForm.jsx
File metadata and controls
39 lines (32 loc) · 898 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
34
35
36
37
38
39
import React, { useState, useId, useContext, useRef, useEffect } from "react";
import { TaskContext } from "../context/TaskContext";
function TaskForm() {
const [taskName, setTaskName] = useState("");
const userid = useId();
const inputRef = useRef(null)
const { addTask } = useContext(TaskContext);
function handleSubmit(e) {
e.preventDefault();
addTask(taskName)
if (taskName.trim() === "") return;
setTaskName("");
}
useEffect(()=>{
inputRef.current.focus()
},[])
return (
<form onSubmit={handleSubmit}>
<label htmlFor={userid}>New Task:</label>
<input
ref={inputRef}
id={userid}
type="text"
value={taskName}
onChange={(e) => setTaskName(e.target.value)}
placeholder="Add a new task..."
/>
<button type="submit">Add Task</button>
</form>
);
}
export default TaskForm;