ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Props ဆိုတာ component တစ်ခုဆီ ပို့ပေးတဲ့ setting တွေပါ။ ကော်ဖီဆိုင်က order form လိုပါပဲ — ပုံစံက အတူတူ ဖြစ်ပေမယ့် "size: large, milk: oat" လို တန်ဖိုးတွေ ပြောင်းဖြည့်ရင် ကော်ဖီ မတူတာ ထွက်လာပါတယ်။ <CourseCard title="React" lessons={18} /> မှာ title နဲ့ lessons က props တွေပါ။
HTML attribute တွေနဲ့ ရေးပုံ တူပေမယ့် props က ဘယ် JavaScript value မဆို ဖြစ်နိုင်ပါတယ် — string၊ number၊ array၊ object၊ function တောင် ဖြစ်နိုင်ပါတယ်။ Component က အဲဒီ value တွေကို argument တစ်ခုအဖြစ် လက်ခံပြီး UI ထဲမှာ ပြပါတယ်။
Problem → Why → Solution
ပြဿနာ: component တွေက ပုံသေ စာသားပဲ ပြနိုင်တယ်
Components lesson က WelcomeCard ကို ပြန်သုံးလို့ ရပေမယ့် card တိုင်းက "React Course" ပဲ ပြပါတယ်။ React၊ Node.js၊ CSS card သုံးခု လိုချင်တော့ ReactCard၊ NodeCard၊ CssCard ဆိုပြီး markup အတူတူ component သုံးခု ကူးရေးရပြန်ပါတယ် — component ခွဲခဲ့တဲ့ အကျိုး ပျောက်သွားပါတယ်။
function ReactCard() {
return <article className="card"><h2>React</h2><p>18 lessons</p></article>;
}
function NodeCard() {
return <article className="card"><h2>Node.js</h2><p>12 lessons</p></article>;
}
// ...one copy per course: the markup is duplicated againဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Card ရဲ့ ပုံစံ (markup) နဲ့ data (title၊ lessons) ကို ပေါင်းရေးထားလို့ data ပြောင်းတိုင်း component အသစ် လိုနေတာပါ။ Props က အဲဒီ နှစ်ခုကို ခွဲပေးပါတယ် — ပုံစံက CourseCard ထဲမှာ တစ်ခါပဲ ရှိပြီး data ကိုတော့ parent က card တစ်ခုချင်းအတွက် မတူအောင် ပို့ပါတယ်။ Function တစ်ခုကို argument မတူတာနဲ့ ခေါ်သလိုပါပဲ။
Core concept: props object နဲ့ one-way data flow
<CourseCard title="React Foundation" lessons={18} /> လို ရေးလိုက်ရင် React က attribute အားလုံးကို object တစ်ခုထဲ စုပြီး { title: "React Foundation", lessons: 18 } အဖြစ် CourseCard function ရဲ့ ပထမ argument အနေနဲ့ ပို့ပါတယ်။ ဒါက props object ပါ။ function CourseCard(props) လို လက်ခံပြီး props.title နဲ့ ဖတ်လို့ရပေမယ့် function CourseCard({ title, lessons }) လို parameter ထဲမှာ တန်းပြီး destructure လုပ်တာက ပိုအသုံးများပါတယ် — component က ဘာ props တွေ လိုလဲ signature မှာတင် မြင်ရလို့ပါ။
Prop ကို parent က မပို့ရင် အဲဒီ value က undefined ဖြစ်ပါတယ်။ Destructuring ထဲမှာ { level = "Beginner" } လို default value ပေးထားရင် undefined ဖြစ်တဲ့အခါ default ကို သုံးပါတယ်။ သတိထားရမှာက default က undefined အတွက်ပဲ အလုပ်လုပ်ပြီး null ဒါမှမဟုတ် 0 ပို့ရင်တော့ default မဝင်ပါဘူး။ Boolean prop တွေမှာ <Badge isNew /> လို value မပါဘဲ ရေးရင် isNew={true} နဲ့ အတူတူပါ။ Object တစ်ခုထဲက property အားလုံးကို props အဖြစ် ပို့ချင်ရင် <CourseCard {...course} /> လို spread syntax ကို သုံးလို့ရပါတယ်။
Opening tag နဲ့ closing tag ကြားမှာ ထည့်ထားတဲ့ content တွေကို React က children ဆိုတဲ့ prop အထူးတစ်ခုအဖြစ် ပို့ပါတယ်။ <Panel title="Price"><p>45,000 MMK</p></Panel> မှာ <p> က Panel ရဲ့ children ပါ။ ဒါက card၊ modal၊ layout လို "ဘောင်" component တွေ ရေးတဲ့ အဓိကနည်းပါ — ဘောင်က အထဲမှာ ဘာထည့်မလဲ မသိဘဲ {children} နေရာမှာ ပြပေးရုံပါ။
Props တွေက parent ကနေ child ဆီ တစ်ဖက်တည်း စီးဆင်းပါတယ် (one-way data flow)။ Child က props ကို ဖတ်လို့ပဲ ရပြီး ပြင်လို့ မရပါဘူး — development မှာ React က props object ကို freeze လုပ်ထားလို့ props.title = ... လို assign လုပ်ရင် TypeError တက်ပါတယ်။ Data ကို ပြောင်းချင်ရင် data ပိုင်ရှင်ဖြစ်တဲ့ parent က state ကို ပြောင်းပြီး props အသစ် ပြန်ပို့ရပါမယ်၊ အဲဒါကို state lesson မှာ စတွေ့ရပါမယ်။
- Parent က <Child a="x" b={1} /> ရေးတယ်
- React က { a, b } object စုတယ်
- Child က destructure လုပ်ပြီး ဖတ်တယ်
- Data ပြောင်းရင် parent က props အသစ်ပို့
Props က အပေါ်ကနေ အောက်ကို စီးတဲ့ ရေလိုပါ — child က ဆန်တက်ပြီး မပြင်နိုင်ပါဘူး။
အထွေထွေ ပုံစံ
function Child({ textProp, numberProp, optionalProp = "default", children }) {
return <element>{textProp} {numberProp} {optionalProp} {children}</element>;
}
<Child textProp="a string" numberProp={42}>
nested content becomes children
</Child>Child ရဲ့ parameter မှာ props တွေကို destructure လုပ်ပြီး လိုရင် = နဲ့ default ပေးပါတယ်။ Parent ဘက်မှာ string ကို quote နဲ့၊ ကျန်တာကို braces နဲ့ ပို့ပြီး tag ကြားက content က children ဖြစ်ပါတယ်။
Example: props နဲ့ CourseCard
CourseCard က ပုံစံ (template) ကို ကိုင်ထားပြီး App က data ကို ပို့ပါတယ်။ lessons={18} က braces ထဲမှာ number အဖြစ် ပို့ထားပြီး title နဲ့ level ကတော့ quote ထဲက string တွေဖြစ်တာကို သတိထားကြည့်ပါ။ Course နောက်တစ်ခု ပြချင်ရင် App ထဲမှာ value မတူတဲ့ <CourseCard /> တစ်ခု ထပ်ထည့်ရုံပါ၊ card ရဲ့ markup ကို လုံးဝ မထိရပါဘူး။
function CourseCard({ title, level, lessons }) {
return (
<article className="course-card">
<h2>{title}</h2>
<p>Level: {level}</p>
<p>{lessons} lessons included</p>
</article>
);
}
function App() {
return (
<CourseCard
title="React Foundation"
level="Starter"
lessons={18}
/>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 1
function CourseCard({ title, level, lessons }) {Props object ကို parameter ထဲမှာ တန်းပြီး destructure လုပ်ပါတယ်။ { } မပါဘဲ function CourseCard(title) လို့ ရေးရင် title က props object တစ်ခုလုံး ဖြစ်သွားပြီး render လုပ်တဲ့အခါ error တက်ပါတယ်။
- Line 4
<h2>{title}</h2>Parent ပို့တဲ့ title value က ဒီနေရာမှာ ပေါ်ပါတယ်။ CourseCard ကိုယ်တိုင်က ဘယ် course လဲ မသိပါဘူး၊ ပို့ပေးတာကိုပဲ ပြပါတယ်။
- Line 6
<p>{lessons} lessons included</p>lessons က number 18 ဖြစ်လို့ "18 lessons included" ပေါ်ပါတယ်။ lessons + 1 လို တွက်ချက်ရင်လည်း 19 ရပါမယ်။
- Lines 14-16
title="React Foundation"Parent ဘက်မှာ props တွေကို attribute ပုံစံ ရေးပါတယ်။ Props များရင် ဒီလို line တစ်ကြောင်းကို prop တစ်ခုစီ ခွဲရေးတာက ဖတ်ရ လွယ်ပါတယ်။
- Line 16
lessons={18}Braces နဲ့ ပို့လို့ number ဖြစ်ပါတယ်။ lessons="18" လို quote နဲ့ ပို့ရင် string ဖြစ်သွားပြီး lessons + 1 က "181" ဖြစ်ပါတယ်။
Course title, level, lessons count ပါတဲ့ card တစ်ခု ပေါ်လာမယ်။children နဲ့ default value ကို တွဲသုံးခြင်း
ဒီ example မှာ Panel က ဘောင် component ဖြစ်ပြီး children ကို ပြပါတယ်။ PriceTag ကတော့ currency နဲ့ discount အတွက် default value ပေးထားလို့ Mouse card မှာ discount မပို့လည်း 0 အဖြစ် အလုပ်လုပ်ပါတယ်။ discount > 0 && ... က discount ရှိမှ badge ကို ပြပါတယ်။
function Panel({ title, children }) {
return (
<section className="panel">
<h3>{title}</h3>
{children}
</section>
);
}
function PriceTag({ amount, currency = "MMK", discount = 0 }) {
const finalAmount = amount - (amount * discount) / 100;
return (
<p className="price">
{finalAmount.toLocaleString("en-US")} {currency}
{discount > 0 && <span className="badge"> -{discount}%</span>}
</p>
);
}
export default function Shop() {
return (
<main>
<Panel title="Keyboard"><PriceTag amount={45000} discount={10} /></Panel>
<Panel title="Mouse"><PriceTag amount={18000} /></Panel>
</main>
);
}Render ရလဒ်မှာ Keyboard panel က "40,500 MMK -10%" ကို ပြပြီး Mouse panel က badge မပါဘဲ "18,000 MMK" ကို ပြပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ
Real app တွေမှာ data က API ကနေ array အဖြစ် လာပြီး {courses.map((course) => <CourseCard key={course.id} {...course} />)} လို item တစ်ခုစီကို props အဖြစ် ပို့ပြီး card တွေ ဆွဲလေ့ရှိပါတယ်။ Design system ထဲက Button component တစ်ခုမှာ variant="primary"၊ size="small"၊ disabled လို props တွေနဲ့ ပုံစံ အမျိုးမျိုး ထုတ်ပြီး Layout၊ Modal၊ Card လို component တွေက children နဲ့ အထဲက content ကို လက်ခံကြပါတယ်။
Function တွေကိုလည်း props အဖြစ် ပို့ကြပါတယ် — <SearchBox onSearch={handleSearch} /> လို ပို့ပြီး child က user ရိုက်ထည့်တာကို parent ဆီ ပြန်အကြောင်းကြားပါတယ်။ ဒါက one-way data flow ကို မချိုးဖောက်ပါဘူး၊ child က parent ရဲ့ data ကို တိုက်ရိုက် မပြင်ဘဲ parent ပေးထားတဲ့ function ကို ခေါ်ရုံပါ။ Events lesson မှာ ဒီ pattern ကို အသေးစိတ် လေ့လာပါမယ်။ TypeScript project တွေမှာတော့ props တွေကို type ပေးပြီး လိုအပ်တဲ့ prop ကျန်ခဲ့ရင် editor က ချက်ချင်း သတိပေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Number ကို quote နဲ့ ပို့တာ
<CourseCard lessons="18" /> ဆိုရင် lessons က string ဖြစ်ပြီး {lessons + 1} က 19 မဟုတ်ဘဲ "181" ပြပါတယ်။ Error မတက်လို့ သတိမထားမိဘဲ ကျော်သွားတတ်ပါတယ်။ String မဟုတ်တဲ့ value တိုင်းကို lessons={18} လို braces နဲ့ ပို့ပါ။
2. Destructuring braces ကို မေ့တာ
function CourseCard(title) လို့ ရေးရင် title က string မဟုတ်ဘဲ props object တစ်ခုလုံး ဖြစ်ပါတယ်။ <h2>{title}</h2> က "Objects are not valid as a React child (found: object with keys {title})" error ပေးပါတယ်။ function CourseCard({ title }) လို braces ထည့်ပါ။
3. Props ကို child ထဲမှာ ပြင်တာ
props.title = props.title.toUpperCase(); လို ရေးရင် development မှာ "Cannot assign to read only property 'title'" TypeError တက်ပါတယ်။ ပြောင်းထားတဲ့ value လိုရင် const upperTitle = title.toUpperCase(); လို variable အသစ်ထဲ ထည့်ပါ။
4. Default က null အတွက်လည်း အလုပ်လုပ်မယ်လို့ ထင်တာ
{ level = "Beginner" } က level={null} ပို့ရင် အလုပ်မလုပ်ပါဘူး၊ null က undefined မဟုတ်လို့ level က null ဖြစ်ပြီး ဘာမှ မပြပါဘူး။ API data မှာ null ပါနိုင်ရင် level ?? "Beginner" ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Props ကို parameter ထဲမှာ တန်း destructure လုပ်ပါ၊ component က ဘာ data လိုလဲ signature မှာတင် မြင်ရပါတယ်။
- Prop နာမည်တွေကို component ရဲ့ အမြင်ကနေ ပေးပါ — CourseCard အတွက် title က reactCourseTitle ထက် ပိုကောင်းပါတယ်။
- Boolean prop တွေကို is၊ has နဲ့ စပြီး (isOnline၊ hasDiscount) နာမည်ပေးပါ၊ ဖတ်တဲ့အခါ yes/no မေးခွန်းလို ရှင်းပါတယ်။
- Props ဆယ်ခုလောက် များလာရင် component တာဝန် များနေတဲ့ လက္ခဏာပါ၊ children သုံးတာ ဒါမှမဟုတ် component ခွဲတာကို စဉ်းစားပါ။
Mini challenge
Mini challenge: profile card ပြန်သုံးခြင်း
ProfileCard component တစ်ခု ရေးပြီး Team component ထဲမှာ member သုံးယောက်အတွက် props မတူအောင် ပို့ကာ သုံးပါ။
- ProfileCard က name၊ role၊ years (number) နဲ့ isOnline (boolean) props ကို destructure လုပ်ရမယ်
- role ရဲ့ default က "Member" ဖြစ်ရမယ်
- isOnline က true ဖြစ်ရင် "online" badge ကို && နဲ့ ပြရမယ်
- Children ကို card အောက်ခြေမှာ ပြရမယ် (ဥပမာ bio စာကြောင်း)
function ProfileCard(/* TODO: destructure props, default role = "Member" */) {
return (
<article className="profile">
{/* TODO: name in an <h3> */}
{/* TODO: "<role> - <years> years" */}
{/* TODO: online badge only when isOnline */}
{/* TODO: children */}
</article>
);
}
export default function Team() {
return (
<section>
{/* TODO: three ProfileCards; one without role, one with isOnline */}
</section>
);
}ရလဒ်: Card သုံးခု ပေါ်ပါမယ် — "Aye Aye" (Designer - 4 years, online badge, bio ပါ)၊ "Min Thu" (Developer - 2 years)၊ "Hla Hla" (role မပို့လို့ Member - 1 years)။
အရိပ်အမြွက် ကြည့်ရန်
Boolean prop ကို <ProfileCard isOnline /> လို value မပါဘဲ ရေးလို့ရပါတယ်။ Children ပို့ချင်ရင် self-closing မလုပ်ဘဲ <ProfileCard ...><p>...</p></ProfileCard> လို ဖွင့်ပိတ် tag သုံးပါ။
အဖြေ ကြည့်ရန်
function ProfileCard({ name, role = "Member", years, isOnline, children }) {
return (
<article className="profile">
<h3>{name}</h3>
<p>
{role} - {years} years
</p>
{isOnline && <span className="badge">online</span>}
{children}
</article>
);
}
export default function Team() {
return (
<section>
<ProfileCard name="Aye Aye" role="Designer" years={4} isOnline>
<p>Turns sketches into clean interfaces.</p>
</ProfileCard>
<ProfileCard name="Min Thu" role="Developer" years={2} />
<ProfileCard name="Hla Hla" years={1} />
</section>
);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Props က parent ကနေ child ဆီ ပို့တဲ့ data ပါ၊ React က attribute အားလုံးကို object တစ်ခု အဖြစ် ပို့ပါတယ်။
- String ကို quote နဲ့၊ ကျန်တဲ့ value တွေကို braces နဲ့ ပို့ပြီး destructuring ထဲမှာ default ပေးလို့ရပါတယ်။
- Tag ကြားက content က children prop ဖြစ်ပြီး ဘောင် component တွေ ရေးဖို့ သုံးပါတယ်။
- Props က read-only ဖြစ်ပြီး data က parent ကနေ child ဆီ တစ်ဖက်တည်း စီးပါတယ်။
React Documentation — React