forked from nic-dgl104-winter-2024/pattern-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodo.js
More file actions
44 lines (40 loc) · 1.24 KB
/
Copy pathTodo.js
File metadata and controls
44 lines (40 loc) · 1.24 KB
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
40
41
42
43
44
// Import necessary modules
import React, { useState, useEffect } from "react"; // For React hooks
import TodoList from "./TodoList"; // Import the TodoList component
import api from "./api"; // Import the API service
// TodoPage component
function Todo() {
// Define state to store todos
const [todos, setTodos] = useState([]);
// Effect hook to fetch todos when the component mounts
useEffect(() => {
// Fetch todos from the API
const fetchTodos = async () => {
try {
// Make API request to fetch todos
const response = await api.get("/todos");
// Update state with fetched todos
setTodos(response.data);
} catch (error) {
// Handle error if fetching todos fails
console.error("Error fetching todos:", error);
}
};
// Call fetchTodos function
fetchTodos();
}, []); // Empty dependency array ensures the effect runs only once
// Render the TodoPage
return (
<div>
<Link to="/newtask">
{" "}
<button> + Create new Task </button>
</Link>
{/* Render TodoList component and pass todos as prop */}
<h1>Todo List</h1>
<TodoList todos={todos} />
</div>
);
}
// Export the TodoPage component
export default Todo;