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

Conditional Rendering

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

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

  • Loading/error လို အခြေအနေတွေအတွက် JSX မတိုင်ခင် if နဲ့ early return လုပ်နိုင်ဖို့
  • နှစ်မျိုးထဲက တစ်မျိုး ပြဖို့ ternary ကို၊ ပြမလား မပြဘူးလား အတွက် && ကို ရွေးသုံးနိုင်ဖို့
  • {count && ...} က 0 ကို screen ပေါ် ထုတ်ပြတဲ့ bug ကို ရှင်းပြပြီး ပြင်နိုင်ဖို့
  • null, false, undefined က ဘာမှ မ render ဘဲ 0 နဲ့ string က render ဖြစ်ကြောင်း ခွဲခြားနိုင်ဖို့

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

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

စာကြည့်တိုက်ဝင်ပေါက်က ဆိုင်းဘုတ်တစ်ခုကို စဉ်းစားကြည့်ပါ။ ဖွင့်ချိန်ဆိုရင် "Welcome" ပြပြီး ပိတ်ချိန်ဆိုရင် "Closed, come back at 9" ပြပါတယ်။ ဆိုင်းဘုတ် ကိုယ်တိုင်က တူတူပဲ၊ အချိန် (condition) ပေါ်မူတည်ပြီး စာသားပဲ ပြောင်းတာပါ။

React component တွေလည်း ဒီလိုပါပဲ။ Component က props နဲ့ state ကို ကြည့်ပြီး ဘယ် JSX ကို return ပြန်မလဲ ဆုံးဖြတ်ပါတယ်။ ဒီအတွက် React ရဲ့ special syntax မလိုပါဘူး — JavaScript ရဲ့ if, ? : နဲ့ && ကိုပဲ သုံးတာပါ။

Problem → Why → Solution

ပြဿနာ: badge ပေါ်မှာ မလိုအပ်တဲ့ 0 ပေါ်နေတယ်

Unread message ရှိမှ badge ပြချင်လို့ အောက်က code ကို ရေးလိုက်ပါတယ်။ Message ရှိတဲ့အခါ အလုပ်ဖြစ်ပေမယ့် unread က 0 ဖြစ်သွားတဲ့အခါ badge ပျောက်သွားရမယ့်အစား Inbox ဘေးမှာ "0" ဆိုတဲ့ ဂဏန်းတစ်လုံး ပေါ်နေပါတယ်။ Mobile layout မှာဆိုရင် ဒီ 0 က icon တွေကိုတောင် နေရာရွှေ့သွားစေပါတယ်။

InboxLink.jsx
function InboxLink({ unread }) {
  return (
    <a href="/inbox">
      Inbox
      {unread && <span className="badge">{unread}</span>}
    </a>
  );
}

// <InboxLink unread={0} />  renders: <a href="/inbox">Inbox0</a>

ဘာကြောင့် 0 ပေါ်လာတာလဲ

JavaScript ရဲ့ a && b က a ဟာ falsy ဖြစ်ရင် b ကို မကြည့်ဘဲ a ကိုယ်တိုင်ကို ပြန်ပေးပါတယ်။ unread က 0 ဆိုရင် expression တစ်ခုလုံးရဲ့ value က 0 ဖြစ်ပြီး React က number တွေကို text အဖြစ် render လုပ်ပါတယ်။ false, null, undefined ကိုသာ ဘာမှ မပြတာပါ။ {unread > 0 && ...} လို့ boolean ဖြစ်အောင် ရေးရင် ပြဿနာ ပြေလည်ပါတယ်။

Core concept: JSX ကို JavaScript နဲ့ ရွေးတယ်

