Thuta Learning
Next.js
BasicWeb Developmentအလယ်အလတ်

Pages နှင့် Shared Layouts

Next.jsသင်ခန်းစာ 525 မိနစ်

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

  • page.tsx နဲ့ route အသစ်တစ်ခု ဖန်တီးနိုင်ဖို့
  • Root layout ရဲ့ တာဝန်နဲ့ html/body tag လိုအပ်ချက်ကို ရှင်းပြနိုင်ဖို့
  • Section တစ်ခုအတွက် nested layout ရေးပြီး URL တစ်ခုရဲ့ component tree ကို ဆွဲပြနိုင်ဖို့
  • Navigation အတွင်း layout ရဲ့ state ဘာဖြစ်လဲ ခန့်မှန်းနိုင်ဖို့

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

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

Page တိုင်းမှာ header တူတူကို ကူးရေးနေရရင် header ပြင်ချင်တဲ့အခါ file တွေ အများကြီး လိုက်ပြင်ရပြီး တစ်ခုခု ကျန်ခဲ့ဖို့ လွယ်ပါတယ်။ Layout က အဲဒီလို ထပ်နေတဲ့ UI ကို နေရာတစ်ခုတည်းမှာ ထားပေးပြီး လက်ရှိ page ကို children prop ကနေ အလယ်မှာ ထည့်ပေးပါတယ်။

ဓာတ်ပုံဘောင်လို့ မြင်ကြည့်ပါ — ဘောင် (layout) က တစ်ခုတည်း ကျန်နေပြီး အထဲက ဓာတ်ပုံ (page) ကိုပဲ လဲလှယ်ပါတယ်။ /notes ကနေ /notes/42 ကို သွားတဲ့အခါ ဘောင်ကို ပြန်မဆောက်ဘဲ အထဲက page ကိုပဲ ပြောင်းလို့ navigation မြန်ပြီး ဘောင်ထဲက state တွေလည်း မပျောက်ပါဘူး။

Problem → Why → Solution

ပြဿနာ: page တိုင်းမှာ header ကူးရေးတာနဲ့ html/body မေ့တာ

Myanmar Notes ရဲ့ ပထမ version မှာ page သုံးခုလုံးက <header>Myanmar Notes</header> ကို ကိုယ်တိုင် render လုပ်ထားပါတယ်။ Logo ထည့်ဖို့ ဆုံးဖြတ်တဲ့အခါ file သုံးခု ပြင်ရပြီး about page ကို မေ့ကျန်ခဲ့ပါတယ်။ ပြီးတော့ root layout ကို ရေးတဲ့အခါ html နဲ့ body tag မပါဘဲ <div> တစ်ခုတည်း return ပြန်လိုက်လို့ Next.js က error ပြပါတယ်။

app/layout.tsx + pages
// app/layout.tsx: missing <html> and <body>
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return <div>{children}</div>;
}

