ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
စာရွက်ပေါ်မှာ ရေးထားတဲ့ to-do list တစ်ခုနဲ့ ဖုန်း screen ပေါ်က to-do app ကို နှိုင်းယှဉ်ကြည့်ပါ။ App ထဲမှာ "အမှန်တကယ်" list က screen ပေါ်က စာသားတွေ မဟုတ်ပါဘူး။ Memory ထဲက array တစ်ခုပါ။ Screen ကတော့ အဲဒီ array ရဲ့ ဓာတ်ပုံတစ်ပုံလိုပါပဲ။
ဒီ project ရဲ့ စည်းမျဉ်းက ရိုးရှင်းပါတယ်။ ပြောင်းလဲမှုတိုင်းကို array (state) မှာ အရင်လုပ်ပြီးမှ screen ကို state ကနေ ပြန်ဆွဲပါတယ်။ Screen ကို တိုက်ရိုက် လိုက်ပြင်တာ မလုပ်ပါဘူး။ ဒါဆိုရင် screen နဲ့ data က ဘယ်တော့မှ မကိုက်ညီဘဲ မဖြစ်ပါဘူး။ Page ကို reload လုပ်လည်း localStorage ထဲက state ကနေ ပြန်စလို့ ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: array index ကို id အဖြစ် သုံးတာ
ပထမ version မှာ developer က todo တွေကို string array အဖြစ် သိမ်းပြီး button တစ်ခုစီမှာ data-index ဆိုပြီး array index ကို တပ်ထားပါတယ်။ Item တစ်ခုကို ဖျက်လိုက်တာနဲ့ နောက်က item တွေ အားလုံး index တစ်နေရာစီ ရှေ့တိုးသွားပါတယ်။ ဒါပေမဲ့ screen ပေါ်က button တွေကို ပြန်မဆွဲရသေးလို့ data-index အဟောင်းတွေကို ကိုင်ထားဆဲပါ။
User က "Call Ana" ကို ဖျက်ဖို့ နှိပ်လိုက်ပေမယ့် တကယ်ပျက်သွားတာက နောက်က item ဖြစ်နေပါတယ်။ Done ဖြစ်မဖြစ်ကိုလည်း CSS class အဖြစ် DOM ထဲမှာပဲ သိမ်းထားလို့ page reload လုပ်တာနဲ့ ပျောက်သွားပါတယ်။
const todos = ["Buy milk", "Call Ana", "Write report"];
// buttons were rendered earlier with data-index="0", "1", "2"
list.addEventListener("click", (event) => {
const index = Number(event.target.dataset.index);
todos.splice(index, 1); // every later item shifts left
event.target.closest("li").remove();
// the remaining buttons still say 1 and 2, but the array is now 0 and 1
});
// "done" lives only in the DOM, so a reload forgets it
li.classList.add("done");ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Index က item ရဲ့ identity မဟုတ်ဘဲ "လက်ရှိ ဘယ်နေရာမှာ ရှိလဲ" ဆိုတာပဲ ဖြစ်လို့ ဖျက်တာ၊ sort လုပ်တာ၊ filter လုပ်တာနဲ့ ပြောင်းသွားပါတယ်။ Data (array) နဲ့ screen (DOM) ကို သီးခြားစီ ပြင်နေရင် အချိန်တစ်ခုမှာ နှစ်ခု မကိုက်တော့ပါဘူး။ Todo တစ်ခုစီကို ဘယ်တော့မှ မပြောင်းတဲ့ id ပေးပြီး screen ကို state ကနေ အမြဲ ပြန်ဆွဲရင် ပြဿနာ နှစ်ခုလုံး ပျောက်သွားပါတယ်။
Core concept: state က အမှန်တရား၊ UI က ပုံရိပ်
ဒီ app ကို အလွှာသုံးခု ခွဲပါတယ်။ ပထမ အလွှာက state logic ပါ။ State ကိုယ်တိုင်က { nextId, items } ဆိုတဲ့ object တစ်ခုဖြစ်ပြီး item တစ်ခုစီမှာ id၊ title နဲ့ completed ပါပါတယ်။ ဒုတိယ အလွှာက storage adapter ဖြစ်ပြီး state ကို string အဖြစ် သိမ်းပေးပြီး ပြန်ဖတ်ပေးပါတယ်။ တတိယ အလွှာက DOM renderer ဖြစ်ပြီး state ကနေ HTML ကို ဆွဲပေးပါတယ်။ ပထမ အလွှာနှစ်ခုက DOM ကို လုံးဝ မသိလို့ Node ထဲမှာ test လုပ်နိုင်ပါတယ်။
၁။ Id ကို state ထဲမှာ ထုတ်ပေးပါ
nextId ကို state ထဲမှာ သိမ်းထားပြီး todo အသစ်တိုင်းက လက်ရှိ nextId ကို ယူကာ nextId ကို တစ်တိုးပါတယ်။ Item ကို ဖျက်လည်း nextId က ပြန်မကျလို့ id တစ်ခုကို ဘယ်တော့မှ ပြန်မသုံးပါဘူး။ Server ပါတဲ့ app တွေမှာတော့ crypto.randomUUID() လို unique id ကို သုံးလေ့ရှိပါတယ်။
၂။ ပြောင်းလဲမှုတိုင်း state အသစ် ပြန်ပေးပါ
Add က spread နဲ့ array အသစ်ဆောက်ပြီး toggle က map နဲ့ ပြောင်းချင်တဲ့ item ကိုပဲ copy အသစ်လုပ်ပါတယ်။ Delete က filter နဲ့ ဖယ်ထုတ်ပါတယ်။ State အဟောင်းကို ဘယ်တော့မှ မပြင်ပါဘူး။
၃။ Filter က view တစ်ခုသာ ဖြစ်ပါတယ်
"Active" ဒါမှမဟုတ် "Done" ကို ရွေးတဲ့အခါ state ထဲက item တွေကို မဖျက်ပါဘူး။ visibleTodos() က ပြဖို့ list ကိုပဲ တွက်ပေးပါတယ်။ Filter ကို ဖျက်ပစ်ရင် data ပျောက်သွားမှာ ဖြစ်လို့ ဒီခွဲခြားမှုက အရေးကြီးပါတယ်။
၄။ ပြန်ဖတ်တဲ့ data ကို မယုံပါနဲ့
localStorage ထဲက string ကို user ကိုယ်တိုင်၊ extension တစ်ခုခု ဒါမှမဟုတ် app version အဟောင်းက ပြင်ထားနိုင်ပါတယ်။ JSON.parse ကို try/catch ထဲ ထည့်ပြီး shape မမှန်တဲ့ item တွေကို စစ်ထုတ်ပါ။ nextId ကိုလည်း ရှိပြီးသား id အကြီးဆုံးထက် ကြီးအောင် ပြန်တွက်ပါ။
List တစ်ခုလုံး ပြန်ဆွဲတာ နှေးမလား
Todo ရာဂဏန်းလောက်အထိ list တစ်ခုလုံးကို ပြန်ဆွဲတာက မျက်စိနဲ့ မမြင်နိုင်လောက်အောင် မြန်ပြီး code ကို အရမ်းရိုးရှင်းစေပါတယ်။ Item ထောင်ချီလာမှသာ ပြောင်းသွားတဲ့ row တွေကိုပဲ update လုပ်တဲ့ နည်းလမ်း ဒါမှမဟုတ် အဲဒါကို လုပ်ပေးတဲ့ framework တစ်ခုကို စဉ်းစားပါ။
- Event က state function ကို ခေါ်
- Function က state အသစ် ပြန်ပေး
- State အသစ်ကို storage ထဲ သိမ်း
- List ကို state ကနေ အကုန်ပြန်ဆွဲ
Screen ပေါ်က ဘာကိုမှ ကိုယ်တိုင် ပြင်ဖို့ စိတ်ကူးပေါ်လာရင် ရပ်လိုက်ပါ။ State ကို ပြင်ပြီး render() ကို ခေါ်ပါ။
အခြေခံ ပုံစံ
const add = (state, data) => ({
nextId: state.nextId + 1,
items: [...state.items, { id: state.nextId, ...data }],
});
const update = (state, id, changes) => ({
...state,
items: state.items.map((it) => (it.id === id ? { ...it, ...changes } : it)),
});
const remove = (state, id) => ({
...state,
items: state.items.filter((it) => it.id !== id),
});Collection တစ်ခုကို ကိုင်တွယ်တဲ့ state တိုင်းအတွက် ပုံစံသုံးမျိုးပါ။ ထည့်ရင် spread နဲ့ array အသစ်၊ ပြင်ရင် map နဲ့ item တစ်ခုကိုပဲ copy အသစ်၊ ဖျက်ရင် filter ပါ။ ဘယ်အရာကိုမှ နေရာမှာ တိုက်ရိုက် မပြင်ပါဘူး။
Todo logic နဲ့ storage ကို Node ထဲမှာ run ကြည့်မယ်
Browser ရဲ့ localStorage က Node မှာ မရှိလို့ createMemoryStorage() က getItem/setItem ပုံစံတူ object တစ်ခုကို Map နဲ့ ဆောက်ပေးပါတယ်။ saveState နဲ့ loadState က storage ကို parameter အဖြစ် လက်ခံလို့ browser ထဲမှာ localStorage ကို တိုက်ရိုက် ထည့်ပေးလိုက်ရုံပါပဲ။ Demo မှာ title အလွတ်ကို ignore လုပ်ပုံ၊ toggle/delete ပြီးနောက် id တွေ မပြောင်းပုံနဲ့ data ပျက်နေတဲ့အခါ app မပျက်ပုံကို ကြည့်ပါ။
// todo-logic.js - state transitions only, no DOM
function createState() {
return { nextId: 1, items: [] };
}
function addTodo(state, text) {
const title = text.trim();
if (title === "") return state; // ignore empty input
const todo = { id: state.nextId, title, completed: false };
return { nextId: state.nextId + 1, items: [...state.items, todo] };
}
function toggleTodo(state, id) {
const items = state.items.map((t) =>
t.id === id ? { ...t, completed: !t.completed } : t,
);
return { ...state, items };
}
function deleteTodo(state, id) {
return { ...state, items: state.items.filter((t) => t.id !== id) };
}
function visibleTodos(items, filter) {
if (filter === "active") return items.filter((t) => !t.completed);
if (filter === "done") return items.filter((t) => t.completed);
return items; // "all"
}
function formatTodo(t) {
return `[${t.completed ? "x" : " "}] ${t.id}. ${t.title}`;
}
// Persistence: same getItem/setItem shape as the browser's localStorage
function createMemoryStorage() {
const data = new Map();
return {
getItem: (key) => (data.has(key) ? data.get(key) : null),
setItem: (key, value) => data.set(key, String(value)),
};
}
function saveState(storage, state) {
storage.setItem("todos", JSON.stringify(state));
}
function loadState(storage) {
try {
const saved = JSON.parse(storage.getItem("todos"));
if (!saved || !Array.isArray(saved.items)) return createState();
const items = saved.items.filter(
(t) => t && typeof t.id === "number" && typeof t.title === "string",
);
const maxId = items.reduce((max, t) => Math.max(max, t.id), 0);
return { nextId: Math.max(Number(saved.nextId) || 1, maxId + 1), items };
} catch {
return createState(); // corrupted JSON: start fresh instead of crashing
}
}
// Demo: every step replaces state with a new object
let state = createState();
state = addTodo(state, "Learn JavaScript");
state = addTodo(state, "Build a project");
state = addTodo(state, " "); // ignored: no id is used up
state = addTodo(state, "Master React");
state = toggleTodo(state, 1);
state = deleteTodo(state, 2);
console.log(state.items.map(formatTodo).join("\n"));
console.log("Active:", visibleTodos(state.items, "active").map((t) => t.title));
const storage = createMemoryStorage();
saveState(storage, state);
const reloaded = loadState(storage);
console.log("Reloaded:", reloaded.items.length, "items, next id", reloaded.nextId);
storage.setItem("todos", "{not json");
console.log("Corrupted:", loadState(storage).items.length, "items");Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Stable ids from the state
return { nextId: state.nextId + 1, items: [...state.items, todo] };Todo အသစ်က လက်ရှိ nextId ကို ယူပြီး state အသစ်မှာ nextId က တစ်တိုးပါတယ်။ Title အလွတ်ဆိုရင် ဒီ line ကို မရောက်လို့ id တစ်ခုမှ မကုန်ပါဘူး။ ဒါကြောင့် "Master React" က 3 ရပါတယ်။
- Copy only what changes
t.id === id ? { ...t, completed: !t.completed } : t,id ကိုက်တဲ့ item ကိုပဲ completed ပြောင်းပြန်ထားတဲ့ object အသစ်အဖြစ် copy လုပ်ပြီး ကျန်တဲ့ item တွေကိုတော့ ဒီအတိုင်း ပြန်သုံးပါတယ်။
- Filter is a view
if (filter === "active") return items.filter((t) => !t.completed);ပြဖို့ list အသစ်ကိုပဲ ပြန်ပေးပြီး state ထဲက item တွေကို မဖျက်ပါဘူး။ ဒါကြောင့် "All" ကို ပြန်ရွေးရင် item အားလုံး ပြန်ပေါ်ပါတယ်။
- A storage adapter
setItem: (key, value) => data.set(key, String(value)),localStorage လိုပဲ value ကို string အဖြစ် သိမ်းပါတယ်။ ဒါကြောင့် saveState() က JSON.stringify() ကို သုံးရပြီး loadState() က JSON.parse() နဲ့ ပြန်ပြောင်းရပါတယ်။
- Validate what you load
(t) => t && typeof t.id === "number" && typeof t.title === "string",Shape မမှန်တဲ့ item တွေကို ဖယ်ထုတ်ပြီး nextId ကိုလည်း id အကြီးဆုံးထက် ကြီးအောင် ပြန်တွက်ပါတယ်။ JSON ပျက်နေရင်တော့ catch ထဲကနေ state အလွတ်နဲ့ စပါတယ်။
[x] 1. Learn JavaScript
[ ] 3. Master React
Active: [ 'Master React' ]
Reloaded: 2 items, next id 4
Corrupted: 0 itemsLogic ကို DOM နဲ့ ချိတ်ခြင်း
Logic file ထဲက function တွေကို export လုပ်လိုက်ရင် browser ဘက်မှာ အလုပ်သုံးခုပဲ ကျန်ပါတယ်။ render() က state ကနေ <li> တွေကို အသစ်ဆွဲပါတယ်။ update() က state ကို အစားထိုးပြီး သိမ်းကာ render လုပ်ပါတယ်။ Listener တွေကတော့ ဘယ် function ကို ခေါ်ရမလဲ ဆုံးဖြတ်ပါတယ်။ <ul> ပေါ်မှာ click listener တစ်ခုတည်းထားပြီး <li> တစ်ခုစီရဲ့ data-id ကနေ ဘယ် todo လဲ သိပါတယ်။ Todo အသစ်ထည့်လည်း listener ထပ်ချိတ်စရာ မလိုပါဘူး။
<form id="new-todo">
<input name="todo" placeholder="What needs doing?" autocomplete="off" />
<button>Add</button>
</form>
<ul id="list"></ul>
<script type="module">
import { addTodo, toggleTodo, deleteTodo, loadState, saveState }
from "./todo-logic.js";
let state = loadState(localStorage); // localStorage has getItem/setItem too
const list = document.querySelector("#list");
function render() {
list.replaceChildren(...state.items.map((todo) => {
const li = document.createElement("li");
li.dataset.id = todo.id;
li.textContent = todo.title; // textContent, never innerHTML
li.classList.toggle("done", todo.completed);
const del = document.createElement("button");
del.textContent = "Delete";
del.dataset.action = "delete";
li.append(del);
return li;
}));
}
function update(nextState) {
state = nextState;
saveState(localStorage, state);
render();
}
document.querySelector("#new-todo").addEventListener("submit", (event) => {
event.preventDefault();
const input = event.target.elements.todo;
update(addTodo(state, input.value));
input.value = "";
});
list.addEventListener("click", (event) => {
const li = event.target.closest("li");
if (!li) return;
const id = Number(li.dataset.id); // dataset values are strings
if (event.target.dataset.action === "delete") update(deleteTodo(state, id));
else update(toggleTodo(state, id));
});
render();
</script>User စာသားကို innerHTML ထဲ မထည့်ပါနဲ့
li.innerHTML = todo.title လို့ ရေးရင် user က <img src=x onerror=alert(1)> လို title ရိုက်ထည့်တဲ့အခါ အဲဒီ HTML နဲ့ JavaScript က page ထဲမှာ run သွားပါတယ် (XSS)။ localStorage ထဲမှာ သိမ်းထားလို့ reload လုပ်တိုင်း ထပ် run ပါတယ်။ User data ကို textContent နဲ့ပဲ ထည့်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
"State ကနေ UI ကို ဆွဲ" ဆိုတဲ့ စည်းမျဉ်းက React၊ Vue၊ Svelte စတဲ့ framework တိုင်းရဲ့ အခြေခံ အယူအဆပါ။ Framework တွေက ဒီ lesson ရဲ့ render() ကို ပိုထိရောက်အောင် လုပ်ပေးပြီး ပြောင်းသွားတဲ့ element တွေကိုပဲ update လုပ်ပေးတာပဲ ကွာပါတယ်။ React မှာ list item တွေကို key={todo.id} ပေးရတာလည်း ဒီ lesson ရဲ့ stable id ပြဿနာကြောင့်ပါ။ Index ကို key အဖြစ် သုံးရင် item ဖျက်တဲ့အခါ input ထဲက စာတွေ မှားတဲ့ row ကို ရောက်သွားတတ်ပါတယ်။
Storage adapter ပုံစံကလည်း app ကြီးလာတဲ့အခါ အကျိုးရှိပါတယ်။ ဒီနေ့ localStorage သုံးပေမယ့် နောက်ပိုင်း server API ဒါမှမဟုတ် IndexedDB ကို ပြောင်းချင်ရင် saveState/loadState နောက်က implementation ကိုပဲ လဲရုံပါ။ State logic နဲ့ renderer ကို ထိစရာ မလိုပါဘူး။
Production app တွေမှာ saved data ထဲကို version number ထည့်ပြီး shape ပြောင်းတဲ့အခါ data အဟောင်းကို migrate လုပ်ပါတယ်။ Test တွေကိုလည်း add၊ toggle၊ delete၊ filter၊ reload flow တစ်ခုစီအတွက် ရေးပြီး CI မှာ အမြဲ run ပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Array index ကို id အဖြစ် သုံးတာ
Index က ဖျက်တာ၊ sort တာ၊ filter တာနဲ့ ပြောင်းလို့ "Active" filter ဖွင့်ထားချိန် index 1 က list အပြည့်ရဲ့ index 1 နဲ့ မတူတော့ပါဘူး။ မှားတဲ့ todo ကို ဖျက်တာ ဒါမှမဟုတ် toggle လုပ်တာ ဖြစ်ပါတယ်။ Item ဆောက်တဲ့အချိန်မှာ ပေးထားတဲ့ stable id ကိုပဲ သုံးပါ။
data-id ကို string အတိုင်း နှိုင်းယှဉ်တာ
li.dataset.id က "3" ဆိုတဲ့ string ပြန်ပေးပြီး state ထဲက id က number 3 ပါ။ t.id === "3" က false ဖြစ်လို့ toggle နှိပ်လည်း ဘာမှ မဖြစ်ပါဘူး၊ error လည်း မတက်ပါဘူး။ Number(li.dataset.id) နဲ့ ပြောင်းပြီးမှ ပို့ပါ။
State နဲ့ DOM ကို သီးခြားစီ ပြင်တာ
Todo ဖျက်တဲ့အခါ array ထဲက ဖယ်ပြီး li.remove() ကိုလည်း သီးသန့် ခေါ်နေရင် တစ်နေရာမှာ မေ့သွားတာနဲ့ screen နဲ့ data မကိုက်တော့ပါဘူး။ State ကိုပဲ ပြောင်းပြီး render() ကို ခေါ်ပါ။
localStorage data ကို မစစ်ဘဲ ယုံတာ
JSON.parse(localStorage.getItem("todos")) ကို try/catch မပါဘဲ ရေးရင် data တစ်လုံးပျက်တာနဲ့ app တစ်ခုလုံး start မဖြစ်တော့ပါဘူး။ Version အဟောင်းက သိမ်းခဲ့တဲ့ array (object မဟုတ်) ဆိုရင်လည်း items undefined ဖြစ်ပါတယ်။ Parse ကို ကာကွယ်ပြီး shape ကို စစ်ပါ။
Mini challenge
Edit၊ clear completed နဲ့ stats ထည့်ပါ
Todo logic ကို feature အသစ်သုံးခုနဲ့ ချဲ့ပါ။ Todo title ကို ပြင်တဲ့ editTodo၊ ပြီးသွားတဲ့ todo တွေကို ဖယ်တဲ့ clearCompleted နဲ့ စုစုပေါင်း၊ active၊ done အရေအတွက်ကို ပြန်ပေးတဲ့ stats ပါ။ ပြောင်းစရာ မရှိတဲ့အခါ state object တူတူကိုပဲ ပြန်ပေးရပါမယ်။
- editTodo(state, id, newTitle) က title ကို trim လုပ်ပြီး title အလွတ် ဒါမှမဟုတ် id မရှိရင် state တူတူကို ပြန်ပေးပါ
- editTodo က state အဟောင်းထဲက item ကို မပြင်ရပါ
- clearCompleted(state) က completed မဖြစ်သေးတဲ့ item တွေကိုပဲ ချန်ပြီး nextId ကို မပြောင်းပါနဲ့
- stats(items) က { total, active, done } ကို ပြန်ပေးပါ
// Reuse createState, addTodo and toggleTodo from the lesson (or compact versions).
function editTodo(state, id, newTitle) {
// TODO: trim newTitle; if it is blank or no item has this id, return state itself
// TODO: otherwise map over items and copy only the matching one with the new title
}
function clearCompleted(state) {
// TODO: keep unfinished items; nextId must not change
}
function stats(items) {
// TODO: return { total, active, done }
}
// TODO: add three todos, toggle one, then
// - rename todo 1 and show the old state still has the old title
// - show that a blank title and an unknown id both return the same object (===)
// - print stats before and after clearCompleted, plus the remaining idsရလဒ်: Buy oat milk | original kept: Buy milk၊ blank edit ignored: true၊ unknown id ignored: true ထွက်ပြီး clear မလုပ်ခင် { total: 3, active: 2, done: 1 }၊ ပြီးနောက် { total: 2, active: 2, done: 0 } နဲ့ ids left: 1, 3 | next id: 4 ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
state.items.some((t) => t.id === id) နဲ့ id ရှိမရှိ စစ်နိုင်ပါတယ်။ "state တူတူ" ဖြစ်မဖြစ်ကို editTodo(state, 1, " ") === state နဲ့ စစ်ပါ။ done ကို filter(...).length နဲ့ တွက်ပြီး active က total - done ပါ။
အဖြေ ကြည့်ရန်
// Minimal core from the lesson, written compactly
const emptyState = () => ({ nextId: 1, items: [] });
const add = (state, text) => {
const title = text.trim();
if (!title) return state;
const item = { id: state.nextId, title, completed: false };
return { nextId: state.nextId + 1, items: [...state.items, item] };
};
const toggle = (state, id) => ({
...state,
items: state.items.map((t) =>
t.id === id ? { ...t, completed: !t.completed } : t,
),
});
// New features
function editTodo(state, id, newTitle) {
const title = newTitle.trim();
const exists = state.items.some((t) => t.id === id);
if (!title || !exists) return state; // nothing to change: same object back
return {
...state,
items: state.items.map((t) => (t.id === id ? { ...t, title } : t)),
};
}
function clearCompleted(state) {
return { ...state, items: state.items.filter((t) => !t.completed) };
}
function stats(items) {
const done = items.filter((t) => t.completed).length;
return { total: items.length, active: items.length - done, done };
}
// Build a list, then check each feature
let state = emptyState();
for (const text of ["Buy milk", "Write report", "Call Ana"]) state = add(state, text);
state = toggle(state, 2);
const before = state;
state = editTodo(state, 1, " Buy oat milk ");
console.log(state.items[0].title, "| original kept:", before.items[0].title);
console.log("blank edit ignored:", editTodo(state, 1, " ") === state);
console.log("unknown id ignored:", editTodo(state, 99, "Ghost") === state);
console.log("before clear:", stats(state.items));
state = clearCompleted(state);
console.log("after clear:", stats(state.items));
const ids = state.items.map((t) => t.id).join(", ");
console.log("ids left:", ids, "| next id:", state.nextId);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- State ({ nextId, items }) က အမှန်တရားဖြစ်ပြီး UI ကို state ကနေ အမြဲ ပြန်ဆွဲပါ
- Todo တိုင်းကို ဘယ်တော့မှ မပြောင်းတဲ့ stable id ပေးပါ၊ array index ကို id အဖြစ် မသုံးပါနဲ့
- Add/toggle/delete ကို spread၊ map၊ filter နဲ့ state အသစ်ပြန်ပေးတဲ့ pure function အဖြစ် ရေးပါ
- Storage ကို adapter နောက်မှာ ထားပြီး ပြန်ဖတ်တဲ့ data ကို try/catch နဲ့ shape check လုပ်ပါ
- DOM မှာ event delegation၊ Number(dataset.id) နဲ့ textContent ကို သုံးပါ
MDN — JavaScript Guide — MDN Web Docs