Thuta Learning
React
AdvancedWeb Developmentအလယ်အလတ်

React Router Intro

Reactသင်ခန်းစာ 1825 မိနစ်

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

  • Multi-page website နဲ့ single-page app ရဲ့ navigation ကွာခြားချက်ကို ရှင်းပြနိုင်ဖို့
  • history.pushState နဲ့ popstate event က URL နဲ့ UI ကို ဘယ်လို ချိတ်ပေးလဲ နားလည်ဖို့
  • Page ကို state variable နဲ့ပဲ ပြောင်းရင် refresh, Back button, link share တွေ ဘာကြောင့် ပျက်လဲ ရှင်းပြနိုင်ဖို့
  • React Router v7 ရဲ့ package နာမည်နဲ့ အဲဒီ library က ဘာတွေ ကိုင်တွယ်ပေးလဲ သိဖို့

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

Client-side routing ကို ရိုးရိုးလေး ပြောရရင်

ရိုးရိုး website မှာ link တစ်ခု နှိပ်တိုင်း browser က server ဆီက HTML page အသစ်တစ်ခုလုံးကို တောင်းပြီး screen တစ်ခုလုံးကို ပြန်ဆွဲပါတယ်။ React app တွေကတော့ များသောအားဖြင့် HTML page တစ်ခုတည်း ရှိပြီး Home, About, Dashboard စတဲ့ screen တွေကို JavaScript နဲ့ component လဲပေးရုံပါပဲ။ ဒီလို page refresh မပါဘဲ screen ပြောင်းတာကို client-side routing လို့ ခေါ်ပါတယ်။

အရေးကြီးတဲ့ အချက်က address bar ထဲက URL လည်း တစ်ပြိုင်နက် ပြောင်းရတာပါ။ URL က 'ဒီ screen ရဲ့ လိပ်စာ' ဖြစ်လို့ bookmark လုပ်တာ၊ သူငယ်ချင်းဆီ link ပို့တာ၊ Back button နှိပ်တာ အားလုံး အလုပ်လုပ်ဖို့ လိုပါတယ်။ React မှာ ဒီအလုပ်ကို များသောအားဖြင့် React Router library နဲ့ လုပ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: state နဲ့ပဲ page ပြောင်းထားတဲ့ app

Router မသိခင် page ပြောင်းဖို့ ပထမဆုံး စဉ်းစားမိတာက page ဆိုတဲ့ state variable တစ်ခုပါ။ Button နှိပ်ရင် page ပြောင်းပြီး မှန်ကန်တဲ့ component ကို ပြပါတယ်၊ ပထမကြည့်ရင် အဆင်ပြေပါတယ်။ ဒါပေမဲ့ URL က အမြဲ / ပဲ ရှိနေလို့ About page မှာ refresh လုပ်ရင် Home ကို ပြန်ရောက်သွားပါတယ်၊ Back button နှိပ်ရင် app ထဲက page အဟောင်းကို မရောက်ဘဲ website ထဲကနေ လုံးဝ ထွက်သွားပါတယ်၊ About page ရဲ့ link ကို တခြားသူကို ပို့လို့လည်း မရပါဘူး။

StateOnlyPages.jsx
function App() {
  const [page, setPage] = useState('home'); // the URL never changes

  return (
    <>
      <button onClick={() => setPage('home')}>Home</button>
      <button onClick={() => setPage('about')}>About</button>
      {page === 'home' ? <Home /> : <About />}
    </>
  );
}
// Refresh on About -> Home again. Back button -> leaves the site.

URL ကို ဘာကြောင့် source of truth လုပ်ရလဲ

Browser က refresh, Back/Forward, bookmark, link share အားလုံးကို URL နဲ့ပဲ မှတ်ပါတယ်၊ သင့် component ထဲက state ကို မသိပါဘူး။ ဒါကြောင့် 'ဘယ် screen ပြမလဲ' ဆိုတဲ့ အချက်ကို state ထဲမှာ မဟုတ်ဘဲ URL ထဲမှာ ထားပြီး UI က URL ကို ဖတ်ပြီး ဆွဲရပါမယ်။ Client-side routing ရဲ့ အလုပ်က အဲဒီ ချိတ်ဆက်မှုကို page refresh မပါဘဲ ထိန်းပေးဖို့ပါပဲ။

