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

Server Component နှင့် Client Component

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

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

  • Component တစ်ခုက Server Component ဖြစ်သင့်လား Client Component ဖြစ်သင့်လား ဆုံးဖြတ်နိုင်ဖို့
  • "use client" boundary ကြောင့် import လုပ်ထားတဲ့ module တွေ client bundle ထဲ ရောက်ပုံကို ရှင်းပြနိုင်ဖို့
  • Server ကနေ Client Component ဆီ serializable props နဲ့ data ပို့နိုင်ဖို့
  • Secret တွေနဲ့ database code ကို client ထဲ မရောက်အောင် ကာကွယ်နိုင်ဖို့

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

Server နဲ့ Client Component ကို ရိုးရိုးလေး ပြောရရင်

စားသောက်ဆိုင်မှာ မီးဖိုချောင်နဲ့ စားပွဲ ရှိပါတယ်။ ဟင်းချက်တာ၊ ကုန်ကြမ်းသိုလှောင်ခန်းကို ဖွင့်တာက မီးဖိုချောင်ထဲမှာပဲ ဖြစ်ပြီး ဧည့်သည်က အဲဒီထဲ မမြင်ရပါဘူး။ ဆားထပ်ထည့်တာ၊ ပန်းကန်ရွှေ့တာကတော့ စားပွဲမှာ ဧည့်သည် ကိုယ်တိုင် လုပ်ပါတယ်။ Server Component က မီးဖိုချောင်ပါ၊ Client Component က စားပွဲပါ။

App Router မှာ page နဲ့ layout တိုင်းက default အားဖြင့် Server Component ပါ။ Click နဲ့ တုံ့ပြန်ရမယ့်၊ state မှတ်ထားရမယ့် အပိုင်းလေးတွေမှာပဲ file ထိပ်မှာ "use client" ရေးပြီး browser ဆီ ပို့ပါတယ်။

Problem → Why → Solution

ပြဿနာ: Like button တစ်ခုကြောင့် page တစ်ခုလုံး browser ဆီ ရောက်ခြင်း

Note page မှာ Like button လိုလာလို့ developer က useState ကို ထည့်ပါတယ်။ 'useState only works in Client Components' error ကို မြင်တော့ page.tsx ထိပ်မှာ "use client" ကို ထည့်လိုက်ပါတယ်။ Error ပျောက်သွားပေမဲ့ အခု page က async ဖြစ်လို့ မရတော့ဘဲ database ကနေ တိုက်ရိုက် ဖတ်လို့ မရတော့ပါဘူး။ ပိုဆိုးတာက database client ကို import လုပ်ထားတာ ဆက်ရှိနေလို့ server-only code က browser bundle ထဲ ဝင်ဖို့ ကြိုးစားပါတယ်။

app/notes/[id]/page.tsx
"use client"; // added only to silence the useState error
import { useState } from "react";
import { db } from "@/lib/db"; // database code is now imported by client code

export default function NotePage() {
  const [likes, setLikes] = useState(0);
  // can no longer write: const note = await db.note.find(...)
  return <button onClick={() => setLikes(likes + 1)}>Like ({likes})</button>;
}

ဒါက ဘာကြောင့် အရေးကြီးတာလဲ

"use client" က 'ဒီ file နဲ့ သူ import လုပ်တဲ့ အရာအားလုံး browser မှာလည်း run မယ်' လို့ ကြေညာတာပါ။ Page တစ်ခုလုံးကို client လုပ်လိုက်ရင် JavaScript bundle ကြီးလာပြီး server ပေါ်မှာ data ယူနိုင်တဲ့ အားသာချက် ပျောက်ကာ secret တွေ ပေါက်ကြားနိုင်တဲ့ အန္တရာယ်လည်း ရှိလာပါတယ်။ အဖြေက button ကိုပဲ Client Component ခွဲထုတ်တာပါ။

Core concept: "use client" က module tree ကို ခွဲတဲ့ နယ်နိမိတ်

