-
Notifications
You must be signed in to change notification settings - Fork 6
Fix: Local Storage Persistence #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||
| import { useState, useEffect } from 'react' | ||||||
| import { TodoForm, TodoItem } from './components' | ||||||
| import { TodoProvider } from './context' | ||||||
|
|
||||||
| function App() { | ||||||
| const [todos, setTodos] = useState([]) | ||||||
|
|
||||||
| const addTodos = (todo) => { | ||||||
| setTodos((prev) => [{ id: Date.now(), ...todo }, ...prev]) | ||||||
| } | ||||||
|
|
||||||
| const updateTodo = (id, todo) => { | ||||||
| setTodos((prev) => | ||||||
| prev.map((prevTodo) => (prevTodo.id === id ? todo : prevTodo)) | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| const deleteTodo = (id) => { | ||||||
| setTodos((prev) => prev.filter((todo) => todo.id !== id)) | ||||||
| } | ||||||
|
|
||||||
| const toggleComplete = (id) => { | ||||||
| setTodos((prev) => | ||||||
| prev.map((prevTodo) => | ||||||
| prevTodo.id === id | ||||||
| ? { ...prevTodo, completed: !prevTodo.completed } | ||||||
| : prevTodo | ||||||
| ) | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| // Load todos from localStorage on initial render | ||||||
| useEffect(() => { | ||||||
| const savedTodos = JSON.parse(localStorage.getItem('todos')) | ||||||
| if (savedTodos && savedTodos.length > 0) { | ||||||
| setTodos(savedTodos) | ||||||
| } | ||||||
| }, []) | ||||||
|
|
||||||
| // Save todos to localStorage whenever todos change | ||||||
| useEffect(() => { | ||||||
| localStorage.setItem('todos', JSON.stringify(todos)) | ||||||
| }, [todos]) | ||||||
|
|
||||||
| return ( | ||||||
| <TodoProvider value={{ todos, addTodos, updateTodo, deleteTodo, toggleComplete }}> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Context value key mismatch breaks toggle completion. At Line 46, Proposed fix- <TodoProvider value={{ todos, addTodos, updateTodo, deleteTodo, toggleComplete }}>
+ <TodoProvider value={{ todos, addTodos, updateTodo, deleteTodo, toggleComplete, toggleCompleted: toggleComplete }}>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| <div className="bg-[#172842] min-h-screen py-8"> | ||||||
| <div className="w-full max-w-2xl mx-auto shadow-md rounded-lg px-4 py-3 text-white"> | ||||||
| <h1 className="text-2xl font-bold text-center mb-8 mt-2">Manage Your Todos</h1> | ||||||
| <div className="mb-4"> | ||||||
| {/* Todo form goes here */} | ||||||
| <TodoForm /> | ||||||
| </div> | ||||||
| <div className="flex flex-wrap gap-y-3"> | ||||||
| {/* Loop and Add TodoItem here */} | ||||||
| {todos.map((todo) => ( | ||||||
| <div key={todo.id} className="w-full"> | ||||||
| <TodoItem todo={todo} /> | ||||||
| </div> | ||||||
| ))} | ||||||
| </div> | ||||||
| </div> | ||||||
| </div> | ||||||
| </TodoProvider> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export default App | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard localStorage deserialization to prevent mount-time crash.
At Line 34,
JSON.parse(...)is unguarded. Invalid or manually-corrupted localStorage data will throw and crash the app on initial render.Proposed fix
useEffect(() => { - const savedTodos = JSON.parse(localStorage.getItem('todos')) - if (savedTodos && savedTodos.length > 0) { - setTodos(savedTodos) - } + const raw = localStorage.getItem('todos') + if (!raw) return + try { + const savedTodos = JSON.parse(raw) + if (Array.isArray(savedTodos)) { + setTodos(savedTodos) + } + } catch { + localStorage.removeItem('todos') + } }, [])📝 Committable suggestion
🤖 Prompt for AI Agents