Core concept: History API နဲ့ URL-UI ချိတ်ဆက်မှု

Browser ရဲ့ History API မှာ client-side routing အတွက် အခြေခံ အစိတ်အပိုင်း နှစ်ခု ပါပါတယ်။ window.history.pushState(state, '', '/about') က address bar ကို /about ပြောင်းပြီး browser history ထဲကို entry အသစ်တစ်ခု ထည့်ပေမဲ့ server ဆီ request မပို့ပါဘူး၊ page လည်း reload မဖြစ်ပါဘူး။ User က Back ဒါမှမဟုတ် Forward နှိပ်တဲ့အခါမှာတော့ browser က URL ကို ပြောင်းပြီး window ပေါ်မှာ popstate event ကို ပစ်ပါတယ်။

Router တစ်ခုက အဲဒီ နှစ်ခုကို ပေါင်းပြီး loop တစ်ခု ဖန်တီးပါတယ်: link နှိပ်ရင် ပုံမှန် navigation ကို preventDefault နဲ့ တားပြီး pushState ခေါ်ကာ React ကို re-render ခိုင်းပါတယ်။ popstate ဖြစ်ရင် URL အသစ်ကို ဖတ်ပြီး re-render ခိုင်းပါတယ်။ Render တိုင်းမှာ router က လက်ရှိ pathname ကို route list နဲ့ တိုက်စစ် (match) ပြီး ကိုက်ညီတဲ့ component ကို ပြပါတယ်၊ ဘာနဲ့မှ မကိုက်ရင် 'not found' screen ကို ပြပါတယ်။

Server ဘက်ကလည်း တစ်ခု သတိထားရပါမယ်။ User က /about ကို တိုက်ရိုက် ဖွင့်တာ ဒါမှမဟုတ် အဲဒီမှာ refresh လုပ်တာက ပုံမှန် HTTP request ဖြစ်လို့ server က /about file ကို ရှာပါတယ်။ SPA မှာ အဲဒီ file မရှိလို့ path တိုင်းအတွက် index.html ကို ပြန်ပေးရပြီး ကျန်တာကို JavaScript router က ဆက်လုပ်ပါတယ်။ Vite dev server က ဒါကို အလိုလို လုပ်ပေးပေမဲ့ production hosting မှာ 'SPA fallback' ဒါမှမဟုတ် rewrite rule ကို ကိုယ်တိုင် ပြင်ဆင်ရလေ့ ရှိပါတယ်။

React Router က ဒီ loop တစ်ခုလုံးကို သင့်ကိုယ်စား လုပ်ပေးပြီး nested layout, URL parameter, search parameter, navigation hook တွေကိုလည်း ထည့်ပေးပါတယ်။ Version 7 မှာ package နာမည်က react-router ဖြစ်ပြီး ယခင် react-router-dom ကတော့ upgrade လုပ်သူတွေအတွက် react-router ကို re-export ပြန်လုပ်ပေးတဲ့ package အဖြစ် ဆက်ရှိပါတယ်။ v7 ကို 'declarative', 'data', 'framework' ဆိုတဲ့ mode သုံးမျိုးနဲ့ သုံးနိုင်ပြီး ဒီ tutorial မှာတော့ <BrowserRouter> နဲ့ <Routes> ကို သုံးတဲ့ declarative mode ကို သင်ပါမယ်။

URL က အမိန့်ပေးတယ်၊ UI က လိုက်ဆွဲတယ်
  1. Link နှိပ် -> preventDefault
  2. pushState က URL ကို ပြောင်း
  3. Router က pathname ကို route နဲ့ တိုက်
  4. ကိုက်တဲ့ component ကို render
  5. Back -> popstate -> ပြန် match

