In my React application, tasks do not update in real time after adding or updating them. I must refresh the browser to see the changes. How can I make the tasks update in real time without needing a refresh? Below is my code snippet involving task operations like add, delete, and fetch.
This is todo.jsx:
const Todo = () => {
let id = sessionStorage.getItem("id");
const [Array, setArray] = useState([]);
const [Inputs, setInputs] = useState({
title: "",
});
const username = localStorage.getItem("username");
const naviagte = useNavigate();
useEffect(() => {
if (id === null) {
naviagte("/login");
}
const fetchTasks = async () => {
try {
const response = await axios.get(
`http://localhost:1000/api/v2/getTasks/${id}`
);
setArray(response.data.list);
} catch (error) {
console.error("Failed to fetch tasks:", error);
}
};
fetchTasks();
}, [id]);
const change = (e) => {
const { name, value } = e.target;
setInputs({ ...Inputs, [name]: value });
};
const handleSubmit = async (e) => {
e.preventDefault();
if (Inputs.title === "") {
toast.error("Enter Task");
} else {
if (id) {
await axios
.post("http://localhost:1000/api/v2/addTask", {
title: Inputs.title,
id: id,
})
.then((response) => {
console.log(response);
});
console.log("task is added : ", Inputs);
setInputs({ title: "" });
toast.success("Task Added");
} else {
setArray([...Array, Inputs]);
setInputs({ title: "" });
toast.success("Task Added");
toast.error("No id found");
}
}
};
const del = async (Cardid) => {
try {
const response = await axios.delete(
`http://localhost:1000/api/v2/deleteTask/${Cardid}`,
{
data: { id: id },
}
);
if (response.status === 200) {
// Filter out the deleted task from the Array state
setArray(Array.filter((item) => item._id !== Cardid));
toast.success("Task Deleted");
}
} catch (error) {
console.error(error);
toast.error("Failed to delete task");
}
};
// const dis = (value) => {
// document.getElementById("todo-update").style.display = value;
// };
// const onTaskUpdate = () => {
// fetchTasks(); // Re-fetch tasks after an update
// };
return (
<div className="todo">
<nav>
<h1>Welcome {username} </h1>
<button className="btnlogout" onClick={() => naviagte("/")}>
Logout
</button>
</nav>
<div className="todo-heading">
<h1>To-Do List</h1>
</div>
<div className="todo-container">
<ToastContainer></ToastContainer>
<div className="todo-form">
<form onSubmit={handleSubmit} style={{ display: "flex" }}>
<input
className="addtodoinput"
type="text"
placeholder="Enter task"
name="title"
onChange={change}
value={Inputs.title}
/>
<button className="add" type="submit">
Add Task
</button>
</form>
</div>
<div className="todo-body">
<div>
{Array &&
Array.map((item, index) => (
<div key={index} className="todo-container">
<TodoCard
title={item.title}
id={item._id}
delid={del}
// onUpdate={onTaskUpdate}
/>
</div>
))}
</div>
</div>
</div>
</div>
);
};
export default Todo;
This is todo card
const TodoCard = ({ title, id, delid }) => {
const [isEditing, setIsEditing] = useState(false);
const [Inputs, setInputs] = useState({
title: "",
});
useEffect(() => {
setInputs({ title });
}, [title]);
const change = (e) => {
const { name, value } = e.target;
setInputs({ ...Inputs, [name]: value });
};
const handleEdit = () => {
setIsEditing(true);
console.log(title);
};
const handleUpdate = async () => {
setIsEditing(false);
await axios
.put(`http://localhost:1000/api/v2/updateTask/${id}`, Inputs)
.then((response) => {
console.log(response);
toast.success("Task Added");
// onUpdate();
});
console.log(Inputs);
console.log("title is : ", title);
};
return (
<div>
<div className="task-cont">
{isEditing ? (
<input
name="title"
type="text"
value={Inputs.title}
onChange={change}
/>
) : (
<div className="tasks">{title}</div>
)}
<div className="task-icons">
{isEditing ? (
<ImCheckmark className="icons" onClick={handleUpdate} />
) : (
<>
<MdDelete className="icons" onClick={() => delid(id)} />
<MdModeEditOutline
className="icons"
onClick={handleEdit}
name="title"
/>
</>
)}
</div>
</div>
</div>
);
};
export default TodoCard;
I tried to add and update task - it works on backend but not seems in window , without refresh .
There is nothing in the code you provided that updates the local
Arraystate array when any specific todo is updated.I suggest the following refactor to:
handleUpdatecallback into the parentTodocomponent so it can update thetodosstate array. Pass as a callback down to theTodoCardcomponent.TodoCardto call the passedonUpdateprop andawaitit to resolve.