Thuta Learning
React
BasicWeb Developmentအလယ်အလတ်

Components

Reactသင်ခန်းစာ 525 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • PascalCase နာမည်နဲ့ JSX ပြန်ပေးတဲ့ function component တစ်ခု ရေးနိုင်ဖို့
  • Component တွေကို တစ်ခုထဲ တစ်ခု ထည့်ပြီး component tree တစ်ခု ဆောက်နိုင်ဖို့
  • Component ကို file သီးသန့်ခွဲပြီး default export နဲ့ named export ကွာခြားချက်ကို သုံးနိုင်ဖို့
  • Render လုပ်နေစဉ် အပြင်က variable ကို မပြင်ရဘူးဆိုတဲ့ purity စည်းမျဉ်းကို ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Component ဆိုတာ UI အပိုင်းတစ်ခုကို နာမည်ပေးထားတဲ့ function တစ်ခုပါ။ Website တစ်ခုကို ကြည့်လိုက်ရင် header၊ course card၊ footer စတဲ့ အပိုင်းတွေ ထပ်ခါထပ်ခါ ပေါ်နေတာ တွေ့ရပါတယ်။ အဲဒီ အပိုင်းတစ်ခုစီကို component အဖြစ် တစ်ခါရေးပြီး <CourseCard /> လို ကိုယ်ပိုင် HTML tag တစ်ခုလို လိုသလောက် ပြန်သုံးလို့ရပါတယ်။

Component တွေက တခြား component တွေကို ထပ်သုံးလို့ရလို့ App ဆိုတဲ့ component ကြီးတစ်ခုက Header၊ CourseList၊ Footer ကို ပေါင်းထားပြီး CourseList က CourseCard တွေကို ထပ်ပေါင်းထားတယ် စသဖြင့် tree တစ်ခု ဖြစ်လာပါတယ်။

Problem → Why → Solution

ပြဿနာ: copy-paste လုပ်ထားတဲ့ markup ကို နေရာတိုင်းမှာ ပြင်ရတာ

Course page တစ်ခုမှာ card တွေကို HTML အတိုင်း သုံးခု copy-paste လုပ်ထားတယ် ဆိုပါစို့။ Designer က card ထဲကို "Enroll" button ထည့်ခိုင်းတော့ နေရာသုံးခုလုံးမှာ လိုက်ထည့်ရပါတယ်။ Card နှစ်ဆယ်ရှိတဲ့ page တွေ သုံးခုမှာ ဆိုရင် ပြင်ရမယ့်နေရာ ခြောက်ဆယ် ဖြစ်ပြီး တစ်နေရာ ကျန်ခဲ့ရင် card တွေ မညီတော့ပါဘူး။ App component တစ်ခုတည်းက line ရာချီ ရှည်လာပြီး ဘယ် markup က ဘာအတွက်လဲ ရှာရ ခက်လာပါတယ်။

App.jsx
function App() {
  return (
    <main>
      <article className="card">
        <h2>React</h2><p>18 lessons</p><button>Enroll</button>
      </article>
      <article className="card">
        <h2>Node.js</h2><p>12 lessons</p><button>Enroll</button>
      </article>
      <article className="card">
        <h2>CSS</h2><p>20 lessons</p>
      </article>
    </main>
  );
}

ဘာကြောင့် ဒါက ပြဿနာ ဖြစ်တာလဲ

Card ရဲ့ ပုံစံကို တစ်နေရာတည်းမှာ သတ်မှတ်ထားတာ မရှိလို့ ပြင်ဆင်မှုတိုင်းကို copy တိုင်းမှာ လက်နဲ့ ထပ်လုပ်ရပါတယ်။ CSS card က button မပါဘဲ ကျန်ခဲ့တာ ဒီလိုနဲ့ ဖြစ်တာပါ။ Card ကို component တစ်ခု ဖြစ်အောင် ထုတ်လိုက်ရင် ပုံစံက တစ်နေရာတည်းမှာ ရှိပြီး ပြင်လိုက်တာနဲ့ card အားလုံး တစ်ပြိုင်နက် ပြောင်းပါတယ်။

Core concept: component တွေ၊ tree နဲ့ purity