ဘယ် screen ပြမလဲဆိုတာကို state ထဲ မထားပါနဲ့ — URL ကနေ အမြဲ တွက်ထုတ်ပါ။

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

jsx
// 1. On a link click: stop the page load, change the URL, re-render
event.preventDefault();
window.history.pushState(null, '', newPath);
setPath(newPath);

// 2. On Back/Forward: read the URL the browser restored
window.addEventListener('popstate', () => setPath(window.location.pathname));

// 3. On every render: pick the screen for the current path
const screen = routes[path] ?? notFoundScreen;

Router တိုင်း (React Router အပါအဝင်) ရဲ့ အတွင်းပိုင်းမှာ ဒီ အဆင့်သုံးဆင့် ရှိပါတယ်: navigation ကို ကြားဖြတ်ခြင်း၊ history ကို နားထောင်ခြင်းနဲ့ path ကို screen တစ်ခုနဲ့ match လုပ်ခြင်းပါ။

Worked example: router သေးသေးလေး ကိုယ်တိုင် ဆောက်ခြင်း

အောက်က component က library မပါဘဲ client-side routing ရဲ့ အခြေခံ အလုပ်ကို လုပ်ပါတယ်။ Production မှာ ဒီလို ကိုယ်တိုင် မရေးပါဘူး — nested route, parameter, scroll restoration လို အသေးစိတ်တွေ အများကြီး ကျန်ပါသေးတယ်။ ဒါပေမဲ့ ဒါကို နားလည်ထားရင် နောက် lesson မှာ React Router ရဲ့ <Link> နဲ့ <Routes> တွေက နောက်ကွယ်မှာ ဘာလုပ်နေလဲ ရှင်းရှင်း မြင်ရပါမယ်။

TinyRouter.jsx
import { useEffect, useState } from 'react';

const routes = {
  '/': <h1>Home</h1>,
  '/about': <h1>About us</h1>,
};

export default function TinyRouter() {
  const [path, setPath] = useState(window.location.pathname);

  useEffect(() => {
    const onPopState = () => setPath(window.location.pathname);
    window.addEventListener('popstate', onPopState); // Back / Forward
    return () => window.removeEventListener('popstate', onPopState);
  }, []);

  function navigate(event, to) {
    event.preventDefault(); // stop the full page load
    window.history.pushState(null, '', to); // update the address bar
    setPath(to); // re-render with the matching page
  }

  return (
    <>
      <nav>
        <a href="/" onClick={(e) => navigate(e, '/')}>Home</a> |{' '}
        <a href="/about" onClick={(e) => navigate(e, '/about')}>About</a>
      </nav>
      {routes[path] ?? <h1>Page not found</h1>}
    </>
  );
}

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

  1. Path in state, read from the URLconst [path, setPath] = useState(window.location.pathname);

    Page ကို 'home' လို နာမည်နဲ့ မမှတ်ဘဲ browser ရဲ့ pathname ကနေ စပါတယ်။ ဒါကြောင့် /about ကို တိုက်ရိုက် ဖွင့်ရင်လည်း About ကို ပြပါတယ်။

  2. Listen for Back and Forwardwindow.addEventListener('popstate', onPopState); // Back / Forward

    Back/Forward နှိပ်ရင် browser က URL ကို ပြောင်းပြီး popstate ကို ပစ်ပါတယ်၊ handler က path state ကို URL နဲ့ ပြန်ညှိပါတယ်။ Cleanup က listener ကို ဖယ်ရှားပါတယ်။

  3. Intercept the clickevent.preventDefault(); // stop the full page load

    <a href> ကို ဆက်သုံးထားလို့ link က ပုံမှန် link လို ပေါ်ပြီး right-click 'open in new tab' လည်း ရပါတယ်။ preventDefault က browser ရဲ့ full page load ကိုပဲ တားပါတယ်။

  4. Change the URL without reloadingwindow.history.pushState(null, '', to); // update the address bar

    Address bar ကို ပြောင်းပြီး history entry အသစ် ထည့်ပါတယ်၊ ဒါကြောင့် Back က ယခင် screen ကို ပြန်ရောက်ပါတယ်။ ပြီးမှ setPath(to) က React ကို re-render ခိုင်းပါတယ်။

  5. Match the path{routes[path] ?? <h1>Page not found</h1>}

    routes object ထဲမှာ path ကို ရှာပြီး မတွေ့ရင် not-found heading ကို ပြပါတယ်။ React Router ကတော့ /users/:id လို pattern တွေကိုပါ match လုပ်နိုင်ပါတယ်။