Server Component တွေက server ပေါ်မှာပဲ run ပြီး သူတို့ရဲ့ code က browser ဆီ ဘယ်တော့မှ မရောက်ပါဘူး။ ဒါကြောင့် async/await နဲ့ database ကို တိုက်ရိုက် မေးနိုင်ပြီး API key တွေကို လုံခြုံစွာ သုံးနိုင်ကာ JavaScript bundle ကိုလည်း မတိုးစေပါဘူး။ Server Component ရဲ့ ရလဒ်ကို React Server Component Payload (RSC Payload) လို့ခေါ်တဲ့ compact format အဖြစ် client ဆီ ပို့ပါတယ်။ နောက်ပိုင်း navigation တွေမှာ browser က route အသစ်အတွက် အဲဒီ payload ကိုပဲ ယူပြီး HTML document တစ်ခုလုံး အသစ် မယူပါဘူး။

Client Component တွေကို useState၊ useEffect၊ onClick လို event handler တွေ၊ window နဲ့ localStorage လို browser API တွေ၊ custom hook တွေ လိုတဲ့အခါ သုံးပါတယ်။ နာမည်က 'client' ဖြစ်ပေမဲ့ ပထမဆုံး load မှာ server ပေါ်မှာ HTML အဖြစ် prerender လုပ်ပြီးမှ browser ထဲမှာ hydrate လုပ်ပါတယ် — hydrate ဆိုတာ ရှိပြီးသား HTML ကို event handler တွေ ချိတ်ပြီး interactive ဖြစ်အောင် လုပ်တာပါ။

"use client" ကို file ထိပ်ဆုံး၊ import တွေ အပေါ်မှာ ရေးရပါတယ်။ သူက component တစ်ခုတည်းကို မဟုတ်ဘဲ နယ်နိမိတ်တစ်ခုကို ကြေညာတာပါ: အဲဒီ file က import လုပ်တဲ့ module တွေနဲ့ တိုက်ရိုက် render လုပ်တဲ့ component တွေအားလုံး client bundle ထဲ ဝင်ပါတယ်၊ သူတို့မှာ directive ထပ်ရေးစရာ မလိုပါဘူး။ ဒါကြောင့် boundary ကို tree ရဲ့ အောက်ဆုံး interactive အရွက်လေးမှာ ထားလေ bundle သေးလေ ဖြစ်ပါတယ်။

လိုအပ်ချက်Component အမျိုးအစား
Database/API ကနေ data ယူ၊ secret သုံးServer Component
useState၊ onClick၊ onChangeClient Component
window၊ localStorage၊ geolocationClient Component
Interactivity မပါတဲ့ static markupServer Component (default)

Server Component ကနေ Client Component ဆီ data ပို့တာ props နဲ့ပါ၊ ဒါပေမဲ့ props တွေကို network ဖြတ်ပြီး ပို့ရလို့ React က serialize လုပ်နိုင်တဲ့ value ဖြစ်ရပါမယ်: string၊ number၊ boolean၊ array၊ plain object နဲ့ Date လို built-in အချို့ပါ။ ရိုးရိုး function နဲ့ class instance တွေကို ပို့လို့ မရပါဘူး။

Boundary ဘယ်မှာ ထားမလဲ ဆုံးဖြတ်ပုံ
  1. Default: Server Component
  2. State/event/browser API လို? → client
  3. အဲဒီ အရွက်လေးကိုပဲ file ခွဲ
  4. Server က serializable props ပို့

"use client" ကို error ပျောက်အောင် ထည့်တာ မဟုတ်ဘဲ browser ဆီ ပို့ရမယ့် code ကို ရွေးချယ်တာလို့ မြင်ပါ။

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

server parent + client leaf
// parent.tsx (Server Component: no directive)
export default async function Parent() {
  const data = await loadData();
  return <InteractiveLeaf initial={data.value} />;
}

// interactive-leaf.tsx
"use client";
export function InteractiveLeaf({ initial }: { initial: number }) {
  const [value, setValue] = useState(initial); // state lives here only
  return <Control value={value} onChange={setValue} />;
}