// app/page.tsx, app/notes/page.tsx, app/about/page.tsx all repeat this:
export default function NotesPage() {
  return (
    <>
      <header>Myanmar Notes</header>  {/* copy 2 of 3: easy to forget one */}
      <h1>All notes</h1>
    </>
  );
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

App Router မှာ Next.js က html နဲ့ body tag ကို ကိုယ်တိုင် မထည့်ပေးပါဘူး၊ root layout က document ရဲ့ အပြင်ဆုံး အပိုင်း ဖြစ်ရပါတယ်။ Header ကိုလည်း page တစ်ခုချင်းမှာ ကူးရေးရင် "ဒီ UI ကို ဘယ်သူပိုင်လဲ" ဆိုတဲ့ မေးခွန်းရဲ့ အဖြေ မရှိတော့ပါဘူး။ Layout ထဲ ရွှေ့လိုက်ရင် နေရာတစ်ခုတည်းမှာ ပိုင်ပြီး page အားလုံး အလိုအလျောက် ရပါတယ်။

Core concept: page က leaf၊ layout က wrapper

page.tsx က route segment တစ်ခုကို public URL ဖြစ်စေပြီး React component တစ်ခုကို default export လုပ်ပါတယ်။ ဒါက tree ရဲ့ အဖျား (leaf) ပါ — URL တစ်ခုမှာ page တစ်ခုပဲ ပေါ်ပါတယ်။ layout.tsx ကလည်း component တစ်ခုကို default export လုပ်ပေမယ့် children prop ကို လက်ခံပြီး အဲဒီ folder နဲ့ အောက်က route အားလုံးကို wrap လုပ်ပါတယ်။ children နေရာမှာ အောက်အဆင့်က layout ဒါမှမဟုတ် page ဝင်လာပါတယ်။ page.tsx မပါတဲ့ folder မှာလည်း layout ထားလို့ ရပါတယ် — အဲဒီ folder ကိုယ်တိုင် URL မရှိပေမယ့် သူ့ layout က အထဲက route တွေကို wrap လုပ်ပါတယ်။

app/layout.tsx က root layout ဖြစ်ပြီး မဖြစ်မနေ ရှိရပါတယ်။ Document တစ်ခုလုံးရဲ့ <html> နဲ့ <body> ကို ဒီမှာပဲ ရေးရပါတယ်၊ lang attribute ကိုလည်း ဒီမှာ ထည့်ပါတယ်။ <title> လို <head> အချက်အလက်တွေကိုတော့ tag အနေနဲ့ ကိုယ်တိုင် မရေးဘဲ metadata API နဲ့ သတ်မှတ်ပါတယ် — metadata lesson မှာ လေ့လာပါမယ်။ Root layout က page အားလုံးမှာ ပါတဲ့ header၊ footer၊ font နဲ့ global CSS အတွက် သင့်တော်တဲ့ နေရာပါ။

Folder တစ်ခုထဲမှာ layout.tsx ထပ်ထည့်ရင် nested layout ရပါတယ်။ /notes/42 ကို ဖွင့်ရင် Next.js က root layout ထဲမှာ notes layout ကို ထည့်ပြီး အဲဒီထဲမှာ note page ကို ထည့်ပါတယ် — folder ပုံစံအတိုင်း ထပ်ဆင့်ပါတယ်။

အရေးကြီးဆုံး အပြုအမူက layout တွေ navigation အတွင်း ပြန် render မဖြစ်တာပါ။ Layout တူတူ မျှသုံးတဲ့ page နှစ်ခုကြား သွားတဲ့အခါ Next.js က ပြောင်းသွားတဲ့ segment ကိုပဲ render လုပ်ပြီး layout ကို ထိန်းထားလို့ layout ထဲက Client Component ရဲ့ state (ဥပမာ search box ထဲက စာ) မပျောက်ပါဘူး။ ဒါကြောင့် layout က URL ကို ဘယ် page ဖြစ်နေလဲ မှီခိုတဲ့ data နဲ့ searchParams ကို မရပါဘူး — route တစ်ခုတည်းနဲ့ ဆိုင်တဲ့ data ကို page မှာ ယူပါ။

text
Component tree for /notes/42
----------------------------
app/layout.tsx              RootLayout   <html><body><header>
  app/notes/layout.tsx      NotesLayout  <aside> stays mounted
    app/notes/[id]/page.tsx NotePage     swapped on navigation
မှတ်ထားရမယ့် ပုံစံ: ဘောင်ထဲက ဘောင်
  1. Root layout က html/body ကို ပိုင်တယ်
  2. Folder layout တွေ ထပ်ဆင့် wrap တယ်
  3. Page က children အဖြစ် ဝင်တယ်
  4. Navigation မှာ page ပဲ လဲတယ်

UI တစ်ခုကို page အများကြီးမှာ ပြချင်ရင် အဲဒီ page တွေ အားလုံး အောက်မှာ ရှိတဲ့ အနိမ့်ဆုံး folder ရဲ့ layout ထဲ ထည့်ပါ။

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

tsx
// app/<segment>/layout.tsx
export default function SegmentLayout({ children }: { children: React.ReactNode }) {
  return <div>{/* shared UI */}{children}</div>;
}

// app/<segment>/page.tsx
export default function SegmentPage() {
  return <h1>{/* this URL's content */}</h1>;
}

Layout က children: React.ReactNode ကို လက်ခံပြီး shared UI ထဲမှာ ထည့်ပါတယ်။ Page ကတော့ prop မလိုဘဲ URL ရဲ့ content ကို return ပြန်ပါတယ်။ Root layout (app/layout.tsx) မှာတော့ <div> အစား <html> နဲ့ <body> ကို return ပြန်ရပါမယ်။

Example: root layout၊ notes layout နဲ့ notes page

ဒီ block မှာ file သုံးခု ပါပါတယ်၊ comment တစ်ခုစီက file ရဲ့ path ပါ။ Root layout က site header ကို page အားလုံးနဲ့ မျှပြီး notes layout က /notes အောက်က page တွေအတွက်ပဲ sidebar ထည့်ပါတယ်။ /notes ကို ဖွင့်ရင် component သုံးခု ဘယ်လို ထပ်ဆင့်မလဲ မ run ခင် ခန့်မှန်းကြည့်ပါ။

React.ReactNode ကို import မလုပ်ဘဲ သုံးထားတာ သတိထားပါ။ Next.js project ရဲ့ TypeScript setup က React type namespace ကို ရနိုင်အောင် ပြင်ဆင်ထားလို့ create-next-app ရဲ့ template ကိုယ်တိုင်လည်း ဒီပုံစံ သုံးပါတယ်၊ import type { ReactNode } from "react" လို့ ရေးလည်း အတူတူပါပဲ။

app/layout.tsx + app/notes/layout.tsx + app/notes/page.tsx
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="my">
      <body>
        <header>Myanmar Notes</header>
        {children}
      </body>
    </html>
  );
}

