ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
React ကို သိပြီးသားဆိုရင် button၊ card၊ form လို UI အပိုင်းအစလေးတွေ ဆောက်တတ်ပါပြီ။ ဒါပေမယ့် website တစ်ခုလုံး ဖြစ်ဖို့ဆိုရင် URL တစ်ခုချင်းစီကို ဘယ် page နဲ့ ချိတ်မလဲ၊ data ကို ဘယ်နေရာကနေ ယူမလဲ၊ production မှာ ဘယ်လို မြန်မြန် serve လုပ်မလဲ ဆိုတဲ့ ဆုံးဖြတ်ချက်တွေ အများကြီး ကျန်နေပါသေးတယ်။
Next.js က အဲဒီ ဆုံးဖြတ်ချက်တွေကို စနစ်တကျ စီစဉ်ပေးထားတဲ့ React framework တစ်ခုပါ။ ဒီ course မှာ snippet ပြတ်ပြတ်လေးတွေ စမ်းပြီး ပစ်ထားမှာ မဟုတ်ပါဘူး။ "Myanmar Notes" ဆိုတဲ့ မှတ်စု share လုပ်တဲ့ app တစ်ခုကို lesson တိုင်းမှာ နည်းနည်းချင်း ချဲ့သွားပြီး နောက်ဆုံးမှာ production ကို deploy လုပ်တဲ့အထိ ဆက်သွားမှာပါ။
Problem → Why → Solution
ပြဿနာ: file တစ်ခုချင်းရဲ့ တာဝန်ကို မသိဘဲ code ကူးတာ
Next.js စလေ့လာတဲ့သူ အများစုက React component ရေးရတာမှာ မခက်ပါဘူး။ ခက်တာက tutorial တစ်ခုက code ကို ကူးလိုက်တဲ့အခါ "useState only works in Client Components" လို error တက်လာတာ၊ ဒါမှမဟုတ် file ကို page.tsx မဟုတ်တဲ့ နာမည်နဲ့ သိမ်းလိုက်လို့ URL က 404 ပြနေတာမျိုးပါ။ Code ကိုယ်တိုင်က မှန်နေပေမယ့် ဘယ် file မှာ ထားရမလဲ၊ ဘယ်နေရာမှာ run မလဲ ဆိုတာ မသိလို့ ဖြစ်တာပါ။
နောက်ထပ် ထောင်ချောက်တစ်ခုက development server မှာ အလုပ်လုပ်ရင် production မှာလည်း အဆင်ပြေမယ်လို့ ယူဆတာပါ။ Production build က code ကို ပိုတင်းကျပ်စွာ စစ်ပြီး page တွေကို ကြိုတင် render လုပ်တာမို့ dev မှာ မမြင်ရတဲ့ error တွေ ပေါ်လာတတ်ပါတယ်။
ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Next.js မှာ file နာမည်နဲ့ folder နေရာကိုယ်တိုင်က အဓိပ္ပာယ် ရှိပါတယ်၊ component တစ်ခုချင်းကလည်း server ပေါ်မှာ run မလား browser ထဲမှာ run မလား ဆိုတာ ကွဲပြားပါတယ်။ ဒီ rule နှစ်ခုကို နားမလည်ဘဲ code ကူးရင် error message တွေက လျှို့ဝှက်ဆန်းကြယ်သလို ဖြစ်နေပါမယ်။ ဒါကြောင့် ဒီ course က file-based routing ကနေ စပြီး server နဲ့ browser ခွဲခြားပုံကို အစောကြီးကတည်းက သင်ပါတယ်။
Core concept: Next.js ရဲ့ အဓိက အယူအဆတွေနဲ့ course ရဲ့ ပုံစံ
ဒီ course က Next.js ရဲ့ App Router ကို သင်ပါတယ်။ App Router မှာ app folder အောက်က folder တစ်ခုစီက URL segment တစ်ခု ဖြစ်ပြီး page.tsx file က အဲဒီ URL မှာ ပြမယ့် UI ပါ။ ဥပမာ app/notes/page.tsx က /notes ဖြစ်လာပါတယ်။ Router config file သီးသန့် ရေးစရာ မလိုပါဘူး — folder structure ကိုယ်တိုင်က router ပါပဲ။
ဒုတိယ အယူအဆက Server Component နဲ့ Client Component ပါ။ app folder ထဲက component တွေဟာ default အနေနဲ့ Server Component ဖြစ်ပြီး server ပေါ်မှာ render လုပ်ပါတယ်၊ database ကို တိုက်ရိုက် ဖတ်လို့ ရပြီး သူတို့ရဲ့ code က browser ဆီ JavaScript အဖြစ် မရောက်ပါဘူး။ Click၊ state၊ browser API လိုအပ်တဲ့ အပိုင်းတွေကိုပဲ file ထိပ်မှာ "use client" ရေးပြီး Client Component အဖြစ် ခွဲထုတ်ပါတယ်။
| Chapter | ဘာတွေ သင်မလဲ |
|---|---|
| Basic (lesson 6 ခု) | Next.js ဆိုတာ ဘာလဲ၊ install လုပ်ပုံ၊ project structure၊ page နဲ့ layout၊ CSS၊ image နဲ့ font |
| Intermediate (lesson 10 ခု) | Link နဲ့ navigation၊ dynamic route၊ route group၊ loading/error UI၊ metadata၊ Server/Client Component၊ data fetching၊ streaming၊ cache၊ search params |
| Advanced (lesson 12 ခု) | Route Handler၊ Server Action နဲ့ form၊ database layer၊ auth၊ security၊ testing၊ performance၊ build နဲ့ deploy (Vercel နဲ့ Docker) |
| Projects (lesson 3 ခု) | Link Stash app ကို setup မှ deploy အထိ ဆောက် |
| Exercises (lesson 2 ခု) | Routing/layout နဲ့ data/action တွေကို လေ့ကျင့်ခန်းတွေနဲ့ ပြန်စစ် |
စတင်ဖို့ HTML၊ CSS၊ JavaScript နဲ့ React ရဲ့ component၊ props၊ state ကို သိထားဖို့ လိုပါတယ်။ Code တွေကို TypeScript (.tsx) နဲ့ ရေးထားပေမယ့် type annotation အခြေခံလောက် နားလည်ရင် လုံလောက်ပါတယ်။ Next.js က မကြာခဏ version အသစ် ထွက်တဲ့ framework ဖြစ်လို့ version တစ်ခုနဲ့ တစ်ခု အပြုအမူ ကွာတဲ့ နေရာတွေမှာ lesson ထဲမှာ ပြောထားပြီး official docs ကို ညွှန်ပြထားပါတယ်။
Next.js မှာ Pages Router ဆိုတဲ့ routing စနစ် အဟောင်းတစ်ခုလည်း ရှိပါသေးတယ် — pages/ folder ကို သုံးပြီး support ဆက်ပေးထားပေမယ့် project အသစ်တွေအတွက် docs က App Router ကို အကြံပြုပါတယ်။ ဒီ course ရဲ့ code အားလုံးက App Router ပါ။
- Browser က URL တစ်ခု တောင်းတယ်
- Next.js က app/ ထဲက page.tsx ကို ရှာတယ်
- Server Component တွေ server မှာ render ဖြစ်တယ်
- Client အပိုင်းတွေ browser မှာ interactive ဖြစ်တယ်
Lesson တိုင်းမှာ "ဒီ file က ဘယ် URL လဲ၊ ဒီ code က ဘယ်မှာ run လဲ" ဆိုတဲ့ မေးခွန်းနှစ်ခုကို ကိုယ့်ကိုယ်ကိုယ် မေးကြည့်ပါ။
အထွေထွေ ပုံစံ: route တစ်ခုရဲ့ page file
// The folder path under app/ decides the URL
export default function PageName() {
const data = "values to show";
return (
<main>
<h1>Heading for this URL</h1>
<p>{data}</p>
</main>
);
}Page file တစ်ခုက React component တစ်ခုကို default export လုပ်ရပါတယ်။ File ရဲ့ နေရာ (app/ အောက်က folder path) က URL ကို ဆုံးဖြတ်ပြီး component ရဲ့ JSX က အဲဒီ URL မှာ ပေါ်မယ့် UI ပါ။ "use client" မပါရင် Server Component ဖြစ်ပါတယ်။
Example: course map ကို ပြတဲ့ home page
ဒီ file က Next.js project တစ်ခုရဲ့ app/page.tsx ပါ၊ ဒါကြောင့် site ရဲ့ "/" URL မှာ ပေါ်ပါတယ်။ Chapter စာရင်းကို data အဖြစ် ကိုင်ထားပြီး စုစုပေါင်း lesson အရေအတွက်ကို တွက်ကာ heading၊ အနှစ်ချုပ် စာကြောင်းနဲ့ နံပါတ်တပ် list တစ်ခု ပြပါတယ်။ Installation lesson ပြီးရင် ကိုယ့် project ထဲ ထည့် run ကြည့်နိုင်ပါတယ်၊ အခုတော့ ဖတ်ပြီး ဘာပေါ်မလဲ ခန့်မှန်းကြည့်ပါ။
const chapters = [
{ name: "Basic", lessons: 6 },
{ name: "Intermediate", lessons: 10 },
{ name: "Advanced", lessons: 12 },
{ name: "Projects", lessons: 3 },
{ name: "Exercises", lessons: 2 },
];
export default function HomePage() {
const total = chapters.reduce((sum, chapter) => sum + chapter.lessons, 0);
return (
<main>
<h1>Learning Next.js with Myanmar Notes</h1>
<p>
{chapters.length} chapters, {total} lessons.
</p>
<ol>
{chapters.map((chapter) => (
<li key={chapter.name}>
{chapter.name}: {chapter.lessons} lessons
</li>
))}
</ol>
</main>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Data
const chapters = [Chapter စာရင်းကို component အပြင်မှာ ရိုးရိုး array အဖြစ် ထားပါတယ်။ နောက် lesson တွေမှာ ဒီလို data ကို database ဒါမှမဟုတ် API ကနေ server ပေါ်မှာ ယူပါမယ်။
- Default export
export default function HomePage() {page.tsx က component တစ်ခုကို default export လုပ်ရပါတယ်၊ Next.js က အဲဒါကို "/" အတွက် render လုပ်ပါတယ်။ "use client" မပါလို့ ဒါက Server Component ပါ။
- Total
const total = chapters.reduce((sum, chapter) => sum + chapter.lessons, 0);6 + 10 + 12 + 3 + 2 = 33 ဖြစ်လို့ total က 33 ပါ။ Server ပေါ်မှာ တွက်ပြီးသားမို့ browser က ရလဒ် HTML ကိုပဲ ရပါတယ်။
- Summary line
{chapters.length} chapters, {total} lessons.{ } ထဲက expression တွေ အလုပ်လုပ်ပြီး "5 chapters, 33 lessons." ဖြစ်လာပါတယ်။
- List
<li key={chapter.name}>chapters.map() က chapter တစ်ခုစီအတွက် <li> တစ်ခု ထုတ်ပြီး key က React ကို item တစ်ခုချင်းကို ခွဲသိစေပါတယ်။ <ol> ဖြစ်လို့ browser က 1 ကနေ 5 အထိ နံပါတ်တပ်ပေးပါတယ်။
"/" ကို ဖွင့်ရင် "Learning Next.js with Myanmar Notes" heading၊ "5 chapters, 33 lessons." ဆိုတဲ့ စာကြောင်း၊ ပြီးရင် 1. Basic: 6 lessons, 2. Intermediate: 10 lessons, 3. Advanced: 12 lessons, 4. Projects: 3 lessons, 5. Exercises: 2 lessons ဆိုတဲ့ နံပါတ်တပ် list ပေါ်လာပါမယ်။Lesson တစ်ခုကို ဘယ်လို လေ့လာမလဲ
1. ပြဿနာကို အရင်ဖတ်ပါ
Feature တစ်ခုက ဘာနာကျင်မှုကို ဖြေရှင်းလဲ သိရင် syntax ကို မှတ်ရ ပိုလွယ်ပါတယ်။
2. Code ကို ကိုယ်တိုင် ရိုက်ပါ
Copy-paste မလုပ်ဘဲ ရိုက်ပြီး file ကို ဘယ် folder မှာ ထားလဲ သတိထားပါ။ Next.js မှာ file နေရာက code လောက် အရေးကြီးပါတယ်။
3. Error ကို ဖတ်ပါ
Error overlay က file နာမည်နဲ့ line ကို ပြပါတယ်။ Message ကို ပိတ်မပစ်ဘဲ ဘယ် file၊ ဘယ် rule ကို ပြောနေလဲ ရှာပါ။
4. Challenge ကို solution မကြည့်ခင် လုပ်ပါ
Starter code မှာ TODO တွေပဲ ပါပါတယ်။ ကိုယ်တိုင် ဖြည့်ကြည့်မှ ကိုယ် ဘယ်နေရာ နားမလည်သေးလဲ သိရပါမယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ Next.js
Next.js ကို marketing site၊ blog၊ documentation site၊ e-commerce store နဲ့ login လိုတဲ့ dashboard app တွေအထိ သုံးကြပါတယ်။ အကြောင်းရင်းက page တစ်ခုကို static အဖြစ် build time မှာ ကြိုထုတ်မလား၊ request တိုင်း server မှာ render မလား ဆိုတာ route တစ်ခုချင်းအလိုက် ရွေးလို့ ရလို့ပါ။ ဒါကြောင့် blog post တွေက static နဲ့ မြန်မြန်ပေါ်ပြီး user dashboard ကတော့ အချိန်နဲ့တပြေးညီ data ကို ပြနိုင်ပါတယ်။
Team တစ်ခုထဲမှာ Next.js ရဲ့ convention တွေက အထောက်အကူ အများကြီး ပြုပါတယ်။ Developer အသစ်တစ်ယောက်က app folder ကို ကြည့်လိုက်တာနဲ့ site မှာ ဘယ် URL တွေ ရှိလဲ ချက်ချင်း သိပါတယ်။ ဒီ course ရဲ့ Myanmar Notes app ကလည်း အဲဒီ ပုံစံအတိုင်း ကြီးထွားလာမှာပါ — အစမှာ static page နှစ်ခု၊ နောက်ပိုင်း database၊ login၊ form နဲ့ production deploy အထိပါ။ Projects chapter မှာတော့ Link Stash ဆိုတဲ့ app သေးသေးလေးကို ကိုယ်တိုင် အစကနေ ပြန်ဆောက်ပြီး သင်ခဲ့တာတွေကို စမ်းသပ်ပါမယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. File ရဲ့ တာဝန်ကို မစဉ်းစားဘဲ code ကူးတာ
Tutorial တစ်ခုက snippet ကို ကိုယ့် project ထဲ ထည့်တဲ့အခါ အဲဒါက page လား၊ layout လား၊ component သာမန်လား၊ server မှာ run ရမှာလား ဆိုတာ အရင် ဆုံးဖြတ်ပါ။ ဒါကို မစဉ်းစားရင် file မှားနေရာ ရောက်ပြီး 404 ဒါမှမဟုတ် Client Component error ရပါတယ်။
2. Development မှာ အလုပ်လုပ်ရင် production အဆင်သင့်လို့ ယူဆတာ
pnpm dev က code ပြင်တိုင်း မြန်မြန် ပြန်ပြဖို့ ဒီဇိုင်းထုတ်ထားတာပါ။ pnpm build က type error တွေ၊ prerender လုပ်ရင်း ပေါ်တဲ့ error တွေကို ဖမ်းပါတယ်။ Feature တစ်ခု ပြီးတိုင်း build ကို run ကြည့်တဲ့ အကျင့် ထားပါ။
3. Tutorial အဟောင်းနဲ့ App Router ကို ရောတာ
Online မှာ pages/ folder နဲ့ getServerSideProps သုံးတဲ့ tutorial အဟောင်းတွေ အများကြီး ရှိပါတယ်။ အဲဒီ code တွေ app/ folder ထဲမှာ အလုပ်မလုပ်ပါဘူး။ Docs ဖတ်တဲ့အခါ App Router အပိုင်းကို ရွေးထားလား စစ်ပါ။
Mini challenge
Mini challenge: ကိုယ့် study plan page
ဒီ challenge က React အခြေခံ အဆင်သင့် ဖြစ်မဖြစ် စစ်ဆေးမှု တစ်ခုလည်း ဖြစ်ပါတယ်။ Chapter တစ်ခုစီမှာ ပြီးသွားတဲ့ lesson အရေအတွက်ကို ပြပြီး စုစုပေါင်း progress ရာခိုင်နှုန်းကို တွက်တဲ့ page တစ်ခု ရေးပါ။
- Progress data ကို { chapter, done, total } object array အဖြစ် type ပေးပါ
- Chapter တစ်ခုစီကို "Basic: 2/6" ပုံစံ <li> တစ်ခုစီနဲ့ ပြပါ
- Chapter တစ်ခု ပြီးသွားရင် (done === total) နောက်မှာ " (complete)" ထည့်ပါ
- စုစုပေါင်း progress ကို Math.round() နဲ့ ရာခိုင်နှုန်း အပြည့်ကိန်းအဖြစ် <p> မှာ ပြပါ
type ChapterProgress = {
// TODO: chapter name, lessons done, lessons in total
};
const progress: ChapterProgress[] = [
// TODO: add at least three chapters, one of them complete
];
export default function StudyPlanPage() {
// TODO: add up done and total across all chapters, then work out the percent
return (
<main>
<h1>My study plan</h1>
{/* TODO: "Overall: N%" paragraph */}
{/* TODO: one <li> per chapter, with " (complete)" when finished */}
</main>
);
}ရလဒ်: Solution ထဲက data နဲ့ဆိုရင် "Overall: 42%" (8/19) ဆိုတဲ့ စာကြောင်းနဲ့ "Basic: 6/6 (complete)", "Intermediate: 2/10", "Projects: 0/3" ဆိုတဲ့ list ပေါ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
done နဲ့ total နှစ်ခုကို reduce() တစ်ခုစီနဲ့ ပေါင်းပါ။ Percent က Math.round((done / total) * 100) ပါ။ Condition တစ်ခုအပေါ် မူတည်ပြီး စာသား ထည့်ဖို့ {isDone && " (complete)"} ကို သုံးနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
type ChapterProgress = {
chapter: string;
done: number;
total: number;
};
const progress: ChapterProgress[] = [
{ chapter: "Basic", done: 6, total: 6 },
{ chapter: "Intermediate", done: 2, total: 10 },
{ chapter: "Projects", done: 0, total: 3 },
];
export default function StudyPlanPage() {
const done = progress.reduce((sum, item) => sum + item.done, 0);
const total = progress.reduce((sum, item) => sum + item.total, 0);
const percent = Math.round((done / total) * 100);
return (
<main>
<h1>My study plan</h1>
<p>Overall: {percent}%</p>
<ul>
{progress.map((item) => {
const isDone = item.done === item.total;
return (
<li key={item.chapter}>
{item.chapter}: {item.done}/{item.total}
{isDone && " (complete)"}
</li>
);
})}
</ul>
</main>
);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Next.js က routing၊ rendering၊ data နဲ့ production build ကို စနစ်တကျ စီစဉ်ပေးတဲ့ React framework ပါ။
- App Router မှာ app/ အောက်က folder တွေက URL segment ဖြစ်ပြီး page.tsx က အဲဒီ URL ရဲ့ UI ပါ။
- Component တွေက default အနေနဲ့ Server Component ဖြစ်ပြီး interactive အပိုင်းတွေကိုပဲ "use client" နဲ့ ခွဲထုတ်ပါတယ်။
- Course က Basic၊ Intermediate၊ Advanced၊ Projects၊ Exercises ဆိုပြီး lesson 33 ခု ရှိပြီး Myanmar Notes app ကို deploy အထိ ဆောက်သွားပါမယ်။
Next.js — Getting Started — Next.js
Next.js — Getting Started — Next.js