Component တစ်ခုမှာ စည်းမျဉ်း သုံးခုပဲ ရှိပါတယ် — JavaScript function ဖြစ်ရမယ်၊ နာမည်ကို စာလုံးအကြီးနဲ့ စရမယ် (PascalCase)၊ JSX (ဒါမှမဟုတ် ဘာမှ မပြချင်ရင် null) ကို return ပြန်ရမယ်။ စာလုံးအကြီး စည်းမျဉ်းက အရေးကြီးပါတယ် — JSX မှာ <welcomeCard /> လို စာလုံးအသေးနဲ့ စရင် React က HTML tag လို့ ယူပြီး ကိုယ့် function ကို လုံးဝ မခေါ်ပါဘူး။

Component ကို သုံးတာက function ကို ကိုယ်တိုင် ခေါ်တာ မဟုတ်ပါဘူး။ <WelcomeCard /> လို ရေးလိုက်ရင် "ဒီနေရာမှာ WelcomeCard ရှိရမယ်" ဆိုတဲ့ element တစ်ခုပဲ ဖန်တီးပြီး render လုပ်မယ့်အချိန်ကျမှ React က function ကို ခေါ်ပါတယ်။ <WelcomeCard /> ကို နှစ်ခါ ရေးရင် instance နှစ်ခု ဖြစ်ပြီး တစ်ခုချင်းက သီးခြားစီ ရှိပါတယ်။

Project ကြီးလာရင် component တစ်ခုကို file တစ်ခုစီ ခွဲထားလေ့ရှိပါတယ်။ WelcomeCard.jsx ထဲမှာ export default function WelcomeCard() လို့ ရေးပြီး App.jsx ထဲမှာ import WelcomeCard from "./WelcomeCard.jsx"; နဲ့ ယူသုံးပါတယ်။ File တစ်ခုမှာ default export တစ်ခုပဲ ရှိနိုင်ပြီး named export (export function Badge) ကိုတော့ အများကြီး ထားလို့ရကာ import { Badge } from "./Badge.jsx"; လို braces နဲ့ ယူရပါတယ်။

React က component တွေကို pure ဖြစ်ဖို့ မျှော်လင့်ပါတယ်။ Pure ဆိုတာ input (props နဲ့ state) တူရင် JSX ရလဒ် အမြဲတူရမယ်၊ render လုပ်နေစဉ် component အပြင်က variable၊ object ဒါမှမဟုတ် DOM ကို မပြင်ရဘူးလို့ ဆိုလိုပါတယ်။ React က component တစ်ခုကို ဘယ်နှခါ ခေါ်မလဲ ကိုယ်က မထိန်းချုပ်နိုင်ပါဘူး — development မှာ StrictMode က တမင် နှစ်ခါ ခေါ်ပါတယ်။ ဒါကြောင့် render လုပ်တိုင်း အပြင်က counter ကို တိုးတဲ့ component ဆိုရင် ကိန်းဂဏန်းတွေ မှားကုန်ပါတယ်။ Side effect တွေကို event handler တွေထဲမှာ ထားရမယ်ဆိုတာ နောက် lesson တွေမှာ လေ့လာရပါမယ်။

Component ကို ဘယ်လောက် ကြီးကြီး သေးသေး ခွဲရမလဲ ဆိုတဲ့ စံနှုန်း တစ်ခုက "component တစ်ခု၊ တာဝန် တစ်ခု" ပါ။ Component နာမည်ကို စကားလုံး နှစ်လုံး သုံးလုံးနဲ့ ရှင်းရှင်းလင်းလင်း ပေးလို့ မရရင် တာဝန် များနေတဲ့ လက္ခဏာပါ။ ဘာမှ မပြချင်တဲ့ အခြေအနေမှာ component က return null; ပြန်လို့ရပြီး parent ဘက်မှာ ဘာမှ ပြင်စရာ မလိုပါဘူး။

မှတ်ထားရမယ့် ပုံစံ: Lego tree
  1. UI ကို ထပ်ခါပေါ်တဲ့ အပိုင်းတွေ ခွဲ
  2. အပိုင်းတစ်ခုစီ PascalCase function
  3. Parent က <Child /> နဲ့ ပေါင်း
  4. React က tree တစ်ခုလုံးကို render

Component တစ်ခုက အလုပ်တစ်ခုပဲ လုပ်ပြီး ပြင်ချင်ရင် နေရာတစ်ခုတည်းမှာ ပြင်ရုံပါ။