// app/notes/layout.tsx
export default function NotesLayout({ children }: { children: React.ReactNode }) {
  return (
    <div className="notes-shell">
      <aside>Your notes</aside>
      <main>{children}</main>
    </div>
  );
}

// app/notes/page.tsx
export default function NotesPage() {
  return <h1>All notes</h1>;
}

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

  1. Root layout<html lang="my">

    Root layout က document ရဲ့ <html> နဲ့ <body> ကို ပိုင်ပါတယ်။ lang="my" က Myanmar ဘာသာ site ဖြစ်ကြောင်း browser နဲ့ screen reader ကို ပြောပါတယ်။

  2. Shared header<header>Myanmar Notes</header>

    Header ကို ဒီနေရာ တစ်ခုတည်းမှာ ရေးလို့ page အားလုံးမှာ ပေါ်ပါတယ်။ Logo ထည့်ချင်ရင် ဒီ line ကိုပဲ ပြင်ရပါတယ်။

  3. children{children}

    /notes ဆိုရင် children က NotesLayout (အထဲမှာ NotesPage ပါ) ဖြစ်ပြီး / ဆိုရင် app/page.tsx ဖြစ်ပါတယ်။ Layout က ဘာဝင်လာမလဲ မသိဘဲ နေရာပဲ ပေးထားတာပါ။

  4. Nested layout<aside>Your notes</aside>

    app/notes/layout.tsx ထဲမှာ ရှိလို့ /notes နဲ့ /notes/42 လို အောက်က route တွေမှာပဲ sidebar ပေါ်ပါတယ်။ Note တစ်ခုကနေ တစ်ခု သွားတဲ့အခါ ဒီ aside က ပြန်မဆောက်ပါဘူး။

  5. Pagereturn <h1>All notes</h1>;

    Page က သူ့ URL ရဲ့ content ကိုပဲ ဂရုစိုက်ပါတယ်၊ header နဲ့ sidebar ကို မသိပါဘူး။

You should see
/notes ကို ဖွင့်ရင် ထိပ်ဆုံးမှာ "Myanmar Notes" header၊ အောက်မှာ "Your notes" sidebar နဲ့ main ထဲမှာ "All notes" heading ပေါ်ပါမယ်။ / ကို ဖွင့်ရင်တော့ header ပဲ မျှသုံးပြီး sidebar မပါပါဘူး။

Layout နဲ့ template ကွာခြားချက်

Layout က navigation အတွင်း mount ဖြစ်ပြီးသား ကျန်နေတာက များသောအားဖြင့် လိုချင်တဲ့ အပြုအမူပါ။ ဒါပေမယ့် page ပြောင်းတိုင်း animation ပြန်စချင်တာ၊ form state ကို reset လုပ်ချင်တာမျိုး ရှိရင် layout.tsx အစား template.tsx ကို သုံးနိုင်ပါတယ်။ Template ကလည်း children ကို wrap လုပ်ပေမယ့် navigation တိုင်း အသစ် mount ဖြစ်ပါတယ်။

FileNavigation ပြီးနောက်
layout.tsxကျန်နေ — state ထိန်းထား
template.tsxအသစ် mount — state reset
page.tsxURL ပြောင်းရင် လဲလှယ်

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