You should see
/ မှာ nav နဲ့ 'Home' heading ကို ပြပါတယ်။ About ကို နှိပ်ရင် page reload မဖြစ်ဘဲ address bar က /about ဖြစ်ပြီး heading က 'About us' ဖြစ်သွားပါတယ်။ Back နှိပ်ရင် / နဲ့ 'Home' ကို ပြန်ရောက်ပါတယ်။ /blog လို တခြား path မှာတော့ 'Page not found' ကို ပြပါတယ်။

Multi-page site နဲ့ SPA ကို ယှဉ်ကြည့်ခြင်း

Client-side routing က အခမဲ့ မရပါဘူး၊ ရွေးချယ်မှု တစ်ခုစီမှာ အားသာချက်နဲ့ အားနည်းချက် ရှိပါတယ်။ အောက်က table က link တစ်ခု နှိပ်လိုက်တဲ့အခါ နည်းလမ်း နှစ်ခုမှာ ဘာဖြစ်လဲဆိုတာ ယှဉ်ပြထားပါတယ်။

အချက်Multi-page siteSPA + client router
Link နှိပ်ရင်Server က HTML အသစ် ပို့JavaScript က component လဲ
Memory ထဲက statePage တိုင်း ပျောက်Navigation ကြား ကျန်
ပထမ loadHTML ချက်ချင်း ပြJavaScript bundle ကို စောင့်ရ
Server setupPath တိုင်း file/routePath တိုင်း index.html

ပထမ load နှေးတာနဲ့ search engine အတွက် HTML အလွတ် ဖြစ်နေတာကို ဖြေရှင်းဖို့ framework တချို့က page ကို server ပေါ်မှာ ကြိုတင် render လုပ်ပြီး ပြီးမှ client-side routing ကို ဆက်သုံးပါတယ်။ React Router ရဲ့ framework mode ကလည်း ဒီလမ်းကြောင်းကို ရွေးပါတယ်၊ ဒါပေမဲ့ routing အခြေခံ — URL ကနေ screen ကို တွက်ထုတ်တာ — ကတော့ အတူတူပါပဲ။

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

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

Dashboard, online shop, admin panel လို React app တော်တော်များများက client-side routing ကို သုံးပါတယ်။ ဒါကြောင့် screen ပြောင်းတာ မြန်ပြီး shopping cart လို state တွေ ပျက်မသွားပါဘူး။ Project အစမှာ routing ကို စဉ်းစားရင် အောက်က အချက်တွေကို ဆုံးဖြတ်ရလေ့ ရှိပါတယ်။

  • URL ဒီဇိုင်း: /products/42 လို ဖတ်ရလွယ်ပြီး share လုပ်လို့ရတဲ့ URL တွေကို ကြိုစီစဉ်ပါ၊ URL တွေက user တွေ bookmark လုပ်ထားမယ့် public API တစ်ခုလိုပါပဲ။
  • Hosting: production server ဒါမှမဟုတ် static host မှာ path တိုင်းအတွက် index.html ကို ပြန်ပေးတဲ့ fallback rule ကို ပြင်ဆင်ပါ၊ မဟုတ်ရင် refresh တိုင်း 404 ရပါမယ်။
  • Router mode: SPA ရိုးရိုးအတွက် declarative mode လုံလောက်ပါတယ်၊ route တစ်ခုစီအတွက် data loading လိုရင် data mode ဒါမှမဟုတ် framework mode ကို React Router docs မှာ လေ့လာကြည့်ပါ။
  • Accessibility: screen ပြောင်းတိုင်း page title ကို update လုပ်ပြီး focus ကို content အသစ်ဆီ ရွှေ့ပေးတာက screen reader သုံးသူတွေအတွက် အရေးကြီးပါတယ်။

