ရိုးရိုးလေး ရှင်းပြမယ်
Search params ကို ရိုးရိုးလေး ပြောရရင်
URL ထဲက ? နောက်က အပိုင်း (query string) ဟာ ဟင်းချက်နည်း ကတ်ပြားပေါ်မှာ ရေးထားတဲ့ setting တွေလိုပါပဲ။ ကတ်ပြား ရတဲ့သူတိုင်း မြင်ကွင်း တူတူကို ပြန်ဖန်တီးနိုင်ပါတယ်: /notes?q=hooks&page=2 ကို သူငယ်ချင်းဆီ ပို့လိုက်ရင် သူလည်း page 2 မှာ hooks ရလဒ်တွေကို မြင်ရပါတယ်။
Component state ကတော့ ခေါင်းထဲမှာ မှတ်ထားတဲ့ setting နဲ့ တူပါတယ်။ Page ကို refresh လုပ်လိုက်တာနဲ့ ပျောက်သွားပြီး တခြားသူကို ပေးလို့လည်း မရပါဘူး။
Problem → Why → Solution
ပြဿနာ: URL ကို ယုံလိုက်တဲ့ filter
ပထမ version မှာ search keyword ကို useState ထဲ ထားခဲ့လို့ refresh လုပ်ရင် ပျောက်ပြီး back button ကလည်း filter ကို မပြန်ဖြေပါဘူး။ ဒါကြောင့် URL ထဲ ရွှေ့လိုက်ပါတယ် — ကောင်းတဲ့ ဆုံးဖြတ်ချက်ပါ။ ဒါပေမဲ့ code က URL value တွေကို database ဆီ တိုက်ရိုက် ပို့နေပါတယ်။ ?page=abc က NaN offset ဖြစ်ပြီး query fail ဖြစ်ပါတယ်၊ ?page=-3 က အနုတ် skip ဖြစ်ပြီး ?sort=passwordHash ကတော့ visitor ရိုက်ထည့်တဲ့ ဘယ် column နဲ့မဆို sort လုပ်ခွင့်ပေးနေပါတယ်။
export default async function NotesPage({ searchParams }) {
const { q, page, sort } = await searchParams;
const notes = await db.note.findMany({
where: { title: { contains: q } },
skip: (page - 1) * 20, // "abc" - 1 is NaN
orderBy: { [sort]: "desc" }, // any column the visitor types
});
return <NoteList notes={notes} />;
}URL ဆိုတာ user input ပါ
Link ကို သင်ကိုယ်တိုင် ထုတ်ပေးခဲ့ပေမဲ့ address bar ထဲမှာ ဘယ်သူမဆို ပြင်ရိုက်နိုင်ပါတယ်၊ bot တွေကလည်း ကျပန်း query တွေ ပို့ပါတယ်။ ဒါကြောင့် search param တိုင်းကို form field လိုပဲ ဆက်ဆံရပါမယ်: တစ်နေရာတည်းမှာ parse လုပ်၊ ခွင့်ပြုထားတဲ့ တန်ဖိုးတွေနဲ့ တိုက်စစ်ပြီး မမှန်ရင် default ကို ပြန်သုံးပါ။
Core concept: URL ကနေ ဖတ်၊ တစ်ခါတည်း normalize လုပ်
App Router မှာ page component တွေက searchParams prop ကို လက်ခံပါတယ်။ Next.js 15 ကစပြီး ဒါက await လုပ်ရတဲ့ Promise ဖြစ်ပါတယ်၊ Next.js 14 မှာတော့ ရိုးရိုး object ပါ။ Key တစ်ခုချင်းရဲ့ value က string | string[] | undefined ဖြစ်ပါတယ်: ?tag=a&tag=b ဆိုရင် array ရပြီး key မပါရင် undefined ရပါတယ်။ Layout တွေကတော့ searchParams ကို မရပါဘူး၊ navigation တိုင်းမှာ layout ကို ပြန် render မလုပ်လို့ပါ။
အရေးကြီးဆုံး အလေ့အကျင့်က boundary မှာ တစ်ခါတည်း normalize လုပ်တာပါ။ Raw object ကို default တွေ ပါပြီး type သတ်မှတ်ထားတဲ့ Filters object တစ်ခုအဖြစ် ပြောင်းပါ: q ကို trim လုပ်ပြီး အရှည်ကန့်သတ်၊ page ကို Number.isInteger နဲ့ စစ်ပြီး 1 ထက် ငယ်ရင် 1 ထား၊ sort နဲ့ difficulty လို enum တွေကို allowlist နဲ့ တိုက်စစ်ပါ။ Data function က Filters ကိုပဲ လက်ခံတဲ့အတွက် အဲဒီအောက်က code တွေ raw string ကို ဘယ်တော့မှ မမြင်ရပါဘူး။
searchParams ရဲ့ value တွေကို request အချိန်မှာပဲ သိနိုင်တဲ့အတွက် ဖတ်လိုက်တာနဲ့ page က dynamic rendering ဖြစ်သွားပါတယ်။ ဒါက ပုံမှန်ပါ၊ ရလဒ်တွေကို query အလိုက် ပြောင်းရမှာကိုး။ Browser ဘက်မှာတော့ URL ကို ပြောင်းရုံပါပဲ: Link ဒါမှမဟုတ် router.replace နဲ့ URL အသစ်ကို သွားလိုက်ရင် Next.js က server ပေါ်မှာ page ကို query အသစ်နဲ့ ပြန် render လုပ်ပေးပါတယ်။
ဘယ် state ကို URL ထဲ ထည့်မလဲဆိုတာလည်း ရွေးရပါတယ်။ Link မျှဝေတဲ့သူ တူတူမြင်ရမယ့် state (keyword, filter, sort, page) က URL ထဲ ရောက်သင့်ပါတယ်။ Dropdown ဖွင့်ထားလား၊ input ကို focus လုပ်ထားလား လို ခဏတာ UI state ကတော့ component state ထဲမှာပဲ နေသင့်ပါတယ်၊ URL ထဲ ထည့်ရင် link တွေ ရှုပ်ထွေးပြီး history ပြည့်သွားလို့ပါ။
| Tool | ဘယ်မှာ | ဘာအတွက် |
|---|---|---|
| searchParams prop | Server page | Query ကို ဖတ်ပြီး data ယူဖို့ |
| useSearchParams() | Client Component | လက်ရှိ query ကို UI ထဲမှာ ဖတ်ဖို့ |
| router.replace(url) | Client Component | History entry အသစ် မထည့်ဘဲ URL ပြောင်းဖို့ |
| <Link href> | နှစ်မျိုးလုံး | Pagination နဲ့ filter link တွေအတွက် |
- URL ထဲမှာ state: ?q=next&page=2
- Page က searchParams ကို await
- လုံခြုံတဲ့ Filters အဖြစ် တစ်ခါ parse
- Data function က Filters ကိုပဲ သုံး
- Link နဲ့ input က URL အသစ် ရေး
UI က state ကို တိုက်ရိုက် မပြောင်းပါဘူး၊ URL ကိုပဲ ပြောင်းပြီး server က URL အသစ်ကနေ page ကို ပြန်တည်ဆောက်ပါတယ်။
အထွေထွေ ပုံစံ
type SearchParams = Promise<{ [key: string]: string | string[] | undefined }>;
export default async function Page({ searchParams }: { searchParams: SearchParams }) {
const filters = parseFilters(await searchParams); // validate once, with defaults
const data = await getData(filters); // never sees raw strings
return <View data={data} filters={filters} />;
}searchParams ကို await လုပ်၊ parse function တစ်ခုထဲမှာ default နဲ့ validation လုပ်ပြီးမှ ရလာတဲ့ typed object ကို data layer ဆီ ပေးပါ။
Worked example: search နဲ့ pagination ပါတဲ့ notes page
ဒီ page က q နဲ့ page ကို URL ကနေ ဖတ်ပါတယ်။ Search param တွေက string ရိုးရိုး အမြဲ မဟုတ်ပါဘူး၊ key တစ်ခု ထပ်နေနိုင်သလို user က page=2.5 လို ကြိုက်တာ ရိုက်ထည့်နိုင်ပါတယ်။ ဒါကြောင့် code က ထပ်နေတဲ့ key ကို first() နဲ့ ကိုင်တွယ်ပြီး page က အပေါင်း integer ဖြစ်မှ လက်ခံပါတယ်။
// app/notes/page.tsx
import { searchNotes } from "@/lib/data/notes";
import { NoteList } from "./note-list";
type SearchParams = Promise<{ [key: string]: string | string[] | undefined }>;
function first(value: string | string[] | undefined): string | undefined {
return Array.isArray(value) ? value[0] : value;
}
export default async function NotesPage({
searchParams,
}: {
searchParams: SearchParams;
}) {
const params = await searchParams;
const query = (first(params.q) ?? "").trim().slice(0, 100);
const requestedPage = Number(first(params.page) ?? "1");
const isValidPage = Number.isInteger(requestedPage) && requestedPage > 0;
const page = isValidPage ? requestedPage : 1;
const notes = await searchNotes({ query, page });
return <NoteList notes={notes} query={query} page={page} />;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Honest param types
type SearchParams = Promise<{ [key: string]: string | string[] | undefined }>;Next.js ပေးတဲ့ shape အတိုင်း type ပေးထားပါတယ်: Promise ဖြစ်ပြီး value တစ်ခုချင်းက string၊ string array ဒါမှမဟုတ် undefined ဖြစ်နိုင်ပါတယ်။
- Repeated keys
return Array.isArray(value) ? value[0] : value;?q=a&q=b လို URL မှာ array ရပါတယ်။ ပထမ value ကို ယူလိုက်ရင် .trim() က array ပေါ်မှာ crash မဖြစ်တော့ပါဘူး။
- Clean the keyword
const query = (first(params.q) ?? "").trim().slice(0, 100);မပါရင် string အလွတ်၊ ရှေ့နောက် space ဖယ်ပြီး စာလုံး 100 ထက် မပိုစေပါဘူး။ ရှည်လျားတဲ့ input နဲ့ database ကို ဝန်မပိစေဖို့ပါ။
- Validate the page
const isValidPage = Number.isInteger(requestedPage) && requestedPage > 0;Number("abc") က NaN၊ Number("2.5") က 2.5 ဖြစ်ပါတယ်။ Integer လည်းဖြစ်၊ သုညထက်လည်း ကြီးမှ လက်ခံပြီး ကျန်တာအားလုံး page 1 ဖြစ်သွားပါတယ်။
- Only clean values go down
const notes = await searchNotes({ query, page });Data function က normalize လုပ်ပြီးသား value တွေကိုပဲ ရတဲ့အတွက် validation က တစ်နေရာတည်းမှာ ရှိပါတယ်။
/notes?q=next&page=2 က "next" နဲ့ ကိုက်တဲ့ notes ရဲ့ ဒုတိယ page ကို ပြပါတယ်။ /notes?page=abc၊ /notes?page=-3 နဲ့ /notes?page=2.5 အားလုံး page 1 ကို ပြပြီး /notes?q=a&q=b က "a" ကို ရှာပါတယ်။Client Component ကနေ URL ကို update လုပ်ခြင်း
Search box က စာရိုက်တာကို တုံ့ပြန်ရလို့ Client Component ဖြစ်ရပါတယ်။ သူ့အလုပ်က data ကို ယူဖို့ မဟုတ်ပါဘူး၊ URL ကို ပြောင်းဖို့ပါပဲ။ Keystroke တိုင်း server ကို မခေါ်မိအောင် debounce လုပ်ပြီး history ထဲ entry အများကြီး မပြည့်အောင် push အစား replace ကို သုံးပါတယ်။ Search အသစ်က page 1 ကနေ စရမှာမို့ page param ကိုလည်း ဖျက်ပါတယ်။
"use client";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useRef } from "react";
export function SearchBox() {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
function onChange(value: string) {
clearTimeout(timer.current);
timer.current = setTimeout(() => {
const params = new URLSearchParams(searchParams.toString());
if (value.trim()) params.set("q", value.trim());
else params.delete("q");
params.delete("page"); // a new search starts at page 1
router.replace(`${pathname}?${params.toString()}`);
}, 300);
}
return (
<input
type="search"
aria-label="Search notes"
defaultValue={searchParams.get("q") ?? ""}
onChange={(e) => onChange(e.target.value)}
/>
);
}useSearchParams နဲ့ Suspense
Static render ဖြစ်တဲ့ route ထဲမှာ useSearchParams သုံးတဲ့ component ကို <Suspense> နဲ့ မထုပ်ထားရင် Next.js က build အချိန်မှာ သတိပေးချက် ဒါမှမဟုတ် error ပြနိုင်ပါတယ်။ ဒီ page က searchParams ကို ဖတ်ပြီးသားမို့ dynamic ဖြစ်ပေမဲ့ သင့် version ရဲ့ useSearchParams docs ကို စစ်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
URL ထဲက state က 'ဒီ မြင်ကွင်းကို ပြန်ဖွင့်လို့ ရရမယ်' ဆိုတဲ့ နေရာတိုင်းမှာ အသုံးဝင်ပါတယ်။ Customer support ticket ထဲကို link တစ်ခု ကူးထည့်လိုက်ရုံနဲ့ ဝန်ထမ်းက customer မြင်ခဲ့တဲ့ filter အတိအကျကို မြင်ရပါတယ်။
- Online ဆိုင်: ?category=shoes&size=42&sort=price-asc လို filter တွေက bookmark လုပ်လို့ရပြီး search engine တွေကလည်း filter page တွေကို တွေ့နိုင်ပါတယ်။
- Admin table: ?status=pending&page=3 လို URL ကြောင့် record တစ်ခုကို ဖွင့်ပြီး back နှိပ်ရင် page 3 ကို ပြန်ရောက်ပါတယ်။
- Analytics dashboard: ?from=2026-01-01&to=2026-01-31 လို date range ကို အစည်းအဝေးမှာ link နဲ့ မျှဝေနိုင်ပါတယ်။
Production မှာ team အများစုက parse function ကို lib/filters.ts လို file တစ်ခုထဲ ထားပြီး page, Route Handler နဲ့ test တွေက အဲဒီ function တစ်ခုတည်းကို မျှသုံးကြပါတယ်။ ဒါကြောင့် API endpoint ကနေ ?page=abc ရောက်လာရင်လည်း page ကနေ ရောက်လာတာနဲ့ အတူတူ ကိုင်တွယ်ပါတယ်။ Analytics မှာ ဘယ် filter တွေကို လူသုံးများလဲဆိုတာကိုလည်း URL ကနေ တိုက်ရိုက် ဖတ်လို့ ရပါတယ်။
အမှားများ
Search params နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
URL value ကို database query ထဲ တိုက်ရိုက် ထည့်ခြင်း
Sort column၊ page offset ဒါမှမဟုတ် filter field ကို URL ကနေ မစစ်ဘဲ သုံးရင် NaN error၊ ကြီးမားလွန်းတဲ့ query နဲ့ မရည်ရွယ်ထားတဲ့ column တွေကို ဖွင့်ပေးမိပါတယ်။ Allowlist နဲ့ number check ကို သုံးပါ။
Value တိုင်းကို string လို့ ယူဆခြင်း
?q=a&q=b ကို ပို့လိုက်ရင် params.q က array ဖြစ်ပြီး params.q.trim() က 'trim is not a function' နဲ့ page crash ဖြစ်ပါတယ်။ Type ကို string | string[] | undefined အဖြစ် ထားပြီး first() လို helper နဲ့ ကိုင်တွယ်ပါ။
Filter ပြောင်းပေမဲ့ page ကို reset မလုပ်ခြင်း
Page 7 မှာ ရှိနေတုန်း keyword အသစ် ရိုက်လိုက်ရင် ရလဒ်အသစ်မှာ page 7 မရှိလို့ list အလွတ် ပေါ်ပါတယ်။ Filter ပြောင်းတိုင်း page param ကို ဖျက်ပါ။
Keystroke တိုင်း router.push ခေါ်ခြင်း
စာလုံးတိုင်းအတွက် history entry တစ်ခုစီ ထည့်လို့ Back ကို ဆယ်ခါ နှိပ်ရပြီး request လည်း အများကြီး ထွက်ပါတယ်။ Debounce နဲ့ router.replace ကို သုံးပါ။
Best Practices
Best practices
- URL key တွေကို တိုတိုနဲ့ တည်ငြိမ်အောင် ထားပါ (q, page, sort)၊ ပြောင်းလိုက်ရင် user တွေ bookmark လုပ်ထားတဲ့ link တွေ ပျက်ပါတယ်။
- Default တန်ဖိုးတွေကို URL ထဲ မထည့်ပါနဲ့၊ ဒါမှ /notes နဲ့ /notes?page=1 လို URL နှစ်ခု ထပ်မဖြစ်ပါဘူး။
- Sort နဲ့ filter field တိုင်းအတွက် allowlist ရေးပြီး allowlist ထဲ မပါတာကို ဆိတ်ဆိတ်နေ default ပြောင်းပါ။
- Page တစ်ခုမှာ ပြမယ့် item အရေအတွက်ကို server ဘက်မှာ ကန့်သတ်ပါ၊ ?limit=100000 ကို ယုံမထားပါနဲ့။
Mini challenge
Mini challenge: parseFilters နဲ့ toQueryString
Tutorial list page အတွက် raw search params ကို typed Filters အဖြစ် ပြောင်းတဲ့ parseFilters နဲ့ Filters ကနေ URL query string ပြန်ထုတ်တဲ့ toQueryString ကို ရေးပါ။ Default တန်ဖိုးတွေကို URL ထဲ မထည့်ရပါဘူး။
- Key ထပ်နေရင် ပထမ value ကို ယူပါ
- q ကို trim လုပ်ပြီး စာလုံး 50 ထက် မပိုစေပါနဲ့
- difficulty က beginner, intermediate, advanced ထဲက တစ်ခု မဟုတ်ရင် "all" ဖြစ်ရမယ်
- page က အပေါင်း integer မဟုတ်ရင် 1 ဖြစ်ရမယ်
- toQueryString က default (q အလွတ်၊ all၊ page 1) တွေကို ချန်ထားရမယ်
type RawParams = { [key: string]: string | string[] | undefined };
type Difficulty = "all" | "beginner" | "intermediate" | "advanced";
type Filters = { q: string; difficulty: Difficulty; page: number };
const LEVELS = ["beginner", "intermediate", "advanced"] as const;
function parseFilters(raw: RawParams): Filters {
// TODO: take the first value when a key is repeated
// TODO: q -> trimmed, at most 50 characters
// TODO: difficulty -> one of LEVELS, otherwise "all"
// TODO: page -> positive integer, otherwise 1
}
function toQueryString(filters: Filters): string {
// TODO: use URLSearchParams and skip default values
}
// TODO: print the result for a normal input and a few hostile onesရလဒ်: ပထမ sample က ?q=hooks&difficulty=beginner&page=2 ကို ထုတ်ပါတယ်။ "expert" နဲ့ "-1" က default ဖြစ်သွားပြီး ["react", "vue"] က "react" ဖြစ်ကာ page 2.5 က 1 ဖြစ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
LEVELS ကို readonly string[] အဖြစ် ယူပြီး includes နဲ့ စစ်နိုင်ပါတယ်၊ ဒါမှမဟုတ် value is Difficulty ပြန်ပေးတဲ့ type guard ရေးပါ။ URLSearchParams.toString() က encoding ကို သင့်ကိုယ်စား လုပ်ပေးပါတယ်။
အဖြေ ကြည့်ရန်
type RawParams = { [key: string]: string | string[] | undefined };
type Difficulty = "all" | "beginner" | "intermediate" | "advanced";
type Filters = { q: string; difficulty: Difficulty; page: number };
const LEVELS = ["beginner", "intermediate", "advanced"] as const;
const firstValue = (v: string | string[] | undefined) => (Array.isArray(v) ? v[0] : v);
function isLevel(value: string | undefined): value is (typeof LEVELS)[number] {
return (LEVELS as readonly (string | undefined)[]).includes(value);
}
function parseFilters(raw: RawParams): Filters {
const q = (firstValue(raw.q) ?? "").trim().slice(0, 50);
const level = firstValue(raw.difficulty);
const n = Number(firstValue(raw.page) ?? "1");
return {
q,
difficulty: isLevel(level) ? level : "all",
page: Number.isInteger(n) && n > 0 ? n : 1,
};
}
function toQueryString(filters: Filters): string {
const params = new URLSearchParams();
if (filters.q) params.set("q", filters.q);
if (filters.difficulty !== "all") params.set("difficulty", filters.difficulty);
if (filters.page !== 1) params.set("page", String(filters.page));
const text = params.toString();
return text ? `?${text}` : "(no query)";
}
const samples: RawParams[] = [
{ q: " hooks ", difficulty: "beginner", page: "2" },
{ difficulty: "expert", page: "-1" },
{ q: ["react", "vue"], page: "2.5" },
{ q: "server actions", difficulty: "advanced" },
];
for (const raw of samples) {
const filters = parseFilters(raw);
console.log(JSON.stringify(filters), "->", toQueryString(filters));
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Search, filter နဲ့ page state ကို URL ထဲ ထားရင် refresh, back button နဲ့ link မျှဝေခြင်း အလုပ်လုပ်ပါတယ်။
- Next.js 15 ကစပြီး searchParams က Promise ဖြစ်ပြီး value တွေက string | string[] | undefined ဖြစ်ပါတယ်။
- Raw value တွေကို allowlist, integer check နဲ့ default တွေနဲ့ တစ်နေရာတည်းမှာ normalize လုပ်ပါ။
- Client ဘက်မှာ URL ကိုပဲ ပြောင်းပါ: debounce + router.replace၊ filter ပြောင်းရင် page ကို reset လုပ်ပါ။
Next.js — Fetching Data — Next.js
Next.js — Fetching Data — Next.js