Reading state directly from HTML classes and text leads to desynchronization. Maintain an in-memory JavaScript state array and render the DOM as a pure projection of that data.
Trap #2: Direct innerHTML Injection of Todo Text (XSS)
Rendering item.innerHTML = todo.text allows malicious users to inject script tags. Always use textContent or sanitized DOM creation.
Trap #3: Duplicate IDs from Date.now() in Automated Tests
Generating IDs with Date.now() creates duplicate keys when multiple items are added within the same millisecond. Use crypto.randomUUID().
Trap #4: Full DOM Re-Renders on Single Item Mutations
Wiping and regenerating the entire list on every checkbox click destroys user input focus and degrades performance. Update the specific item element.
Trap #5: Untrimmed Empty String Submissions
Failing to validate input.value.trim() allows users to add blank todos that clutter storage.
💬 Frequently Asked Questions
Why is the Single Source of Truth architecture recommended for UI apps?
Keeping data in a single JavaScript store and rendering the DOM from that state ensures mutations, persistence, and UI display never fall out of sync.
How should unique IDs be generated in client-side applications?
Use crypto.randomUUID() (built into all modern browsers) to generate cryptographically random, collision-proof UUIDv4 strings.
How do you handle keyboard accessibility (Enter key) in todo forms?
Wrap inputs in a
How can you persist todo data across browser reloads?
Serialize the state array to localStorage using JSON.stringify() on save, and deserialize with JSON.parse() during application initialization.
What is Event Delegation and how does it optimize a todo list?
Attaching a single click listener to the parent
container and inspecting event.target.closest("button") handles clicks for all current and future list items with minimal memory usage.