Real project တွေမှာ layout

Dashboard app တွေမှာ /dashboard အောက်က page အားလုံးက sidebar navigation နဲ့ user menu ကို မျှသုံးကြပါတယ်။ app/dashboard/layout.tsx ထဲမှာ အဲဒါတွေကို ထားလိုက်ရင် page တစ်ခုကနေ တစ်ခု သွားတဲ့အခါ sidebar က လှုပ်ရှားခြင်းမရှိဘဲ ကျန်နေပြီး scroll position နဲ့ ဖွင့်ထားတဲ့ menu တွေ မပျောက်ပါဘူး။ Music app တွေက audio player ကို layout ထဲ ထားလို့ page ပြောင်းနေချိန်မှာ သီချင်း ဆက်ဖွင့်နေပါတယ်။

Marketing site တွေမှာတော့ landing page တွေအတွက် layout တစ်မျိုး၊ docs section အတွက် sidebar ပါတဲ့ layout တစ်မျိုး ခွဲထားလေ့ ရှိပါတယ်။ URL ကို မပြောင်းဘဲ layout ကို ခွဲဖို့ route group ဆိုတဲ့ feature ကို နောက် lesson တစ်ခုမှာ လေ့လာပါမယ်။ ဘယ်လိုပဲဖြစ်ဖြစ် rule က တူတူပါ — UI ကို မျှသုံးတဲ့ page တွေ အားလုံးကို ဖုံးတဲ့ အနိမ့်ဆုံး folder မှာ layout ကို ထားပါ၊ အဲဒီထက် မမြင့်ပါစေနဲ့၊ ဒါမှ မသက်ဆိုင်တဲ့ page တွေက မရပါဘူး။

အမှားများ

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

1. Root layout မှာ html နဲ့ body မပါတာ

Root layout က <div> ဒါမှမဟုတ် fragment ကို return ပြန်ရင် Next.js က root layout မှာ လိုအပ်တဲ့ tag တွေ ပျောက်နေကြောင်း error ပြပါတယ်။ app/layout.tsx က အမြဲ <html> ကို return ပြန်ပြီး အထဲမှာ <body> ပါရပါမယ်။ Nested layout တွေမှာတော့ html/body ထပ်မထည့်ပါနဲ့။

2. Route တစ်ခုတည်းရဲ့ data ကို layout ထဲ ပြုံထည့်တာ

Note တစ်ခုရဲ့ ခေါင်းစဉ်ကို notes layout ထဲမှာ ယူပြီး ပြချင်ရင် layout က navigation အတွင်း ပြန် render မဖြစ်လို့ note ပြောင်းသွားတဲ့အခါ ခေါင်းစဉ်ဟောင်း ကျန်နေနိုင်ပါတယ်။ Layout က searchParams ကိုလည်း မရပါဘူး။ URL တစ်ခုချင်းအလိုက် ပြောင်းတဲ့ data ကို page မှာ ယူပါ။

3. Page file ထဲမှာ default export မေ့တာ

export function NotesPage() လို named export ပဲ ရေးထားရင် Next.js က page component ကို ရှာမတွေ့လို့ error ပြပါတယ်။ page.tsx နဲ့ layout.tsx နှစ်ခုလုံးမှာ export default ကို သုံးရပါမယ်။

Best Practices

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

  • Root layout ကို ပါးပါးလေး ထားပါ — html/body၊ global CSS၊ font နဲ့ page တိုင်း တကယ်လိုတဲ့ header/footer လောက်ပဲ ထည့်ပါ။
  • Section တစ်ခုရဲ့ UI (sidebar၊ tab) ကို အဲဒီ section folder ရဲ့ layout မှာ ထားပြီး root layout ထဲ မတင်ပါနဲ့။
  • Layout တွေကို Server Component အဖြစ် ထားပြီး interactive ဖြစ်ရမယ့် အပိုင်း (search box၊ dropdown) ကိုပဲ Client Component အဖြစ် ခွဲပါ။
  • Page component နာမည်ကို NotesPage၊ ProfilePage လို ရှင်းရှင်း ပေးပါ — error stack နဲ့ React DevTools မှာ ဖတ်ရ လွယ်ပါတယ်။

Mini challenge

Mini challenge: settings section အတွက် layout

