ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဒီ lesson မှာ အကြောင်းအရာ အသစ် မသင်ပါဘူး။ Basic chapter မှာ သင်ခဲ့တဲ့ အရာတွေကို လက်တွေ့ ကိုယ်တိုင် ရေးကြည့်တဲ့ အချိန်ပါ။ စာဖတ်ပြီး နားလည်တာနဲ့ ကီးဘုတ်ပေါ်မှာ ကိုယ်တိုင်ရေးနိုင်တာက မတူပါဘူး၊ ဘီးစက်ဘီး စီးနည်း ဗီဒီယိုကြည့်တာနဲ့ ကိုယ်တိုင်စီးတာလိုပါပဲ။
လေ့ကျင့်ခန်း သုံးခုရှိပါတယ်။ Task 1 က +/- button ပါတဲ့ Counter၊ Task 2 က name prop ရှိရင် "Hello, name!"၊ မရှိရင် "Hello, Guest!" ပြတဲ့ Greeting၊ Task 3 က red/green/blue button နှိပ်ရင် box အရောင်ပြောင်းတဲ့ ColorPicker ပါ။ အောက်က ဖြေဆိုချက်တွေကို မကြည့်ခင် ကိုယ်တိုင် အရင်ရေးကြည့်ပါ။
Problem → Why → Solution
ပြဿနာ: ပထမဆုံး ကြိုးစားမှုမှာ ဖြစ်လေ့ရှိတဲ့ bug နှစ်ခု
Task 1 နဲ့ Task 2 ကို ပထမဆုံး ကြိုးစားတဲ့အခါ ဒီလို code ကို အများဆုံး တွေ့ရပါတယ်။ Counter ကို browser မှာ ဖွင့်လိုက်တာနဲ့ screen ဖြူသွားပြီး console ထဲမှာ "Too many re-renders" error ပေါ်ပါတယ်။ Button ကို တစ်ခါမှ မနှိပ်ရသေးပါဘူး။
Greeting ကတော့ error မတက်ပေမယ့် <Greeting /> လို့ prop မပါဘဲ သုံးလိုက်တဲ့နေရာမှာ "Hello, undefined!" ဆိုပြီး ပေါ်နေပါတယ်။ Code ကို ကြည့်ရင် မှန်သလိုလို ထင်ရလို့ ဘာကြောင့်လဲ ရှာရ ခက်ပါတယ်။
function Counter() {
const [count, setCount] = useState(0);
// calls setCount while rendering -> render -> setCount -> render ...
return <button onClick={setCount(count + 1)}>{count}</button>;
}
function Greeting({ name }) {
// name is undefined when the prop is left out, not ""
return <h3>{name === "" ? "Hello, Guest!" : `Hello, ${name}!`}</h3>;
}ဘာကြောင့် ဖြစ်တာလဲ
{setCount(count + 1)} က function ကို ပို့တာ မဟုတ်ဘဲ render လုပ်နေတုန်းမှာ ချက်ချင်း ခေါ်လိုက်တာပါ။ State ပြောင်းလို့ ထပ် render၊ ထပ်ခေါ်၊ ထပ် render ဆိုပြီး မဆုံးတော့လို့ React က ရပ်လိုက်ပါတယ်။ Greeting မှာတော့ prop မပေးရင် value က "" မဟုတ်ဘဲ undefined ဖြစ်လို့ စစ်ဆေးချက်က false ဖြစ်ပြီး undefined ကို string ထဲ ထည့်လိုက်တာပါ။
Core concept: လေ့ကျင့်ခန်းတစ်ခုစီက ဘာကို စစ်ဆေးလဲ
Task သုံးခုက ကြည့်ရတာ ရိုးရှင်းပေမယ့် တစ်ခုစီက React ရဲ့ အခြေခံ စည်းမျဉ်း တစ်ခုစီကို စမ်းသပ်ပါတယ်။ ဖြေဆိုချက်ကို မကြည့်ခင် အောက်က စည်းမျဉ်းလေးခုကို ကိုယ့် code နဲ့ တိုက်စစ်ကြည့်ပါ။
၁။ State က render တစ်ခုရဲ့ snapshot ပါ (Task 1)
count က render တစ်ခုအတွင်းမှာ မပြောင်းပါဘူး။ setCount က React ကို နောက် render အတွက် value အသစ် တောင်းရုံပါ။ Handler တစ်ခုထဲမှာ setCount(count + 1) ကို နှစ်ခါခေါ်ရင် နှစ်ခုလုံး count အဟောင်းကနေ တွက်လို့ +1 ပဲ တိုးပါတယ်။ setCount((c) => c + 1) ကတော့ နောက်ဆုံး value ကနေ တွက်ပါတယ်။
၂။ Event prop ကို function ပေးပါ၊ ခေါ်မပေးပါနဲ့ (Task 1၊ 3)
onClick={increment} နဲ့ onClick={() => setColor(c)} က function ကို ပို့ပါတယ်။ React က click ဖြစ်မှ ခေါ်ပါတယ်။ Argument ပေးဖို့ လိုရင် arrow function နဲ့ ထုပ်ရပါမယ်။
၃။ "မရှိတဲ့ value" ပုံစံ အားလုံးကို စဉ်းစားပါ (Task 2)
Prop မပေးရင် undefined၊ name={null} ဆိုရင် null၊ name="" ဒါမှမဟုတ် name=" " ဆိုရင် string ဖြစ်ပါတယ်။ name?.trim() က undefined/null အတွက် undefined ပြန်ပေးပြီး space တွေကို ဖယ်ပါတယ်။ ရလဒ်ကို truthy ဟုတ်/မဟုတ် စစ်ရင် case လေးခုလုံး "Hello, Guest!" ရပါတယ်။
၄။ Data ကနေ UI ကို ထုတ်ပါ (Task 3)
Button သုံးခုကို copy-paste မလုပ်ဘဲ COLORS array ကို map လုပ်ပါတယ်။ Color string တွေ unique ဖြစ်လို့ key အဖြစ် သုံးလို့ရပါတယ်။ အရောင်အသစ်ထည့်ချင်ရင် array ထဲ တစ်ခုထည့်ရုံပါပဲ။
စည်းမျဉ်း ၂ ကို Node မှာ model လုပ်ကြည့်ရအောင်။ JSX ထဲက onClick ဆိုတာ object ရဲ့ property တစ်ခုပါပဲ။ setCount(count + 1) လို့ ရေးလိုက်ရင် property ထဲ ရောက်တာက function မဟုတ်ဘဲ ခေါ်ပြီးသား ရလဒ်ပါ။
// What JSX receives in onClick, modelled with plain objects.
function setCount(value) {
console.log(" setCount called with", value);
}
const count = 0;
console.log("rendering the wrong button:");
const wrong = { onClick: setCount(count + 1) }; // runs right now
console.log(" wrong.onClick is", typeof wrong.onClick);
console.log("rendering the right button:");
const right = { onClick: () => setCount(count + 1) }; // runs later
console.log(" right.onClick is", typeof right.onClick);
console.log("user clicks the right button:");
right.onClick();
// rendering the wrong button:
// setCount called with 1
// wrong.onClick is undefined
// rendering the right button:
// right.onClick is function
// user clicks the right button:
// setCount called with 1Click တစ်ခါမှ မဖြစ်ခင် "rendering the wrong button" အဆင့်မှာတင် setCount ရဲ့ log ထွက်လာတာကို သတိထားကြည့်ပါ။ Component အစစ်မှာ အဲဒီ log နေရာမှာ state update ရှိလို့ problem section ထဲက re-render loop ကို စတင်ပေးတာ ဒီအချက်ပါပဲ။
- Render က လက်ရှိ state ကို ပြ
- Click က ပို့ထားတဲ့ function ကို run
- Handler က setState ကို ခေါ်
- React က state အသစ်နဲ့ ပြန် render
Render လုပ်နေတုန်း setState ကို ဘယ်တော့မှ မခေါ်ရပါဘူး။ State ပြောင်းတာက event handler (ဒါမှမဟုတ် effect) ထဲမှာပဲ ဖြစ်ရပါမယ်။
အခြေခံ ပုံစံ
const [value, setValue] = useState(initial);
<button onClick={handleClick}>Label</button>
<button onClick={() => setValue((v) => change(v))}>Label</button>
{condition ? <A /> : <B />}
{hasItems && <List />}
{options.map((opt) => (
<button key={opt} onClick={() => setValue(opt)}>{opt}</button>
))}State ကို ကြေညာ၊ event prop ကို function ပေး၊ ရွေးချယ်စရာ နှစ်ခုအတွက် ternary၊ ပြ/မပြ အတွက် && ကို သုံးပြီး array ကနေ button တွေကို key နဲ့ ထုတ်ပါတယ်။
ဖြေဆိုချက်များ: Counter၊ Greeting၊ ColorPicker
ကိုယ်တိုင်ရေးပြီးမှ ဒီဖြေဆိုချက်နဲ့ နှိုင်းယှဉ်ပါ။ File သုံးခုကို comment နဲ့ ခွဲထားပြီး App.jsx က အားလုံးကို တစ်နေရာတည်းမှာ ပြပါတယ်။ ကိုယ့်ဖြေဆိုချက် မတူလည်း စည်းမျဉ်းလေးခုကို လိုက်နာထားရင် မှန်ပါတယ်။
// Task 1 - src/components/Counter.jsx
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function decrement() {
setCount((c) => c - 1);
}
function increment() {
setCount((c) => c + 1);
}
return (
<div className="counter">
<button type="button" onClick={decrement} aria-label="Decrease">-</button>
<span>{count}</span>
<button type="button" onClick={increment} aria-label="Increase">+</button>
</div>
);
}
export default Counter;
// Task 2 - src/components/Greeting.jsx
function Greeting({ name }) {
const cleanName = name?.trim();
return <h3>{cleanName ? `Hello, ${cleanName}!` : "Hello, Guest!"}</h3>;
}
export default Greeting;
// Task 3 - src/components/ColorPicker.jsx
import { useState } from "react";
const COLORS = ["red", "green", "blue"];
function ColorPicker() {
const [color, setColor] = useState("lightgray");
return (
<div className="color-picker">
{COLORS.map((c) => (
<button key={c} type="button" onClick={() => setColor(c)}>
{c}
</button>
))}
<div style={{ background: color, height: 100 }} />
<p>Selected: {color}</p>
</div>
);
}
export default ColorPicker;
// src/App.jsx
import Counter from "./components/Counter";
import Greeting from "./components/Greeting";
import ColorPicker from "./components/ColorPicker";
function App() {
return (
<main>
<Counter />
<Greeting name="Mya" />
<Greeting />
<ColorPicker />
</main>
);
}
export default App;Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Updater form for the counter
setCount((c) => c + 1);c က React ဆီက နောက်ဆုံး value ပါ။ ဒီ handler ကို ဆက်တိုက်ခေါ်ရင်တောင် click တစ်ခါကို +1 အတိအကျ တိုးပါတယ်။
- Passing the handler, not calling it
<button type="button" onClick={increment} aria-label="Increase">+</button>increment နောက်မှာ () မပါလို့ function ကိုယ်တိုင်ကို ပို့ပါတယ်။ aria-label က screen reader အတွက် "+" သင်္ကေတကို စကားလုံးအဖြစ် ပြောပြပါတယ်။
- One check for every empty case
const cleanName = name?.trim();Optional chaining (?.) က name က undefined ဒါမှမဟုတ် null ဖြစ်ရင် error မတက်ဘဲ undefined ပြန်ပေးပါတယ်။ String ဖြစ်ရင် space တွေကို ဖယ်ပါတယ်။
- Ternary chooses the text
<h3>{cleanName ? `Hello, ${cleanName}!` : "Hello, Guest!"}</h3>Empty string နဲ့ undefined နှစ်ခုလုံး falsy ဖြစ်လို့ "Hello, Guest!" ရပါတယ်။ ရွေးချယ်စရာ နှစ်ခုရှိလို့ && မဟုတ်ဘဲ ternary ကို သုံးပါတယ်။
- Buttons generated from data
<button key={c} type="button" onClick={() => setColor(c)}>Arrow function က button တစ်ခုစီရဲ့ c ကို မှတ်ထားလို့ နှိပ်လိုက်တဲ့ button ရဲ့ အရောင်ကို set လုပ်ပါတယ်။ key={c} က အရောင်နာမည် unique ဖြစ်လို့ ရပါတယ်။
- Inline style object
<div style={{ background: color, height: 100 }} />အပြင်ဘက် {} က JSX expression၊ အတွင်းဘက် {} က style object ပါ။ height: 100 ကို React က 100px အဖြစ် ပြောင်းပေးပါတယ်။
စဖွင့်ချိန်မှာ counter က 0၊ greeting နှစ်ခုက "Hello, Mya!" နဲ့ "Hello, Guest!"၊ box က lightgray နဲ့ "Selected: lightgray" ကို ပြပါတယ်။ + / - နှိပ်ရင် number တိုး/လျော့ပြီး red/green/blue နှိပ်ရင် box အရောင်နဲ့ "Selected:" စာ ချက်ချင်း ပြောင်းပါတယ်။Click မနှိပ်ခင် ခန့်မှန်းကြည့်ပါ
လေ့ကျင့်ခန်း ပြီးရင် handler ကို အောက်က ပုံစံတွေနဲ့ ပြောင်းရေးပြီး count 0 ကနေ click တစ်ခါ နှိပ်ရင် ဘာပြမလဲ အရင်ခန့်မှန်းပါ။ ပြီးမှ browser မှာ စစ်ပါ။ ခန့်မှန်းချက် မှားခဲ့ရင် စည်းမျဉ်း ၁ ကို ပြန်ဖတ်ပါ။
| Handler ထဲက code | Click တစ်ခါ ပြီးရင် |
|---|---|
| setCount(count + 1); setCount(count + 1); | 1 (snapshot တူတူကနေ နှစ်ခါတွက်) |
| setCount((c) => c + 1); setCount((c) => c + 1); | 2 (updater က တစ်ခုပြီးတစ်ခု ဆက်တွက်) |
| setCount(count + 1); console.log(count); | Console မှာ 0၊ screen မှာ 1 |
&& နဲ့ 0 ထောင်ချောက်
{count && <p>Clicked {count} times</p>} လို့ ရေးရင် count က 0 ဖြစ်တဲ့အခါ ဘာမှမပြဘဲ မနေဘဲ "0" ဆိုတဲ့ စာလုံးကို screen ပေါ် ပြပါတယ်။ React က false၊ null၊ undefined ကိုပဲ ကျော်ပြီး number 0 ကိုတော့ render လုပ်လို့ပါ။ {count > 0 && ...} လို့ boolean ဖြစ်အောင် ရေးပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
ဒီ component သေးသေးလေး သုံးခုရဲ့ ပုံစံတွေကို production app တိုင်းမှာ တွေ့ရပါတယ်။ Counter က shopping cart ရဲ့ quantity stepper၊ booking form ရဲ့ ဧည့်သည်အရေအတွက် ရွေးတဲ့ control ဖြစ်လာပါတယ်။ Greeting ရဲ့ fallback logic က login မဝင်ရသေးတဲ့ user အတွက် "Guest" ပြတာ၊ profile ပုံ မရှိရင် initial စာလုံး ပြတာ စတဲ့နေရာတွေမှာ သုံးပါတယ်။
ColorPicker ကတော့ size ရွေးတဲ့ button group၊ dashboard ရဲ့ tab တွေ၊ theme switcher တွေရဲ့ အခြေခံပါ။ လက်တွေ့မှာ ဒီလို button group တွေကို aria-pressed ဒါမှမဟုတ် radio input တွေနဲ့ ဆောက်ပြီး keyboard နဲ့ သုံးလို့ရအောင်၊ ရွေးထားတာကို screen reader က ဖတ်ပြနိုင်အောင် လုပ်ပါတယ်။ အခြေခံ ပုံစံ မှန်နေရင် ဒီ accessibility အဆင့်တွေက ထပ်ဖြည့်ရုံပါပဲ။ ဒီလို သီးသန့်ရပ်တည်တဲ့ component သေးသေးလေးတွေကို test လုပ်ရတာလည်း အလွယ်ဆုံးပါ။ Props ပေးပြီး render လုပ်၊ button တစ်ခု နှိပ်၊ ဘယ်စာ ပေါ်လာလဲ စစ်ရုံပါပဲ။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
onClick={setCount(count + 1)} လို့ ရေးတာ
Function ကို render လုပ်နေတုန်း ချက်ချင်းခေါ်လို့ state ပြောင်း၊ ထပ် render၊ ထပ်ခေါ် ဆိုပြီး loop ဖြစ်ကာ "Too many re-renders" error နဲ့ ရပ်သွားပါတယ်။ onClick={() => setCount(count + 1)} ဒါမှမဟုတ် handler နာမည်ကို () မပါဘဲ ပေးပါ။
name === "" တစ်ခုတည်းကိုပဲ စစ်တာ
Prop မပေးရင် undefined ဖြစ်လို့ "Hello, undefined!" ပေါ်ပါတယ်။ name={null} နဲ့ space သက်သက်လည်း ဖြစ်နိုင်ပါတယ်။ name?.trim() ကို truthy စစ်တာနဲ့ case အားလုံးကို တစ်ခါတည်း ဖုံးပါ။
Number ကို && ရဲ့ ဘယ်ဘက်မှာ ထားတာ
{count && ...} က count 0 ဖြစ်ရင် "0" ကို screen ပေါ် ထုတ်ပါတယ်။ {items.length && ...} မှာလည်း ဒီ bug အတူတူ ဖြစ်ပါတယ်။ Condition ကို > 0 ဒါမှမဟုတ် Boolean() နဲ့ boolean ဖြစ်အောင် လုပ်ပါ။
Best Practices
လေ့ကျင့်ခန်းကနေ အကျိုးအများဆုံး ရဖို့
- ဖြေဆိုချက်ကို မကြည့်ခင် အနည်းဆုံး ၁၅ မိနစ် ကိုယ်တိုင် ကြိုးစားပါ၊ ပိတ်မိနေတဲ့ နေရာက သင်ယူရမယ့် နေရာပါ
- Component တစ်ခုကို file တစ်ခုစီမှာ ရေးပြီး App.jsx ကနေ props အမျိုးမျိုးနဲ့ ခေါ်ကြည့်ပါ
- Browser console ကို အမြဲဖွင့်ထားပါ၊ React ရဲ့ warning တွေက bug ကို ကြိုပြောပြလေ့ရှိပါတယ်
- ပြီးသွားရင် ဖြေဆိုချက်နဲ့ နှိုင်းယှဉ်ပြီး ကွာခြားချက် တစ်ခုစီအတွက် "ဘာကြောင့်လဲ" လို့ မေးပါ
Mini challenge
Practice set 2: ကန့်သတ်ချက်ပါတဲ့ logic
Task သုံးခုကို ပိုခက်အောင် ချဲ့ပါမယ်။ Component ထဲ မထည့်ခင် logic ကို pure function သုံးခုအဖြစ် ရေးပြီး Node မှာ စမ်းပါ။ stepCount က counter ကို min/max ကြားမှာပဲ ထားပေးပြီး greetingFor က name ပုံစံ အားလုံးကို ကိုင်တွယ်ကာ nextColor က button တစ်ခုတည်းနဲ့ အရောင်တွေကို အလှည့်ကျ ပြောင်းပေးပါတယ်။
- stepCount(count, delta, min, max) က ရလဒ်ကို min နဲ့ max ကြားမှာ ကန့်သတ်ပါ (0 အောက် မရောက်ရ)
- greetingFor က undefined၊ null၊ "" နဲ့ space သက်သက်အတွက် "Hello, Guest!" ပြန်ပေးပြီး ကျန်တာကို trim လုပ်ပါ
- nextColor(current, colors) က နောက်အရောင်ကို ပြန်ပေးပြီး နောက်ဆုံးကနေ ပထမဆီ ပြန်လှည့်ပါ
- List ထဲမှာ မရှိတဲ့ current (ဥပမာ "lightgray") ဆိုရင် ပထမ အရောင်ကနေ စပါ
function stepCount(count, delta, min, max) {
// TODO: add delta, then keep the result between min and max
}
function greetingFor(name) {
// TODO: only a non-empty string after trim() counts as a name
}
function nextColor(current, colors) {
// TODO: find current's index; move one step forward and wrap around
}
// TODO: simulate clicks + + + + - with limits 0..3 and print each count
// TODO: print greetingFor for "Mya", " Ko Ko ", "", " ", undefined, null
// TODO: start at "lightgray" and print four nextColor stepsရလဒ်: Click အဆင့်တိုင်းရဲ့ count (1၊ 2၊ 3၊ 3၊ 2) နဲ့ "minus at 0 -> 0"၊ name တစ်ခုစီအတွက် greeting (Mya နဲ့ Ko Ko ကလွဲရင် Guest)၊ နောက်ဆုံးမှာ "lightgray -> red -> green -> blue -> red" ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Clamp အတွက် Math.min(max, Math.max(min, value)) ကို သုံးပါ။ indexOf က မတွေ့ရင် -1 ပြန်ပေးလို့ (-1 + 1) % length က 0 ဖြစ်ပြီး ပထမအရောင်ကနေ အလိုလို စပါတယ်။ Component ထဲမှာ setCount((c) => stepCount(c, 1, 0, 3)) လို့ သုံးနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
function stepCount(count, delta, min, max) {
return Math.min(max, Math.max(min, count + delta));
}
function greetingFor(name) {
const clean = typeof name === "string" ? name.trim() : "";
return clean ? `Hello, ${clean}!` : "Hello, Guest!";
}
function nextColor(current, colors) {
const index = colors.indexOf(current);
return colors[(index + 1) % colors.length];
}
let count = 0;
const clicks = [+1, +1, +1, +1, -1];
for (const delta of clicks) {
count = stepCount(count, delta, 0, 3);
console.log(`click ${delta > 0 ? "+" : "-"} -> ${count}`);
}
console.log("minus at 0 ->", stepCount(0, -1, 0, 3));
for (const name of ["Mya", " Ko Ko ", "", " ", undefined, null]) {
const label = typeof name === "string" ? JSON.stringify(name) : String(name);
console.log(label, "->", greetingFor(name));
}
const colors = ["red", "green", "blue"];
let color = "lightgray";
const seen = [color];
for (let i = 0; i < 4; i++) {
color = nextColor(color, colors);
seen.push(color);
}
console.log(seen.join(" -> "));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Event prop ကို function ပေးပါ၊ argument လိုရင် arrow function နဲ့ ထုပ်ပါ
- State က render တစ်ခုရဲ့ snapshot ဖြစ်လို့ အရင် value ပေါ် မူတည်ရင် updater ပုံစံ သုံးပါ
- "Value မရှိ" ဆိုတာ undefined၊ null၊ ""၊ space သက်သက် ဖြစ်နိုင်လို့ name?.trim() ကို truthy စစ်ပါ
- && ရဲ့ ဘယ်ဘက်ကို boolean ဖြစ်အောင် ထားပြီး button တွေကို array ကနေ key နဲ့ ထုတ်ပါ
React Documentation — React