feat(storage): persist task to localStorage using useEffect and lazy init

This commit is contained in:
2026-04-27 14:28:04 +02:00
parent 6d22fc9e1e
commit 529c3f55b9
+10 -2
View File
@@ -1,11 +1,19 @@
import { useState } from "react" import { useEffect, useState } from "react"
import type { Task } from "./types/task"; import type { Task } from "./types/task";
import { TaskItem } from "./components/TaskItem"; import { TaskItem } from "./components/TaskItem";
function App() { function App() {
const [tasks, setTasks] = useState<Task[]>([]); const [tasks, setTasks] = useState<Task[]>(() => {
const savedTasks = localStorage.getItem('tasks');
return savedTasks ? JSON.parse(savedTasks) : []
});
const [inputValue, setInputValue] = useState<string>(''); const [inputValue, setInputValue] = useState<string>('');
useEffect(() => {
localStorage.setItem('tasks', JSON.stringify(tasks));
}, [tasks]);
const addTask = () => { const addTask = () => {
if (inputValue.trim() === '') return; if (inputValue.trim() === '') return;