အမှားများ

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

Screen ကို state နဲ့ပဲ ပြောင်းခြင်း

Problem section ထဲကလို page state နဲ့ပဲ screen ပြောင်းရင် refresh, Back, bookmark တွေ အလုပ်မလုပ်ပါဘူး။ User တွေက browser ရဲ့ Back button ကို အမြဲ သုံးလို့ ဒါက 'app ပျက်နေတယ်' လို ခံစားရပါတယ်။ ဘယ် screen ပြမလဲဆိုတာ URL ထဲ ထားပါ။

Server fallback ကို မေ့ခြင်း

Development မှာ အားလုံး အဆင်ပြေပေမဲ့ deploy လုပ်ပြီး /about မှာ refresh လုပ်လိုက်တော့ hosting ရဲ့ 404 page ပေါ်လာပါတယ်။ Code bug မဟုတ်ပါဘူး — server က /about file ကို ရှာနေတာပါ။ Host ရဲ့ docs အတိုင်း path အားလုံးကို index.html ဆီ rewrite လုပ်ပါ။

Package မထည့်ဘဲ import လုပ်ခြင်း ဒါမှမဟုတ် package နာမည် ရောခြင်း

npm install မလုပ်ခင် import { Link } from 'react-router' လို့ ရေးရင် Vite က 'Failed to resolve import' လို error ပြပါတယ်။ v7 ကို သုံးရင် import တွေကို react-router ကနေ တစ်နေရာတည်း လုပ်ပါ၊ file တချို့မှာ react-router-dom၊ တချို့မှာ react-router ရောရေးတာက version နှစ်ခု install ဖြစ်နေတာကို ဖုံးကွယ်ထားနိုင်ပါတယ်။

Mini challenge

Mini challenge: Link component ကိုယ်တိုင် ရေးခြင်း

React Router ရဲ့ <Link> က ပုံမှန် click ကိုပဲ ကြားဖြတ်ပြီး Ctrl/Cmd-click လို 'tab အသစ်မှာ ဖွင့်' action တွေကို browser ဆီ ချန်ထားပါတယ်။ Tiny router အတွက် အဲဒီလို Link component တစ်ခုနဲ့ pathname hook တစ်ခု ရေးပြီး Home, Pricing, Contact page သုံးခုရှိတဲ့ App ကို ဆောက်ပါ။

  • usePathname() က [path, setPath] ကို ပြန်ပေးပြီး popstate ကို နားထောင်ရမယ်
  • Link က <a href={to}> ကို render လုပ်ရမယ်
  • Ctrl, Meta, Shift, Alt နှိပ်ထားရင် ဒါမှမဟုတ် left button မဟုတ်ရင် ဘာမှ မလုပ်ဘဲ browser ကို ချန်ထားရမယ်
  • ပုံမှန် click မှာ preventDefault, pushState လုပ်ပြီး onNavigate(to) ကို ခေါ်ရမယ်၊ မသိတဲ့ path အတွက် 'Page not found' ပြရမယ်
LinkRouter.jsx
import { useEffect, useState } from 'react';

function usePathname() {
  // TODO: path state from window.location.pathname
  // TODO: popstate listener (with cleanup) that syncs the state
  // TODO: return [path, setPath]
}

function Link({ to, onNavigate, children }) {
  function handleClick(event) {
    // TODO: leave modified clicks (ctrl/meta/shift/alt) and non-left buttons alone
    // TODO: otherwise preventDefault, pushState, then onNavigate(to)
  }
  return null; // TODO: a real <a> with href and onClick
}

const titles = { '/': 'Home', '/pricing': 'Pricing', '/contact': 'Contact' };

export default function App() {
  // TODO: use the hook, render three Links and an <h1> for the current page
  return null;
}