JSX ထဲက { } ထဲမှာ expression ပဲ ထည့်လို့ရပြီး if လို statement ထည့်လို့ မရပါဘူး။ ဒါကြောင့် condition rendering မှာ နည်းလမ်း လေးမျိုး ရှိပါတယ်။ ပထမ နည်းက JSX မတိုင်ခင် if နဲ့ early return လုပ်တာပါ — if (isLoading) return <Spinner />; လို ရေးလိုက်ရင် component ရဲ့ ကျန်တဲ့ အပိုင်းက loading မဟုတ်တဲ့ အခြေအနေကိုပဲ စဉ်းစားရတော့ပါတယ်။ Loading, error, permission မရှိ လို screen တစ်ခုလုံး အစားထိုးရတဲ့ အခြေအနေတွေအတွက် ဒါက အရှင်းဆုံးပါ။

ဒုတိယ နည်းက ternary operator condition ? A : B ပါ။ JSX အလယ်မှာ နှစ်ခုထဲက တစ်ခု ပြဖို့ အသုံးဝင်ပါတယ် — ဥပမာ login ဝင်ထားရင် "Log out"၊ မဝင်ရသေးရင် "Log in"။ တတိယ နည်းက condition && A ပါ၊ "ပြမလား၊ ဘာမှ မပြဘူးလား" အတွက်ပါ။ && ရဲ့ ဘယ်ဘက်ကို အမြဲ boolean ဖြစ်အောင် ရေးပါ (count > 0, items.length > 0, Boolean(user))၊ မဟုတ်ရင် အပေါ်က 0 bug ကို ပြန်ကြုံရပါမယ်။

စတုတ္ထ နည်းက JSX ကို variable ထဲ သိမ်းတာပါ: let content = <Empty />; if (items.length) content = <List items={items} />; ပြီးမှ return ထဲမှာ {content} ကို ထည့်ပါ။ Ternary တွေ တစ်ခုထဲ တစ်ခု အဆင့်ဆင့် ထပ်လာပြီး ဖတ်ရခက်လာရင် ဒီပုံစံက ပိုရှင်းပါတယ်။

React က value အမျိုးအစားကို ဘယ်လို render လုပ်လဲ သိထားဖို့ လိုပါတယ်။ null, undefined, true, false က ဘာမှ မပြပါဘူး။ String နဲ့ number (0 နဲ့ NaN အပါအဝင်) ကတော့ text အဖြစ် ပေါ်ပါတယ်။ Component တစ်ခုလုံးကို ဘာမှ မ render စေချင်ရင် return null; ပြန်ပါ — ဒါပေမယ့် component ကို ဖျောက်ဖို့ parent က {show && <Panel />} နဲ့ ဆုံးဖြတ်တာက ပိုသဘာဝကျလေ့ ရှိပါတယ်။

နောက်ဆုံး သတိထားရမှာက condition က component ရဲ့ state ကိုလည်း ထိခိုက်တာပါ။ {show && <Panel />} မှာ show က false ဖြစ်သွားရင် Panel ကို tree ထဲကနေ ဖယ်ထုတ် (unmount) လိုက်ပြီး သူ့ state အားလုံး ပျောက်ပါတယ်၊ ပြန်ပေါ်လာရင် အစကနေ ပြန်စပါတယ်။ CSS နဲ့ ဖျောက်ထားတာကတော့ component က tree ထဲ ဆက်ရှိနေလို့ state မပျောက်ပါဘူး။

မှတ်ထားရမယ့် ပုံစံ
  1. Props နဲ့ state ကို ဖတ်
  2. Screen တစ်ခုလုံး ပြောင်းရင် early return
  3. အပိုင်းလေး ပြောင်းရင် ? : သို့ &&
  4. ကျန်တဲ့ JSX ကို return

&& ရဲ့ ဘယ်ဘက်က boolean ဖြစ်ရမယ်၊ number မဖြစ်ရပါဘူး။

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

jsx
function Component({ status, flag, count }) {
  if (status === 'loading') return <Fallback />;   // whole screen
  if (status === 'hidden') return null;             // render nothing
  return (
    <Wrapper>
      {/* either / or */}
      {flag ? <OptionA /> : <OptionB />}
      {/* show or nothing */}
      {count > 0 && <Extra count={count} />}
    </Wrapper>
  );
}