အထွေထွေ ပုံစံ

jsx
// ChildName.jsx
export default function ChildName() {
  return <element>...</element>;
}

// ParentName.jsx
import ChildName from "./ChildName.jsx";

export default function ParentName() {
  return <wrapper><ChildName /><ChildName /></wrapper>;
}

Child component ကို သူ့ file ထဲမှာ default export လုပ်ပြီး parent က import လုပ်ကာ tag အဖြစ် လိုသလောက် သုံးပါတယ်။ Import path မှာ ./ နဲ့ စရပြီး Vite မှာ .jsx extension ကို ချန်ထားလည်း ရပါတယ်။

Example: WelcomeCard ကို နှစ်ခါ ပြန်သုံးခြင်း

ဒီ example မှာ component နှစ်ခု ရှိပါတယ် — card တစ်ခုကို ဖော်ပြတဲ့ WelcomeCard နဲ့ page ကို ပေါင်းစပ်တဲ့ App ပါ။ App ရဲ့ JSX ထဲမှာ WelcomeCard ကို တစ်ခါမှ ကိုယ်တိုင် ခေါ်တာ မဟုတ်ဘဲ tag အဖြစ်ပဲ ရေးထားတာကို သတိထားကြည့်ပါ။ <WelcomeCard /> line တစ်ကြောင်း ထပ်ထည့်ကြည့်ပါ — card ကိုယ်တိုင်ကို မထိဘဲ တတိယ card တစ်ခု ပေါ်လာတာ တွေ့ရပါမယ်။

jsx
function WelcomeCard() {
  return (
    <article className="card">
      <h2>React Course</h2>
      <p>Component နဲ့ UI ကိုအပိုင်းလိုက် တည်ဆောက်ကြမယ်။</p>
    </article>
  );
}