ရလဒ်: Pricing ကို ပုံမှန် နှိပ်ရင် URL က /pricing ဖြစ်ပြီး heading က Pricing ဖြစ်ပါတယ်၊ page reload မဖြစ်ပါဘူး။ Ctrl-click (Mac မှာ Cmd-click) လုပ်ရင် လက်ရှိ tab မပြောင်းဘဲ browser က tab အသစ်မှာ ဖွင့်ပါတယ်။ Back နှိပ်ရင် ယခင် page ကို ပြန်ရောက်ပြီး /missing လို path မှာ 'Page not found' ကို ပြပါတယ်။

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

MouseEvent မှာ event.ctrlKey, event.metaKey, event.shiftKey, event.altKey နဲ့ event.button (left button = 0) တွေ ပါပါတယ်။ Return early လုပ်လိုက်ရင် preventDefault မခေါ်တဲ့အတွက် browser ရဲ့ ပုံမှန် အပြုအမူ ဆက်ဖြစ်ပါတယ်။

အဖြေ ကြည့်ရန်
LinkRouter.jsx
import { useEffect, useState } from 'react';

function usePathname() {
  const [path, setPath] = useState(window.location.pathname);
  useEffect(() => {
    const sync = () => setPath(window.location.pathname);
    window.addEventListener('popstate', sync);
    return () => window.removeEventListener('popstate', sync);
  }, []);
  return [path, setPath];
}

function Link({ to, onNavigate, children }) {
  function handleClick(event) {
    const modified = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
    if (modified || event.button !== 0) return; // new tab/window: browser handles it
    event.preventDefault();
    window.history.pushState(null, '', to);
    onNavigate(to);
  }
  return <a href={to} onClick={handleClick}>{children}</a>;
}

const titles = { '/': 'Home', '/pricing': 'Pricing', '/contact': 'Contact' };

export default function App() {
  const [path, setPath] = usePathname();
  return (
    <>
      <nav>
        <Link to="/" onNavigate={setPath}>Home</Link>{' '}
        <Link to="/pricing" onNavigate={setPath}>Pricing</Link>{' '}
        <Link to="/contact" onNavigate={setPath}>Contact</Link>
      </nav>
      <h1>{titles[path] ?? 'Page not found'}</h1>
    </>
  );
}

Quick Quiz 1 / 3

window.history.pushState(null, '', '/about') ကို ခေါ်လိုက်ရင် ဘာဖြစ်လဲ?

Quick Quiz 2 / 3

React SPA ကို static host ပေါ် deploy လုပ်ပြီး /about မှာ refresh လုပ်တော့ host ရဲ့ 404 page ပေါ်လာတယ်။ ဖြစ်နိုင်ခြေ အများဆုံး အကြောင်းရင်းက ဘာလဲ?

Quick Quiz 3 / 3

const [page, setPage] = useState('home') နဲ့ screen ပြောင်းထားတဲ့ app ရဲ့ အဓိက ပြဿနာက ဘာလဲ?

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

  • Client-side routing က page reload မပါဘဲ URL နဲ့ ပြတဲ့ component ကို ချိတ်ပေးပါတယ်
  • pushState က URL ကို ပြောင်းပြီး popstate က Back/Forward ကို သတင်းပို့ပါတယ်၊ router က render တိုင်း path ကို route နဲ့ match လုပ်ပါတယ်
  • ဘယ် screen ပြမလဲဆိုတာ URL ထဲမှာ ထားပါ၊ state တစ်ခုတည်းနဲ့ဆိုရင် refresh, Back, bookmark ပျက်ပါတယ်
  • Production server မှာ path တိုင်းအတွက် index.html ပြန်ပေးတဲ့ fallback လိုပါတယ်
  • React Router v7 ရဲ့ package က react-router ဖြစ်ပြီး ဒီ tutorial မှာ declarative mode ကို သုံးပါမယ်

React Router Documentation — React Router

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

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

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

React Router Intro | Thuta Learning