Screen တစ်ခုလုံး ပြောင်းတဲ့ အခြေအနေတွေကို အပေါ်ဆုံးမှာ early return နဲ့ ကိုင်ပါ။ JSX ထဲမှာတော့ နှစ်မျိုးထဲက တစ်မျိုးအတွက် ? : နဲ့၊ ပြမလား မပြဘူးလား အတွက် boolean && ကို သုံးပါ။

Example: login အခြေအနေအလိုက် greeting

Greeting component က isLoggedIn prop ကို ကြည့်ပြီး heading နှစ်ခုထဲက တစ်ခုကို ternary နဲ့ ရွေးပါတယ်။ App က isLoggedIn={true} ပို့ထားလို့ welcome heading ပေါ်ပါတယ်။

jsx
function Greeting({ isLoggedIn, userName }) {
  return (
    <div>
      {isLoggedIn ? (
        <h1>Welcome back, {userName}!</h1>
      ) : (
        <h1>Please sign in to continue.</h1>
      )}
    </div>
  );
}

function App() {
  return <Greeting isLoggedIn={true} userName="Sai" />;
}

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

  1. Line 1function Greeting({ isLoggedIn, userName }) {

    Condition က prop ကနေ လာပါတယ်။ Greeting က user ဘယ်လို login ဝင်လဲ မသိဘဲ ပေးတဲ့ value ကိုပဲ ကြည့်ပါတယ်၊ ဒါကြောင့် test လုပ်ရ လွယ်ပါတယ်။

  2. Line 4{isLoggedIn ? (

    { } ထဲမှာ expression ပဲ ရလို့ if အစား ternary ကို သုံးထားပါတယ်။ isLoggedIn true ဆိုရင် ? နောက်က JSX ကို ယူပါတယ်။

  3. Line 6) : (

    : နောက်က အပိုင်းက false ဖြစ်တဲ့အခါ ပြမယ့် JSX ပါ။ အပိုင်းနှစ်ခုလုံးမှာ <h1> ဖြစ်နေလို့ layout က မပြောင်းဘဲ စာသားပဲ ပြောင်းပါတယ်။

  4. Line 14return <Greeting isLoggedIn={true} userName="Sai" />;

    isLoggedIn={false} လို့ ပြောင်းကြည့်ပါ — "Please sign in to continue." ပေါ်လာပြီး userName ကို မသုံးတော့ပါဘူး။

You should see
Heading တစ်ခု ပေါ်ပါတယ်: Welcome back, Sai!
(Render ဖြစ်တဲ့ HTML: <div><h1>Welcome back, Sai!</h1></div>)

ဘယ်နည်းကို ဘယ်အချိန် သုံးမလဲ

နည်းလမ်းသုံးသင့်တဲ့ အချိန်သတိထားရမယ့်အချက်
if + early returnScreen တစ်ခုလုံး အစားထိုးရတဲ့အခါ (loading, error)Hook တွေကို return မတိုင်ခင် ခေါ်ထားရမယ်
cond ? A : Bနှစ်မျိုးထဲက တစ်မျိုးအဆင့်ဆင့် ထပ်ရင် ဖတ်ရခက်
cond && Aပြမလား ဘာမှ မပြဘူးလားဘယ်ဘက်က 0 ဆိုရင် 0 ပေါ်
JSX variableCase သုံးခုထက် များရင်let ကို တစ်နေရာထဲမှာ set လုပ်

Early return နဲ့ Hook

useState, useEffect လို Hook တွေကို if (loading) return ... ရဲ့ အောက်မှာ ခေါ်ရင် render တစ်ခုနဲ့ တစ်ခု Hook အရေအတွက် မတူတော့လို့ React က error ပြပါတယ်။ Hook အားလုံးကို component ရဲ့ ထိပ်ဆုံးမှာ ခေါ်ပြီးမှ early return လုပ်ပါ။

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

Real project တွေမှာ ဘယ်လို သုံးလဲ

Data ကို server ကနေ ယူတဲ့ screen တိုင်းမှာ အခြေအနေ လေးမျိုး ရှိပါတယ် — loading, error, empty, ready။ Production app တွေမှာ ဒီလေးမျိုးစလုံးအတွက် UI ကို သေချာ ဒီဇိုင်းဆွဲထားပါတယ်: loading အတွက် skeleton၊ error အတွက် "Try again" button ပါတဲ့ message၊ empty အတွက် "No orders yet" နဲ့ ပထမဆုံး order လုပ်ဖို့ link။ Happy path တစ်ခုတည်း ရေးပြီး ကျန်တာ မေ့သွားတာက user တွေ screen ဗလာ မြင်ရတဲ့ အဓိက အကြောင်းရင်းပါ။

Permission အလိုက် UI ပြတာလည်း အရမ်းအသုံးများပါတယ် — {user.role === 'admin' && <DeleteButton />}။ ဒါပေမယ့် button ကို ဖျောက်ထားတာက security မဟုတ်ပါဘူး၊ server ဘက်ကလည်း permission ကို ထပ်စစ်ရပါမယ်။ Feature flag တွေ (feature အသစ်ကို user တစ်ချို့ကိုပဲ ပြတာ) နဲ့ responsive layout အချို့ကလည်း ဒီ conditional rendering pattern တွေကိုပဲ သုံးပါတယ်။

အမှားများ

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

1. && ရဲ့ ဘယ်ဘက်မှာ number ထားတာ

{items.length && <List />} က array ဗလာဖြစ်ရင် 0 ကို ပြပါတယ်။ {items.length > 0 && <List />} လို့ ရေးပါ၊ ဒါမှမဟုတ် ternary နဲ့ empty state တစ်ခုကို တစ်ခါတည်း ပြပါ။

2. JSX ထဲမှာ if ရေးဖို့ ကြိုးစားတာ

<div>{if (ok) { <p>Yes</p> }}</div> က syntax error ပါ၊ { } ထဲမှာ statement ထည့်လို့ မရလို့ပါ။ if ကို return မတိုင်ခင် ရွှေ့ပါ ဒါမှမဟုတ် ternary/&& ကို သုံးပါ။

3. Ternary တွေကို အဆင့်ဆင့် ထပ်တာ

a ? <A /> : b ? <B /> : c ? <C /> : <D /> လို code က ဘယ် condition က ဘယ် UI လဲ ခွဲရခက်ပါတယ်။ Case သုံးခုထက် များရင် early return တွေ ဒါမှမဟုတ် JSX variable ကို သုံးပါ၊ ဒါမှမဟုတ် case တစ်ခုစီကို component သီးသန့် ခွဲပါ။

Best Practices

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

  • Condition ရှည်လာရင် JSX ထဲမှာ မရေးဘဲ const canCheckout = items.length > 0 && !isLocked; လို နာမည်ကောင်းတဲ့ variable တစ်ခုထဲ အရင် ထည့်ပါ။
  • Data ယူတဲ့ component အသစ်တိုင်းအတွက် loading, error, empty, ready လေးမျိုးကို checklist လုပ်ပြီး တစ်ခုချင်း ကြည့်ပါ။
  • Loading ပြီးတာနဲ့ layout ခုန်မသွားအောင် spinner အစား နေရာတူ skeleton ကို ရွေးစဉ်းစားပါ။
  • Branch တစ်ခုစီက ရှည်လာရင် <EmptyCart /> လို component သီးသန့် ခွဲပြီး parent မှာ ဘယ်ဟာ ပြမလဲ ဆုံးဖြတ်ရုံပဲ ထားပါ။

Mini challenge

Mini challenge: cart အခြေအနေ လေးမျိုး

status, items, error props တွေကို လက်ခံတဲ့ CartSummary component ရေးပြီး loading, error, empty, ready အခြေအနေ လေးမျိုးစလုံးကို မှန်မှန်ကန်ကန် ပြပါ။

  • loading နဲ့ error ကို JSX မတိုင်ခင် early return နဲ့ ကိုင်ရမယ်
  • Item မရှိရင် "Your cart is empty." နဲ့ ရှိရင် item အရေအတွက်ကို ternary နဲ့ ပြရမယ်
  • Item သုံးခုနဲ့အထက်ဆိုရင်ပဲ "Bulk discount: 10%" ပြရမယ်၊ 0 မပေါ်စေရ
  • Checkout button က item ရှိမှသာ ပေါ်ရမယ်
CartSummary.jsx
function CartSummary({ status, items, error }) {
  // TODO: early return for 'loading'
  // TODO: early return for 'error' (show the error text)

  return (
    <section>
      <h2>Your cart</h2>
      {/* TODO: empty message OR "N item(s) in your cart" */}
      {/* TODO: bulk discount line for 3+ items */}
      {/* TODO: Checkout button only when there are items */}
    </section>
  );
}

export default function App() {
  // TODO: render one CartSummary for each of the four states
}

ရလဒ်: Loading your cart... Could not load the cart: Network timeout Your cart / Your cart is empty. (button မပါ) Your cart / 3 item(s) in your cart / Bulk discount: 10% / Checkout button

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

Discount ကို const discount = count >= 3 ? 10 : 0; လို့ တွက်ပြီး {discount > 0 && ...} လို့ စစ်ပါ။ {discount && ...} လို့ ရေးရင် item နည်းတဲ့ cart မှာ 0 ပေါ်လာပါမယ်။

အဖြေ ကြည့်ရန်
CartSummary.jsx
function CartSummary({ status, items, error }) {
  if (status === 'loading') {
    return <p>Loading your cart...</p>;
  }
  if (status === 'error') {
    return <p role="alert">Could not load the cart: {error}</p>;
  }

  const count = items.length;
  const discount = count >= 3 ? 10 : 0;

  return (
    <section>
      <h2>Your cart</h2>
      {count === 0 ? <p>Your cart is empty.</p> : <p>{count} item(s) in your cart</p>}
      {discount > 0 && <p>Bulk discount: {discount}%</p>}
      {count > 0 && <button type="button">Checkout</button>}
    </section>
  );
}

export default function App() {
  return (
    <>
      <CartSummary status="loading" />
      <CartSummary status="error" error="Network timeout" />
      <CartSummary status="ready" items={[]} />
      <CartSummary status="ready" items={['pen', 'book', 'bag']} />
    </>
  );
}

Quick Quiz 1 / 3

const count = 0; ဆိုရင် <div>{count && <p>New!</p>}</div> က ဘာ render လုပ်မလဲ။

Quick Quiz 2 / 3

unread > 0 ဖြစ်မှ badge ပြပြီး ဘယ်တော့မှ မလိုအပ်တဲ့ character မထွက်စေချင်ရင် ဘယ်ဟာကို ရေးမလဲ။

Quick Quiz 3 / 3

Data loading ဖြစ်နေစဉ် spinner ပဲ ပြပြီး ကျန်တဲ့ UI ကို လုံးဝ မ render စေချင်ရင် ဘယ်နည်းက အရှင်းဆုံးလဲ။

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

  • Condition rendering က JavaScript ရိုးရိုးပါ: if + early return, ? :, && နဲ့ JSX variable။
  • Screen တစ်ခုလုံး ပြောင်းရင် early return၊ နှစ်ခုထဲက တစ်ခုဆို ternary၊ ပြမလား မပြဘူးလား ဆို && ကို သုံးပါ။
  • null, undefined, true, false က ဘာမှ မပြပေမယ့် 0 နဲ့ NaN က text အဖြစ် ပေါ်ပါတယ် — && ရဲ့ ဘယ်ဘက်ကို boolean ဖြစ်အောင် ရေးပါ။
  • Hook တွေကို early return မတိုင်ခင် ခေါ်ပြီး loading, error, empty, ready လေးမျိုးစလုံးကို ဒီဇိုင်းဆွဲပါ။

React Documentation — React

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

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

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

Conditional Rendering | Thuta Learning