function App() {
  return (
    <main>
      <h1>ThutaTech Lessons</h1>
      <WelcomeCard />
      <WelcomeCard />
    </main>
  );
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 1function WelcomeCard() {

    PascalCase နာမည်နဲ့ ရိုးရိုး function ပါ။ Argument မလိုသေးတာက card တိုင်း စာသား အတူတူ ဖြစ်နေလို့ပါ — props lesson မှာ card တစ်ခုချင်း မတူအောင် လုပ်ပါမယ်။

  2. Line 3<article className="card">

    Component ရဲ့ root element ပါ။ className="card" က CSS class တစ်ခုတည်းနဲ့ card အားလုံးကို style ပေးလို့ ရစေပါတယ်။

  3. Line 10function App() {

    App က parent component ဖြစ်ပြီး tree ရဲ့ ထိပ်ဆုံးမှာ ရှိပါတယ်။ သူ့အလုပ်က အပိုင်းတွေကို စီစဉ်ပေးတာပဲ ဖြစ်ပြီး card ရဲ့ အသေးစိတ်ကို မသိပါဘူး။

  4. Lines 14-15<WelcomeCard />

    Self-closing tag နှစ်ခုက WelcomeCard instance နှစ်ခု ဖြစ်ပါတယ်။ React က function ကို နှစ်ခါ ခေါ်ပြီး <article> နှစ်ခု ထုတ်ပေးပါတယ်။

You should see
Browser မှာ heading တစ်ခုနဲ့ တူညီတဲ့ card နှစ်ခု ပေါ်လာမယ်။

Pure မဟုတ်တဲ့ component ဘာဖြစ်လဲ

အောက်က Cup component က render တိုင်း အပြင်က guests variable ကို တိုးပါတယ်။ Server ပေါ်မှာ တစ်ခါ render ရင် guest #1, #2, #3 ရပေမယ့် StrictMode ဖွင့်ထားတဲ့ development မှာ component တစ်ခုစီကို နှစ်ခါ ခေါ်လို့ #2, #4, #6 ဖြစ်သွားပါတယ်။ ရလဒ်က render ဘယ်နှခါ ဖြစ်လဲ ပေါ်မှာ မူတည်နေတာကိုက bug ပါ။ ပြင်နည်းကတော့ နံပါတ်ကို props အဖြစ် parent ကနေ ပို့တာပါ။

Cups.jsx
let guests = 0;

function Cup() {
  guests = guests + 1; // changes a variable outside the component: impure
  return <li>Tea for guest #{guests}</li>;
}

export default function TeaList() {
  return <ul><Cup /><Cup /><Cup /></ul>;
}

လက်တွေ့ project တွေမှာ

Real project တွေမှာ

Real project တွေမှာ src/components/ folder ထဲမှာ Button.jsx၊ Card.jsx၊ Modal.jsx လို ပြန်သုံးလို့ရတဲ့ component တွေ ထားပြီး src/pages/ ဒါမှမဟုတ် src/features/ ထဲမှာ အဲဒါတွေကို ပေါင်းထားတဲ့ page component တွေ ထားလေ့ရှိပါတယ်။ Folder ဖွဲ့စည်းပုံကို React က မသတ်မှတ်ပါဘူး၊ team တစ်ခုချင်း သဘောတူထားတဲ့ ပုံစံကို လိုက်ပါ။

Component ကို ဘယ်အချိန် ခွဲထုတ်ရမလဲ ဆိုတာ မေးခွန်းကောင်းပါ။ အဖြေ သုံးခု ရှိပါတယ် — markup က နေရာနှစ်ခုထက် များတဲ့နေရာမှာ ထပ်ပေါ်ရင်၊ component တစ်ခုက screen တစ်ခုနဲ့ မဆံ့အောင် ရှည်လာရင်၊ ဒါမှမဟုတ် အပိုင်းတစ်ခုမှာ ကိုယ်ပိုင် အမည်ပေးလို့ရတဲ့ တာဝန် ("search bar"၊ "price summary") ရှိရင် ခွဲထုတ်ပါ။ Design system ရှိတဲ့ company တွေမှာတော့ Storybook လို tool နဲ့ component တစ်ခုချင်းကို သီးခြားစီ ပြပြီး စမ်းကြပါတယ်။ သေးသေးနဲ့ တာဝန်တစ်ခုတည်း ရှိတဲ့ component တွေကို course ရဲ့ နောက်ပိုင်းမှာ automated test တွေနဲ့ စစ်ရတာလည်း အများကြီး ပိုလွယ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Component နာမည်ကို စာလုံးအသေးနဲ့ စတာ

function welcomeCard() ကို <welcomeCard /> နဲ့ သုံးရင် React က function ကို မခေါ်ဘဲ မသိတဲ့ HTML tag <welcomecard> တစ်ခု ထုတ်ပါတယ်၊ console မှာ "is using incorrect casing. Use PascalCase for React components" warning ပေါ်ပါတယ်။ နာမည်ကို WelcomeCard လို့ ပြောင်းပါ။

2. Component ထဲမှာ component တစ်ခု ထပ်ကြေညာတာ

function App() { function Card() {...} return <Card />; } လို ရေးရင် App render တိုင်း Card function အသစ် ဖြစ်လာလို့ React က component အမျိုးအစား အသစ်လို့ ယူပြီး Card ရဲ့ state အားလုံးကို ဖျက်ပစ်ပါတယ်၊ နှေးလည်း နှေးပါတယ်။ Component တိုင်းကို file ရဲ့ top level မှာ ကြေညာပါ။

3. return နောက်မှာ line ခွဲလိုက်တာ

return ကို တစ်ကြောင်းတည်း ရေးပြီး JSX ကို နောက်တစ်ကြောင်းမှာ ကွင်းမပါဘဲ စရင် JavaScript က return; လို့ ယူပြီး undefined ပြန်ပါတယ်။ Error မတက်ဘဲ component က ဘာမှ မပြလို့ ရှာရ ခက်ပါတယ်။ return ( ကို line တစ်ကြောင်းတည်းမှာ ထားပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • File နာမည်ကို component နာမည်နဲ့ တူအောင် ပေးပါ — CourseCard component ကို CourseCard.jsx ထဲမှာ ထားပါ။
  • Component ကို ဘာလုပ်လဲ (CourseCard) နဲ့ နာမည်ပေးပါ၊ ဘယ်လို ပုံပေါ်လဲ (BlueBox) နဲ့ မပေးပါနဲ့။
  • ပထမဆုံး component တစ်ခုထဲမှာ အကုန်ရေးပြီး ထပ်ခါပေါ်တာ ဒါမှမဟုတ် ရှည်လာတာ တွေ့မှ ခွဲထုတ်ပါ၊ ကြိုတင် ခွဲလွန်းရင်လည်း ဖတ်ရ ခက်ပါတယ်။
  • Render လုပ်နေစဉ် အပြင်က data ကို မပြင်ပါနဲ့၊ input တူရင် JSX တူရမယ်လို့ မှတ်ထားပါ။

Mini challenge

Mini challenge: page layout ကို component တွေ ခွဲပါ

SiteHeader၊ LessonItem၊ LessonList၊ SiteFooter ဆိုတဲ့ component လေးခု ရေးပြီး App ထဲမှာ fragment နဲ့ ပေါင်းပါ။

  • SiteHeader က site နာမည်နဲ့ <nav> ပါတဲ့ <header> ကို ပြရမယ်
  • LessonList က <h2> နဲ့ <ol> ထဲမှာ <LessonItem /> နှစ်ခု ပြရမယ်
  • SiteFooter က year variable ကို braces နဲ့ ပြရမယ်
  • App က wrapper div မသုံးဘဲ <>...</> နဲ့ အားလုံးကို ပေါင်းရမယ်
App.jsx
function SiteHeader() {
  // TODO: <header className="site-header"> with <strong> and <nav>
}

function LessonItem() {
  // TODO: one <li className="lesson">
}

function LessonList() {
  // TODO: <section> with <h2> and an <ol> of two LessonItems
}

function SiteFooter() {
  // TODO: const year = ...; render it inside <footer>
}

export default function App() {
  // TODO: fragment with SiteHeader, <main><LessonList /></main>, SiteFooter
}

ရလဒ်: Page ပေါ်မှာ "ThutaTech" နဲ့ "Courses | Blog | About" ပါတဲ့ header၊ "Today's lessons" heading အောက်မှာ "Components" item နှစ်ခုပါတဲ့ နံပါတ်တပ် list နဲ့ "Copyright 2026 ThutaTech" footer ပေါ်ပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

Component တိုင်း return ( ... ) ကို မမေ့ပါနဲ့။ LessonList ထဲမှာ LessonItem ကို သုံးတာက component tree ကို တစ်ဆင့် ပိုနက်စေပါတယ် — App → LessonList → LessonItem။

အဖြေ ကြည့်ရန်
App.jsx
function SiteHeader() {
  return (
    <header className="site-header">
      <strong>ThutaTech</strong>
      <nav>Courses | Blog | About</nav>
    </header>
  );
}

function LessonItem() {
  return <li className="lesson">Components</li>;
}

function LessonList() {
  return (
    <section>
      <h2>Today's lessons</h2>
      <ol>
        <LessonItem />
        <LessonItem />
      </ol>
    </section>
  );
}

function SiteFooter() {
  const year = 2026;
  return <footer>Copyright {year} ThutaTech</footer>;
}

export default function App() {
  return (
    <>
      <SiteHeader />
      <main>
        <LessonList />
      </main>
      <SiteFooter />
    </>
  );
}

Quick Quiz 1 / 3

function profileCard() { ... } ကို <profileCard /> နဲ့ သုံးရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

Badge.jsx က export function Badge() { ... } ကို named export လုပ်ထားတယ်။ ဘယ် import က မှန်လဲ။

Quick Quiz 3 / 3

Render လုပ်နေစဉ် component အပြင်က let total ကို total = total + 1 နဲ့ တိုးတဲ့ component ရဲ့ ပြဿနာက ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Component ဆိုတာ PascalCase နာမည်ရှိပြီး JSX ပြန်ပေးတဲ့ function ပါ၊ <Name /> နဲ့ သုံးပါတယ်။
  • Component တွေ တစ်ခုထဲ တစ်ခု ထည့်ပြီး tree ဖြစ်လာကာ ပုံစံကို နေရာတစ်ခုတည်းမှာ ပြင်ရုံပါ။
  • File တစ်ခုမှာ default export တစ်ခု၊ named export အများကြီး ထားနိုင်ပြီး named ကို braces နဲ့ import လုပ်ပါတယ်။
  • Component တွေ pure ဖြစ်ရမယ်၊ ပြီးတော့ component ထဲမှာ component ထပ်မကြေညာပါနဲ့။

React Documentation — React

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Components | Thuta Learning