ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
App သေးသေးလေးဆိုရင် file အားလုံးကို folder တစ်ခုထဲ ထည့်ထားလို့ ရပါတယ်။ Feature ဆယ်ခု၊ developer ငါးယောက် ဖြစ်လာရင်တော့ ဘယ် file က ဘယ်ဟာနဲ့ ဆက်စပ်လဲ ရှာရတာ ခက်လာပြီး ပြင်တိုင်း တခြားနေရာ ကျိုးသွားတတ်ပါတယ်။
Shopping mall တစ်ခုကို စဉ်းစားကြည့်ပါ။ ဆိုင်တစ်ဆိုင်ချင်းစီ (feature) မှာ ကိုယ်ပိုင် ကုန်ပစ္စည်း၊ ကိုယ်ပိုင် ဝန်ထမ်းရှိပြီး customer က ရှေ့တံခါး (public entry point) ကနေပဲ ဝင်ပါတယ်။ Mall ကတော့ လမ်းလျှောက်လမ်း၊ မီး၊ လုံခြုံရေး (app shell) ကို ပေးပါတယ်။ ဆိုင်တစ်ဆိုင် မီးလောင်ရင် (error) mall တစ်ခုလုံး မပိတ်ဘဲ အဲဒီဆိုင်ကိုပဲ ပိတ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: အရာအားလုံး အရာအားလုံးကို import လုပ်နေတဲ့ app
အောက်က Dashboard က tasks feature ရဲ့ အတွင်းပိုင်း helper file နဲ့ billing feature ရဲ့ internal store ကို တိုက်ရိုက် import လုပ်ထားပါတယ်။ Tasks team က TaskRow folder ကို ပြန်စီလိုက်တာနဲ့ Dashboard ကျိုးပါတယ်၊ ဒါပေမယ့် tasks team က Dashboard ကို ဘယ်သူ သုံးနေလဲ မသိပါဘူး။
ပိုဆိုးတာက billing widget က data မမှန်လို့ render လုပ်ချိန်မှာ throw လုပ်ရင် error boundary မရှိလို့ React က root တစ်ခုလုံးကို unmount လုပ်ပြီး user က ဖြူဖြူ page တစ်ခုပဲ မြင်ရပါတယ်။ Widget တစ်ခုရဲ့ bug က app တစ်ခုလုံးကို ဖျက်ပစ်လိုက်တာပါ။
// reaching into other features' private files
import { formatDue } from '../features/tasks/components/TaskRow/helpers.js';
import { invoiceStore } from '../features/billing/store/internal.js';
export default function Dashboard() {
// 600 lines: fetching, permissions, formatting and layout in one place
const invoices = invoiceStore.getAll();
return (
<main>
<h1>Due {formatDue(new Date())}</h1>
<BillingWidget invoices={invoices} /> {/* throws -> blank page */}
</main>
);
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Folder တွေမှာ "ဒီဟာက ပြင်ပကို ပေးထားတာ၊ ဒါက အတွင်းရေး" ဆိုတဲ့ နယ်နိမိတ် မရှိရင် file တိုင်းက တခြား file တိုင်းရဲ့ public API ဖြစ်သွားပါတယ်။ ဒါ့အပြင် React မှာ render ချိန် error တစ်ခုကို ဘယ် error boundary ကမှ မဖမ်းရင် React က UI အပျက်ကို ဆက်မပြဘဲ tree တစ်ခုလုံးကို ဖယ်ရှားပါတယ်။ Boundary တွေကို ဘယ်နေရာ ထားမလဲဆိုတာ architecture ဆုံးဖြတ်ချက် တစ်ခုပါ။
Core concept: feature နယ်နိမိတ်နဲ့ failure နယ်နိမိတ်
Production React app တစ်ခုကို နယ်နိမိတ် နှစ်မျိုးနဲ့ ဒီဇိုင်းဆွဲပါတယ်။ Code နယ်နိမိတ်က "ဘယ် file က ဘယ် file ကို import လုပ်ခွင့်ရှိလဲ" ကို ဆုံးဖြတ်ပါတယ်။ Runtime နယ်နိမိတ်က "တစ်ခုခု မှားသွားရင် ဒါမှမဟုတ် နှေးနေရင် screen ရဲ့ ဘယ်အပိုင်းက ထိခိုက်လဲ" ကို ဆုံးဖြတ်ပါတယ်။
၁။ Feature အလိုက် folder ခွဲပါ
features/tasks/ ထဲမှာ tasks နဲ့ ဆိုင်တဲ့ component၊ hook၊ API call နဲ့ test တွေ အကုန် အတူရှိပါတယ်။ Feature တစ်ခုကို ပြင်ရင် folder တစ်ခုထဲမှာပဲ အလုပ်လုပ်ရပါတယ်။ shared/ ထဲမှာတော့ Button လို feature မခွဲဘဲ ဘယ်နေရာမှာမဆို သုံးတဲ့ အရာတွေပဲ ထားပါ။
၂။ Public entry point တစ်ခုတည်း ထားပါ
features/tasks/index.js က ပြင်ပကို ပေးချင်တာကိုပဲ export လုပ်ပါတယ်။ တခြား code က index.js ကနေပဲ import လုပ်ရလို့ feature အတွင်းပိုင်းကို စိတ်ကြိုက် ပြန်စီနိုင်ပါတယ်။ ESLint ရဲ့ no-restricted-imports rule နဲ့ ဒီစည်းကမ်းကို အလိုအလျောက် စစ်လို့ ရပါတယ်။
၃။ Import ဦးတည်ရာ တစ်ဖက်တည်း ထားပါ
app/ က features/ ကို သုံးပါတယ်၊ features/ က shared/ နဲ့ services/ ကို သုံးပါတယ်။ ပြောင်းပြန် မသွားရပါဘူး။ shared/ က feature တစ်ခုခုကို import လုပ်လာရင် အဲဒီ code က တကယ် shared မဟုတ်တော့ကြောင်း လက္ခဏာပါ။
၄။ Runtime boundary တွေကို ချပါ
Route ဒါမှမဟုတ် widget တစ်ခုစီကို ErrorBoundary နဲ့ ပတ်ထားရင် crash ဖြစ်တဲ့ အပိုင်းမှာပဲ fallback ပေါ်ပါတယ်။ Suspense က code ဒါမှမဟုတ် data loading ဖြစ်နေတုန်း fallback ပြပြီး lazy() က route တစ်ခုချင်းရဲ့ code ကို လိုမှ download လုပ်ပါတယ်။
| Boundary | ဖြေပေးတဲ့ မေးခွန်း | Tool |
|---|---|---|
| Feature folder + index.js | ဒီ code ကို ဘယ်သူ သုံးခွင့်ရှိလဲ | Folder structure၊ no-restricted-imports |
| ErrorBoundary | Crash ဖြစ်ရင် ဘယ်အပိုင်း ပျောက်မလဲ | Class component + fallback |
| Suspense + lazy() | Loading ဖြစ်နေတုန်း ဘာပြမလဲ၊ ဘယ်အချိန် download လုပ်မလဲ | Fallback UI + dynamic import() |
ဒီဖွဲ့စည်းပုံ အားလုံးကို project ရဲ့ ပထမနေ့ကတည်းက မလိုပါဘူး။ Component ဆယ်ခုလောက်ပဲ ရှိသေးရင် src/components ထဲမှာ ထားတာ လုံလောက်ပါတယ်။ Feature နှစ်ခုက file တစ်ခုတည်းကို တစ်ပြိုင်နက် ပြင်ရတာ၊ file တစ်ခုပြင်ရင် မဆိုင်တဲ့ screen ကျိုးတာ စတဲ့ နာကျင်မှု စတွေ့တဲ့အခါ feature folder တွေကို ခွဲထုတ်ပါ။ Error boundary ကိုတော့ production ကို ပထမဆုံး deploy မလုပ်ခင် ထည့်ထားပါ။
Folders and import direction
----------------------------
src/
app/ providers, router, layout --+
features/ | imports flow down only
tasks/ index.js <- public door v
billing/ index.js
shared/ generic UI (Button, Modal), ErrorBoundary
services/ http client, monitoring, storage adapters- Feature တစ်ခု = folder တစ်ခု
- ဝင်ပေါက်က index.js တစ်ခုတည်း
- app/ → features/ → shared/
- Route တိုင်းမှာ error boundary
File အသစ်တစ်ခု ထည့်တိုင်း "ဒါက ဘယ်ဆိုင်ထဲ ဝင်မလဲ၊ ဒါကို တခြားဆိုင်တွေ မြင်ရဖို့ လိုလား" လို့ မေးပါ။
အခြေခံ ပုံစံ
// features/<name>/index.js
export { default as FeaturePage } from './FeaturePage.jsx';
// app/App.jsx
const FeaturePage = lazy(() => import('../features/<name>/index.js')
.then((m) => ({ default: m.FeaturePage })));
<ErrorBoundary fallback={<ErrorMessage />}>
<Suspense fallback={<Spinner />}><FeaturePage /></Suspense>
</ErrorBoundary>Feature က index.js ကနေ export လုပ်၊ app shell က lazy() နဲ့ load လုပ်ပြီး ErrorBoundary နဲ့ Suspense နှစ်ထပ်နဲ့ ပတ်ထားပါတယ်။ lazy() က default export ကို မျှော်လင့်လို့ named export ကို then() နဲ့ ပြောင်းပေးပါတယ်။
Feature entry point နဲ့ app shell
ဒီမှာ folder ပုံစံကို စာသားနဲ့ ရှင်းပြရုံ မဟုတ်ဘဲ code အစစ်နဲ့ ကြည့်ပါမယ်။ Block တစ်ခုထဲမှာ file နှစ်ခု ပါပါတယ်: tasks feature ရဲ့ index.js နဲ့ app shell ဖြစ်တဲ့ App.jsx ပါ။ App.jsx က tasks ရဲ့ အတွင်းပိုင်း file တွေကို တစ်ခုမှ မသိဘဲ index.js ကိုပဲ lazy load လုပ်ပါတယ်။ ErrorBoundary component ကိုယ်တိုင်ကို နောက် section မှာ ကြည့်ပါမယ်။
// src/features/tasks/index.js: the feature's only public entry point
export { default as TasksPage } from './TasksPage.jsx';
export { useTasks } from './useTasks.js';
// src/app/App.jsx: the shell wires features together
import { lazy, Suspense } from 'react';
import AppLayout from './AppLayout.jsx';
import { ErrorBoundary } from '../shared/ErrorBoundary.jsx';
const TasksPage = lazy(() =>
import('../features/tasks/index.js').then((m) => ({ default: m.TasksPage })),
);
export default function App() {
return (
<AppLayout>
<ErrorBoundary fallback={<p role="alert">Tasks could not be shown.</p>}>
<Suspense fallback={<p>Loading tasks…</p>}>
<TasksPage />
</Suspense>
</ErrorBoundary>
</AppLayout>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The feature's public door
export { default as TasksPage } from './TasksPage.jsx';Feature က ပြင်ပကို ပေးတဲ့ component ကို နာမည်နဲ့ re-export လုပ်ပါတယ်။ TaskRow လို အတွင်းပိုင်း component တွေကို export မလုပ်လို့ တခြား feature တွေ မှီခိုလို့ မရပါဘူး။
- Load the feature on demand
const TasksPage = lazy(() =>lazy() က TasksPage ပထမဆုံး render ဖြစ်မှသာ import() ကို ခေါ်လို့ bundler က tasks feature ကို သီးသန့် chunk အဖြစ် ခွဲထုတ်ပါတယ်။ Component အပြင်ဘက် module level မှာ ကြေညာရပါမယ်။
- Named export to default
import('../features/tasks/index.js').then((m) => ({ default: m.TasksPage })),lazy() က { default: Component } ပုံစံကို မျှော်လင့်ပါတယ်။ index.js က named export သုံးထားလို့ then() နဲ့ ပုံစံပြောင်းပေးရပါတယ်။
- Failure boundary outside
<ErrorBoundary fallback={<p role="alert">Tasks could not be shown.</p>}>Boundary အောက်က render error တွေကို ဖမ်းပြီး fallback ကို ပြပါတယ်။ Chunk download fail တာကလည်း lazy component ကို render လုပ်ချိန် error အဖြစ် ပေါ်လို့ ဒီ boundary က ဖမ်းပါတယ်။
- Loading boundary inside
<Suspense fallback={<p>Loading tasks…</p>}>Code download ဖြစ်နေတုန်း fallback ကို ပြပါတယ်။ ErrorBoundary ရဲ့ အတွင်းမှာ ထားလို့ loading နဲ့ error နှစ်မျိုးလုံးက tasks area ထဲမှာပဲ ကန့်သတ်ထားပါတယ်။
AppLayout က ချက်ချင်း ပေါ်ပြီး tasks ရဲ့ code download ဖြစ်နေတုန်း layout ထဲမှာ "Loading tasks…" ပေါ်ပါတယ်။ ပြီးရင် TasksPage က အစားထိုးဝင်ပါတယ်။ TasksPage က render လုပ်ချိန် throw လုပ်ရင် အဲဒီနေရာမှာပဲ "Tasks could not be shown." ပေါ်ပြီး layout နဲ့ navigation က ဆက်အလုပ်လုပ်ပါတယ်။ErrorBoundary နဲ့ error reporting
Error boundary ကို React 19 မှာလည်း class component နဲ့ပဲ ရေးရပါသေးတယ်။ getDerivedStateFromError က fallback ပြဖို့ state ကို ပြောင်းပြီး componentDidCatch က error ကို monitoring service ဆီ ပို့ပါတယ်။ Boundary က render၊ lifecycle method နဲ့ constructor ထဲက error တွေကို ဖမ်းပါတယ်။ Event handler ထဲက error၊ setTimeout ဒါမှမဟုတ် promise ထဲက async error တွေကိုတော့ မဖမ်းပါဘူး။ အဲဒါတွေကို try/catch နဲ့ ကိုယ်တိုင် ကိုင်တွယ်ရပါတယ်။
import { Component } from 'react';
import { logError } from '../services/monitoring.js';
export class ErrorBoundary extends Component {
state = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, info) {
logError(error, info.componentStack);
}
render() {
return this.state.hasError ? this.props.fallback : this.props.children;
}
}Quality gate တွေနဲ့ React Compiler
Merge မလုပ်ခင် CI က lint၊ test၊ build သုံးခုကို အမြဲ run ပါတယ်။ Lint ထဲမှာ eslint-plugin-react-hooks ပါရပါမယ်၊ hook စည်းကမ်းနဲ့ dependency array အမှားတွေကို ဖမ်းပေးလို့ပါ။ Build ကို CI မှာ run တာက local မှာ သတိမထားမိတဲ့ import လမ်းကြောင်း အမှားတွေကို ဖမ်းပေးပါတယ်။
React Compiler က build step မှာ memoization ကို အလိုအလျောက် ထည့်ပေးပေမယ့် Rules of React ကို လိုက်နာတဲ့ code ကိုသာ optimize လုပ်ပါတယ်။ ဒါကြောင့် lint error တွေကို အရင်ရှင်းပြီး official installation guide အတိုင်း project ရဲ့ အစိတ်အပိုင်း တစ်ခုကနေ စဖွင့်ကာ test နဲ့ Profiler နဲ့ မတိုင်ခင်/ပြီးနောက် နှိုင်းယှဉ်တာ လုံခြုံပါတယ်။
{
"scripts": {
"lint": "eslint .",
"test": "vitest run",
"build": "vite build",
"check": "npm run lint && npm run test && npm run build"
}
}လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Team ကြီးတွေမှာ feature folder တစ်ခုစီကို team တစ်ခုက ပိုင်ပြီး CODEOWNERS file နဲ့ အဲဒီ folder ကို ပြင်တဲ့ pull request တိုင်းမှာ ပိုင်ရှင် team ကို review တောင်းပါတယ်။ Public entry point ရှိလို့ team တစ်ခုက တခြား team ရဲ့ အတွင်းပိုင်းကို မထိဘဲ အလုပ်လုပ်နိုင်ပါတယ်။ componentDidCatch ကနေ ပို့တဲ့ error တွေကို Sentry လို monitoring tool မှာ release version နဲ့ တွဲမှတ်ထားလို့ deploy အသစ်တစ်ခုက error တွေ ဖန်တီးလာရင် ချက်ချင်း သိနိုင်ပါတယ်။
Environment variable တွေကိုလည်း သတိထားရပါတယ်။ Vite မှာ VITE_ နဲ့ စတဲ့ variable တွေကို import.meta.env ကနေ client code ထဲ ထည့်ပေးလို့ browser ထဲက JavaScript ကို ဖွင့်ကြည့်တဲ့ ဘယ်သူမဆို မြင်နိုင်ပါတယ်။ API secret key တွေကို frontend env ထဲ ဘယ်တော့မှ မထည့်ဘဲ server ဘက်မှာပဲ ထားပါ။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
App တစ်ခုလုံးကို boundary တစ်ခုတည်းနဲ့ ပတ်တာ
Root မှာ boundary တစ်ခုတည်း ထားရင် page ဖြူကြီး မပေါ်တော့ပေမယ့် widget သေးသေးလေး တစ်ခု crash ဖြစ်တာနဲ့ navigation အပါအဝင် screen တစ်ခုလုံးက fallback ဖြစ်သွားပါတယ်။ Route တစ်ခုစီနဲ့ အရေးကြီး widget တွေကို သီးသန့် boundary ပေးပါ။
shared/ ကို အမှိုက်ပုံး လုပ်တာ
"နေရာမသေချာရင် shared/ ထဲ ထည့်လိုက်" လို့ လုပ်ရင် shared/ က feature logic တွေနဲ့ ပြည့်လာပြီး feature တိုင်းက အချင်းချင်း သွယ်ဝိုက်ပြီး ချိတ်မိသွားပါတယ်။ Feature တစ်ခုတည်းက သုံးတာဆိုရင် အဲဒီ feature folder ထဲမှာပဲ ထားပါ။
Secret ကို VITE_ variable ထဲ ထည့်တာ
VITE_STRIPE_SECRET လို variable က build လုပ်တဲ့အခါ JavaScript bundle ထဲ စာသားအတိုင်း ပါသွားပါတယ်။ ".env file က git ထဲ မပါလို့ လုံခြုံတယ်" ဆိုတာ မှားပါတယ်၊ build ရလဒ်ထဲမှာ ပါနေလို့ပါ။
Mini challenge
services/ အတွက် API client တစ်ခု
Component တွေက fetch ကို တိုက်ရိုက် မခေါ်ရအောင် services/http.js အတွက် createApiClient() ကို ရေးပါ။ Client က base URL၊ auth token နဲ့ JSON ကို ကိုင်တွယ်ပြီး HTTP error တိုင်းကို status ပါတဲ့ ApiError အဖြစ် ပြောင်းပေးရပါမယ်။ Node မှာ run နိုင်အောင် fake fetch ကို ထည့်ပေးထားပါတယ်။
- fetchImpl နဲ့ getToken ကို parameter အဖြစ် လက်ခံပါ (test လုပ်ရ လွယ်အောင်)
- Token ရှိရင် Authorization: Bearer <token> header ထည့်ပါ
- response.ok မဟုတ်ရင် server message (မရှိရင် "HTTP <status>") နဲ့ ApiError throw လုပ်ပါ
- get(path) နဲ့ post(path, body) ကို ပြန်ပေးပြီး tasks API module ကို အဲဒီ client ပေါ်မှာ တည်ဆောက်ပါ
class ApiError extends Error {
// TODO: constructor(status, message) that sets name = 'ApiError' and status
}
function createApiClient({ baseUrl, fetchImpl = fetch, getToken = () => null }) {
async function request(path, { method = 'GET', body, signal } = {}) {
// TODO: headers (Accept, optional Authorization, Content-Type for a body)
// TODO: call fetchImpl(baseUrl + path, { method, headers, signal, body })
// TODO: parse JSON (204 -> null); !res.ok -> throw ApiError
}
// TODO: return { get, post }
}
// TODO: tasksApi(client) -> { list(signal), create(title) }
// TODO: a fake fetch with GET/POST /api/tasks, 401 without a token,
// 404 for unknown paths; then call the client and print the resultsရလဒ်: Task list၊ အသစ်ဖန်တီးလိုက်တဲ့ task၊ "ApiError 404 No route for /api/nope" နဲ့ token ဖယ်ပြီးနောက် "ApiError 401 Please sign in" ဆိုတဲ့ line လေးကြောင်း ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Node 22 မှာ Response class က global ဖြစ်လို့ fake fetch က new Response(JSON.stringify(data), { status }) ကို ပြန်ပေးနိုင်ပါတယ်။ getToken ကို function အဖြစ် ထားရင် request တိုင်းမှာ token အသစ်ကို ဖတ်ပါတယ်။
အဖြေ ကြည့်ရန်
class ApiError extends Error {
constructor(status, message) {
super(message);
this.name = 'ApiError';
this.status = status;
}
}
function createApiClient({ baseUrl, fetchImpl = fetch, getToken = () => null }) {
async function request(path, { method = 'GET', body, signal } = {}) {
const headers = { Accept: 'application/json' };
const token = getToken();
if (token) headers.Authorization = `Bearer ${token}`;
if (body !== undefined) headers['Content-Type'] = 'application/json';
const res = await fetchImpl(`${baseUrl}${path}`, {
method,
headers,
signal,
body: body === undefined ? undefined : JSON.stringify(body),
});
const data = res.status === 204 ? null : await res.json();
if (!res.ok) throw new ApiError(res.status, data?.message ?? `HTTP ${res.status}`);
return data;
}
return {
get: (path, options) => request(path, options),
post: (path, body, options) => request(path, { ...options, method: 'POST', body }),
};
}
const tasksApi = (client) => ({
list: (signal) => client.get('/api/tasks', { signal }),
create: (title) => client.post('/api/tasks', { title }),
});
// A fake server so the example runs without a network.
const tasks = [{ id: 1, title: 'Write tests' }];
const json = (status, data) =>
new Response(JSON.stringify(data), {
status,
headers: { 'Content-Type': 'application/json' },
});
async function fakeFetch(url, init) {
const { pathname } = new URL(url);
if (init.headers.Authorization !== 'Bearer secret') {
return json(401, { message: 'Please sign in' });
}
if (pathname === '/api/tasks' && init.method === 'GET') return json(200, tasks);
if (pathname === '/api/tasks' && init.method === 'POST') {
const task = { id: tasks.length + 1, ...JSON.parse(init.body) };
tasks.push(task);
return json(201, task);
}
return json(404, { message: `No route for ${pathname}` });
}
async function main() {
let token = 'secret';
const client = createApiClient({
baseUrl: 'https://example.test',
fetchImpl: fakeFetch,
getToken: () => token,
});
const api = tasksApi(client);
console.log(JSON.stringify(await api.list()));
console.log(JSON.stringify(await api.create('Ship v1')));
try {
await client.get('/api/nope');
} catch (err) {
console.log(err.name, err.status, err.message);
}
token = null;
try {
await api.list();
} catch (err) {
console.log(err.name, err.status, err.message);
}
}
main();Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Feature တစ်ခုစီကို folder တစ်ခုထဲ စုပြီး index.js ကနေသာ ပြင်ပကို ပေးပါ
- Import ဦးတည်ရာ: app/ → features/ → shared/ နဲ့ services/
- Route နဲ့ widget တွေကို ErrorBoundary + Suspense နဲ့ ပတ်ပြီး lazy() နဲ့ code ခွဲပါ
- Error boundary က event handler နဲ့ async error ကို မဖမ်းပါ
- CI မှာ lint/test/build ကို run ပြီး secret ကို VITE_ variable ထဲ ဘယ်တော့မှ မထည့်ပါနဲ့
React Compiler v1.0 — React