Myanmar Notes မှာ /settings နဲ့ /settings/profile ဆိုတဲ့ page နှစ်ခု ထည့်ပါ။ နှစ်ခုလုံးက "Settings" heading နဲ့ section navigation ကို မျှသုံးရပါမယ်၊ ဒါပေမယ့် / နဲ့ /notes မှာ အဲဒါ မပေါ်ရပါဘူး။

  • app/settings/layout.tsx က children ကို လက်ခံပြီး <h1>Settings</h1> နဲ့ link နှစ်ခုပါတဲ့ <nav> ကို ပြရမယ်
  • app/settings/page.tsx က "General" heading ပြရမယ်
  • app/settings/profile/page.tsx က "Profile" heading နဲ့ စာကြောင်းတစ်ကြောင်း ပြရမယ်
  • Layout ထဲမှာ html/body မထည့်ရ — root layout က ပိုင်ပြီးသားပါ
app/settings/...
// app/settings/layout.tsx
export default function SettingsLayout(/* TODO: children prop with its type */) {
  // TODO: <section> with <h1>Settings</h1>, a <nav> linking to
  // /settings and /settings/profile, then the children
}

// app/settings/page.tsx
// TODO: default-export a page with an <h2>General</h2>

// app/settings/profile/page.tsx
// TODO: default-export a page with an <h2>Profile</h2> and a <p>

ရလဒ်: /settings မှာ Settings heading၊ General နဲ့ Profile link နှစ်ခု၊ ပြီးရင် "General" ပေါ်ပါမယ်။ /settings/profile မှာ heading နဲ့ nav တူတူ ကျန်ပြီး အောက်မှာ "Profile" ပြောင်းပေါ်ပါမယ်။

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

Layout ရဲ့ type ကို { children: React.ReactNode } လို့ ရေးပါ။ Link component ကို နောက် lesson မှာ သင်မှာမို့ အခု <a href> ကို သုံးလို့ ရပါတယ်။ Page တွေက layout ထဲမှာ ရှိပြီးသား <h1> အောက်မှာ ပေါ်လို့ <h2> ကို သုံးပါ။

အဖြေ ကြည့်ရန်
app/settings/...
// app/settings/layout.tsx
export default function SettingsLayout({ children }: { children: React.ReactNode }) {
  return (
    <section>
      <h1>Settings</h1>
      <nav>
        <a href="/settings">General</a> | <a href="/settings/profile">Profile</a>
      </nav>
      {children}
    </section>
  );
}

// app/settings/page.tsx
export default function SettingsPage() {
  return <h2>General</h2>;
}

// app/settings/profile/page.tsx
export default function ProfilePage() {
  return (
    <>
      <h2>Profile</h2>
      <p>Your name appears on every note you share.</p>
    </>
  );
}

Quick Quiz 1 / 3

app/layout.tsx၊ app/notes/layout.tsx နဲ့ app/notes/[id]/page.tsx ရှိတယ်။ /notes/42 ကို ဖွင့်ရင် ထပ်ဆင့်ပုံ ဘယ်လိုလဲ။

Quick Quiz 2 / 3

Root layout က return <main>{children}</main>; ပဲ ပြန်ရင် ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

Notes layout ထဲက Client Component search box မှာ "react" လို့ ရိုက်ထားပြီး /notes ကနေ /notes/42 ကို Link နဲ့ သွားတယ်။ Search box ထဲက စာ ဘာဖြစ်မလဲ။

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

  • page.tsx က route ကို public ဖြစ်စေတဲ့ leaf ဖြစ်ပြီး layout.tsx က children ကို wrap လုပ်တဲ့ shared UI ပါ။
  • app/layout.tsx root layout က မဖြစ်မနေ လိုအပ်ပြီး <html> နဲ့ <body> ကို ပိုင်ပါတယ်။
  • Layout တွေက folder အဆင့်အတိုင်း ထပ်ဆင့်ပြီး navigation အတွင်း ပြန် render မဖြစ်လို့ state ကို ထိန်းထားပါတယ်။
  • URL အလိုက် ပြောင်းတဲ့ data ကို page မှာ ယူပြီး reset လိုရင် template.tsx ကို သုံးပါ။

Next.js — Layouts and Pages — Next.js

Next.js — Layouts and Pages — Next.js

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

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

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

Pages နှင့် Shared Layouts | Thuta Learning