Server parent က data ကို ယူပြီး serializable prop အဖြစ် ပို့ပါတယ်။ "use client" က interactive leaf file မှာပဲ ရှိပြီး state လည်း အဲဒီမှာပဲ နေပါတယ်။

Worked example: server page + client counter

Code block ထဲမှာ file နှစ်ခုကို comment နဲ့ ခွဲပြထားပါတယ်။ Page က server ပေါ်မှာ note အရေအတွက်ကို ယူပြီး Counter ကို number prop အဖြစ် ပေးပါတယ်။ Counter တစ်ခုတည်းကပဲ browser JavaScript လိုပါတယ်။

tsx
// app/page.tsx — Server Component
import { Counter } from "./counter";
import { getNoteCount } from "@/lib/notes";

export default async function Page() {
  const count = await getNoteCount();
  return <Counter initialCount={count} />;
}

// app/counter.tsx — Client Component
"use client";
import { useState } from "react";

export function Counter({ initialCount }: { initialCount: number }) {
  const [count, setCount] = useState(initialCount);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

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

  1. Async server pageexport default async function Page() {

    Directive မပါလို့ Server Component ပါ။ async ဖြစ်လို့ data ကို တိုက်ရိုက် await လုပ်နိုင်ပါတယ်။

  2. Server-side dataconst count = await getNoteCount();

    getNoteCount က server ပေါ်မှာပဲ run လို့ database connection နဲ့ credential တွေ browser ဆီ မရောက်ပါဘူး။

  3. Serializable propreturn <Counter initialCount={count} />;

    Number တစ်ခုကို prop အဖြစ် ပို့တာက RSC Payload ထဲ ထည့်လို့ ရတဲ့ value ဖြစ်လို့ ပြဿနာ မရှိပါဘူး။

  4. The client boundary"use client";

    counter.tsx file ကို client module tree ရဲ့ အစ အဖြစ် ကြေညာပါတယ်။ Page file မှာတော့ ဒီ directive မပါပါဘူး။

  5. State in the leafconst [count, setCount] = useState(initialCount);

    Server ကပေးတဲ့ value နဲ့ state ကို စပြီး click တိုင်း browser ထဲမှာပဲ တိုးပါတယ်။ Server ကို ပြန်မမေးပါဘူး။

You should see
Server မှရလာသော initial count ပေါ်ပြီး button နှိပ်တိုင်း browser မှာ တိုးမည်။

Server Component ကို Client Component ထဲ children အဖြစ် ထည့်ခြင်း

Client Component က Server Component ကို import လုပ်ရင် အဲဒီ component က client bundle ထဲ ဝင်သွားပါတယ်။ Server Component အဖြစ် ဆက်ထားချင်ရင် Server parent ကနေ children (ဒါမှမဟုတ် တခြား prop) အဖြစ် ပို့ပါ။ အဲဒီ children ကို server ပေါ်မှာ ကြိုတင် render လုပ်ပြီး ရလဒ်ကိုပဲ Client Component ထဲ ထည့်ပေးပါတယ်။

app/notes/page.tsx
import Collapsible from "./collapsible"; // "use client" inside
import NoteList from "./note-list"; // async Server Component

export default function NotesPage() {
  return (
    <Collapsible title="My notes">
      <NoteList /> {/* rendered on the server, passed in as children */}
    </Collapsible>
  );
}

Secret တွေကို server-only နဲ့ ကာကွယ်ပါ

NEXT_PUBLIC_ prefix မပါတဲ့ environment variable တွေကို Next.js က client bundle ထဲမှာ empty string နဲ့ အစားထိုးပါတယ်။ ဒါပေမဲ့ database code ကို client က မတော်တဆ import မလုပ်မိအောင် အဲဒီ file ထိပ်မှာ import "server-only" ကို ထည့်ပါ၊ client ကနေ import လုပ်ရင် build error ဖြစ်ပါတယ်။

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

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

ကောင်းမွန်တဲ့ Next.js codebase တွေမှာ "use client" file တွေ နည်းနည်းပဲ ရှိပြီး အားလုံးနီးပါးက tree ရဲ့ အောက်ခြေမှာ ရှိပါတယ်။ Product page တစ်ခုမှာ ပုံ၊ ဖော်ပြချက်၊ review စာရင်းတို့ Server Component ဖြစ်ပြီး 'Add to cart' button နဲ့ ပုံ carousel ကိုသာ Client Component ထားပါတယ်။

  • Theme ဒါမှမဟုတ် cart လို React context provider တွေကို "use client" file တစ်ခုထဲမှာ ရေးပြီး root layout ထဲမှာ {children} ကိုပဲ ဝိုင်းထားပါတယ်။
  • "use client" မပါသေးတဲ့ third-party component (ဥပမာ chart library) ကို ကိုယ်ပိုင် Client Component wrapper file တစ်ခုထဲ re-export လုပ်ပြီးမှ Server Component ကနေ သုံးပါတယ်။
  • lib/db.ts လို file တွေမှာ import "server-only" ထည့်ထားလို့ junior developer တစ်ယောက်က client ကနေ မှားပြီး import လုပ်ရင် build အဆင့်မှာတင် ဖမ်းမိပါတယ်။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

Hook တစ်ခုအတွက် page တစ်ခုလုံးကို "use client" လုပ်ခြင်း

Page က client ဖြစ်သွားရင် async data fetching၊ metadata export နဲ့ server-only import တွေ အကုန် ဆုံးရှုံးပြီး import လုပ်ထားတဲ့ အရာအားလုံး bundle ထဲ ဝင်ပါတယ်။ Hook လိုတဲ့ အပိုင်းကိုပဲ file အသစ် ခွဲထုတ်ပါ။

Function ကို Server ကနေ Client prop အဖြစ် ပို့ခြင်း

<Counter onChange={(n) => save(n)} /> လို Server Component ကနေ ရေးရင် function ကို serialize မလုပ်နိုင်လို့ error ရပါတယ်။ Click logic ကို Client Component ထဲမှာ ရေးပါ၊ server ကို data ပြန်ပို့ရမယ်ဆိုရင် Server Action ကို သုံးပါ (lesson 18)။

Database ဒါမှမဟုတ် secret ကို Client Component ထဲ import လုပ်ခြင်း

Client file ကနေ lib/db ကို import လုပ်ရင် Node-only module တွေကြောင့် build fail ဖြစ်နိုင်ပြီး secret တွေ bundle ထဲ ပါသွားနိုင်တဲ့ အန္တရာယ်ရှိပါတယ်။ Data ကို Server Component မှာ ယူပြီး လိုတဲ့ field တွေကိုပဲ prop အဖြစ် ပို့ပါ။

Best Practices

Best practices

  • Component အသစ်တိုင်းကို Server Component အဖြစ် စရေးပြီး တကယ် interactive လိုမှ "use client" ထည့်ပါ။
  • Client Component file တွေကို -button၊ -form၊ -toggle လို နာမည်တွေ ပေးလိုက်ရင် ဘယ် file တွေ browser ဆီ ရောက်လဲ ဖတ်ရလွယ်ပါတယ်။
  • Database record တစ်ခုလုံး မပို့ဘဲ Client Component က တကယ်သုံးမယ့် field တွေကိုပဲ prop အဖြစ် ပို့ပါ။

Mini challenge

Mini challenge: profile page နဲ့ follow button

Profile page ကို Server Component အဖြစ် ရေးပြီး follower အရေအတွက်ကို server ပေါ်မှာ ယူပါ။ Follow button ကိုတော့ Client Component သီးခြား file အဖြစ် ရေးပြီး click တိုင်း Follow နဲ့ Following ကြား ပြောင်းကာ အရေအတွက်ကိုလည်း ပြင်ရပါမယ်။

  • page.tsx မှာ "use client" မပါရဘူး၊ async ဖြစ်ရမယ်
  • follow-button.tsx က "use client" နဲ့ စရမယ်
  • Server က initialFollowers ကို number prop အဖြစ် ပို့ရမယ်
  • Button text နဲ့ အရေအတွက်က click တိုင်း ပြောင်းရမယ်
app/users/[username]/
// app/users/[username]/page.tsx  (Server Component)
import { FollowButton } from "./follow-button";
import { getProfile } from "@/lib/profiles";

export default async function ProfilePage(/* TODO: params */) {
  // TODO: load the profile on the server
  // TODO: render the name and <FollowButton initialFollowers={...} />
}

// app/users/[username]/follow-button.tsx
// TODO: make this a Client Component
export function FollowButton({ initialFollowers }: { initialFollowers: number }) {
  // TODO: keep "following" and the count in state
  // TODO: toggle both on click
}

ရလဒ်: Page ကို ဖွင့်တာနဲ့ server က ယူလာတဲ့ အရေအတွက် (ဥပမာ 'Follow · 41 followers') ကို HTML ထဲမှာ မြင်ရပါတယ်။ နှိပ်ရင် 'Following · 42 followers' ဖြစ်ပြီး ထပ်နှိပ်ရင် ပြန်ဖြစ်ပါတယ်။ Browser ဆီ ပို့တဲ့ JavaScript ထဲမှာ button code ပဲ ပါပါတယ်။

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

Button ထဲမှာ const [following, setFollowing] = useState(false) ကို ထားပြီး အရေအတွက်ကို initialFollowers + (following ? 1 : 0) လို့ တွက်လိုက်ရင် state နှစ်ခု မလိုပါဘူး။

အဖြေ ကြည့်ရန်
app/users/[username]/
// app/users/[username]/page.tsx  (Server Component)
import { FollowButton } from "./follow-button";
import { getProfile } from "@/lib/profiles";

export default async function ProfilePage({
  params,
}: {
  params: Promise<{ username: string }>;
}) {
  const { username } = await params;
  const profile = await getProfile(username); // { name, followers }
  return (
    <section>
      <h1>{profile.name}</h1>
      <FollowButton initialFollowers={profile.followers} />
    </section>
  );
}

// app/users/[username]/follow-button.tsx
"use client";
import { useState } from "react";

export function FollowButton({ initialFollowers }: { initialFollowers: number }) {
  const [following, setFollowing] = useState(false);
  const count = initialFollowers + (following ? 1 : 0);
  return (
    <button onClick={() => setFollowing(!following)}>
      {following ? "Following" : "Follow"} · {count} followers
    </button>
  );
}

Quick Quiz 1 / 3

အောက်က component တွေထဲက ဘယ်ဟာက "use client" မဖြစ်မနေ လိုလဲ?

Quick Quiz 2 / 3

Server Component page က <Counter initialCount={5} onReset={() => console.log("reset")} /> လို့ ရေးပါတယ်၊ Counter က Client Component ပါ။ ဘာဖြစ်မလဲ?

Quick Quiz 3 / 3

"use client" ပါတဲ့ Client Component တွေက server ပေါ်မှာ render ဖြစ်သလား?

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

  • Page နဲ့ layout တွေက default အားဖြင့် Server Component ဖြစ်ပြီး data ယူတာနဲ့ secret သုံးတာကို server ပေါ်မှာ လုပ်ပါတယ်။
  • "use client" က boundary ပါ: file နဲ့ သူ import လုပ်တာအားလုံး client bundle ထဲ ဝင်လို့ interactive အရွက်လေးမှာပဲ ထားပါ။
  • Server ကနေ client ဆီ props တွေက serializable ဖြစ်ရပြီး function ပို့လို့ မရပါ။
  • Server Component ကို Client Component ထဲ children အဖြစ် ထည့်နိုင်ပြီး import "server-only" နဲ့ secret တွေကို ကာကွယ်ပါ။

Next.js — Server and Client Components — Next.js

Next.js — Server and Client Components — Next.js

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

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

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

Server Component နှင့် Client Component | Thuta Learning