-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandingWork.txt
101 lines (84 loc) · 2.4 KB
/
pandingWork.txt
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
- simple methods
- projects
- Navigate (BrowserRouter method)
- redux
import { useState, useEffect } from 'react';
const Todo = () => {
const [todos, setTodos] = useState([]);
const [task, setTask] = useState('');
useEffect(() => {
const storedTodos = JSON.parse(localStorage.getItem('todos'));
if (storedTodos) {
setTodos(storedTodos);
}
}, []);
const addTodo = () => {
if (task) {
const newTodo = { id: Date.now(), task };
setTodos([...todos, newTodo]);
setTask('');
// Save todos to local storage
localStorage.setItem('todos', JSON.stringify([...todos, newTodo]));
}
};
const removeTodo = (id) => {
const updatedTodos = todos.filter((todo) => todo.id !== id);
setTodos(updatedTodos);
// Update local storage after removing a todo
localStorage.setItem('todos', JSON.stringify(updatedTodos));
};
return (
<div>
<h1>Todo App</h1>
<input
type="text"
placeholder="Add a new task"
value={task}
onChange={(e) => setTask(e.target.value)}
/>
<button onClick={addTodo}>Add</button>
<ul>
{todos.map((todo) => (
<li key={todo.id}>
{todo.task}
<button onClick={() => removeTodo(todo.id)}>Remove</button>
</li>
))}
</ul>
</div>
);
};
export default Todo;
// -------------------
import React, { useState } from "react";
export default function Todo() {
const [todoInput, setTodoInput] = useState([]);
const [todoHeading, setTodoHeading] = useState("");
const [todoTitle, setTodoTitle] = useState("");
const addTodo = () => {
if (todoHeading && todoTitle) {
const newTodo = { id: Date.now(), heading: todoHeading, title: todoTitle };
setTodoInput([...todoInput, newTodo]);
setTodoHeading(""); // Clear the input fields
setTodoTitle("");
}
}
return (
<>
<div>
<input type="text" name="todoHeading" value={todoHeading} onChange={e => setTodoHeading(e.target.value)} />
<input type="text" name="todoTitle" value={todoTitle} onChange={e => setTodoTitle(e.target.value)} />
<button onClick={addTodo}>Submit</button>
</div>
<div>
<ul>
{todoInput.map(todo => (
<li key={todo.id}>
<strong>{todo.heading}:</strong> {todo.title}
</li>
))}
</ul>
</div>
</>
);
}