Thuta Learning
ရှာဖွေရန်
BasicWeb Developmentintermediate

Conditional Rendering

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

Conditional Rendering ဆိုတာ condition အလိုက် UI မတူအောင်ပြတာပါ။ Login ဝင်ထားရင် dashboard ပြ၊ မဝင်ထားရင် sign in button ပြ—ဒီလို app logic တွေကို React မှာ JavaScript condition နဲ့ရေးနိုင်ပါတယ်။

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" />;
}

`isLoggedIn` က true ဖြစ်ရင် welcome message ပြပြီး false ဖြစ်ရင် sign in message ပြပါတယ်။ ဒီ pattern ကို auth UI, empty state, loading state တွေမှာနေ့တိုင်းသုံးရပါတယ်။

You should see
`Welcome back, Sai!` ဆိုတဲ့ heading ပေါ်လာမယ်။

Info

Ternary operator ကို JSX ထဲမှာ `{condition ? trueUI : falseUI}` ပုံစံနဲ့ထည့်နိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Condition တွေများလွန်းတဲ့ JSX ကိုတစ်ကြောင်းတည်းထိုးရေးရင် ပြင်ရခက်ပါတယ်။ UI logic ကိုသီးခြား component ခွဲပါ။