Skip to content

Commit 87e1be9

Browse files
committed
basic todo is compleate without local storage
1 parent 345f311 commit 87e1be9

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed
Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
1-
import React from 'react'
1+
import React, { useState } from "react";
22

33
const Todo = () => {
4+
const [todos, setTodos] = useState([]);
5+
const [todo, setTodo] = useState("");
6+
7+
const addTodo = () => {
8+
if (todo.trim() !== "") {
9+
let newTodo = { id: Date.now(), content: todo };
10+
setTodos((prev) => [...prev, newTodo]);
11+
setTodo("");
12+
}
13+
};
14+
15+
const removeTodo = (id) => {
16+
setTodos(todos.filter((todo) => todo.id !== id));
17+
};
18+
19+
const updateTodo = (id, newText) => {
20+
setTodos(
21+
todos.map((todo) =>
22+
todo.id === id ? { ...todo, content: newText } : todo
23+
)
24+
);
25+
};
26+
27+
const handleChange = (e) => {
28+
setTodo(e.target.value);
29+
};
30+
431
return (
5-
<div>Todo</div>
6-
)
7-
}
32+
<div className="flex flex-col h-screen items-center">
33+
<h1 className="text-5xl font-bold font-sans mt-10">Todo Local Storage</h1>
34+
<div className="flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 mt-10">
35+
<input
36+
className="w-[25rem] px-3 py-1 border border-black rounded-sm"
37+
type="text"
38+
placeholder="learn todo"
39+
value={todo}
40+
onChange={handleChange}
41+
/>
42+
<button
43+
className="bg-red-700 px-10 py-2 text-white font-bold"
44+
onClick={addTodo}
45+
>
46+
Add
47+
</button>
48+
</div>
49+
<div className="mt-10">
50+
{todos.map((todo) => (
51+
<div key={todo.id} className="flex space-x-4">
52+
<p>{todo.content}</p>
53+
<button
54+
className="bg-blue-500 px-4 py-1 text-white rounded"
55+
onClick={() => updateTodo(todo.id, prompt("Update todo", todo.content))}
56+
>
57+
Update
58+
</button>
59+
<button
60+
className="bg-red-500 px-4 py-1 text-white rounded"
61+
onClick={() => removeTodo(todo.id)}
62+
>
63+
Remove
64+
</button>
65+
</div>
66+
))}
67+
</div>
68+
</div>
69+
);
70+
};
871

9-
export default Todo
72+
export default Todo;

0 commit comments

Comments
 (0)