အခြေအနေအလိုက် တင်ဆက်မှုကို တုံ့ပြန်ပါ။
React တွင်၊ သင်သည် အစိတ်အပိုင်းများကို အခြေအနေအရ တင်ဆက်နိုင်သည်။
ဒီလိုလုပ်ဖို့ နည်းလမ်းများစွာရှိပါတယ်။
if
ထုတ်ပြန်ချက်
if
မည်သည့်အစိတ်အပိုင်းကို တင်ဆက်ရမည်ကို ဆုံးဖြတ်ရန် JavaScript အော်ပရေတာကို ကျွန်ုပ်တို့ အသုံးပြုနိုင်ပါသည် ။
ဥပမာ-
ဤအစိတ်အပိုင်းနှစ်ခုကို ကျွန်ုပ်တို့ အသုံးပြုပါမည်-
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>Goal!</h1>;
}
ဥပမာ-
ယခု၊ ကျွန်ုပ်တို့သည် အခြေအနေတစ်ခုအပေါ် အခြေခံ၍ တင်ဆက်ရန် မည်သည့်အစိတ်အပိုင်းကို ရွေးချယ်သည့် အခြားအစိတ်အပိုင်းကို ဖန်တီးပါမည်-
function Goal(props) {
const isGoal = props.isGoal;
if (isGoal) {
return <MadeGoal/>;
}
return <MissedGoal/>;
}
ReactDOM.render(
<Goal isGoal={false} />,
document.getElementById('root')
);
isGoal
ရည်ညွှန်းချက်ကို ပြောင်းကြည့်ပါ true
-
ဥပမာ-
ReactDOM.render(
<Goal isGoal={true} />,
document.getElementById('root')
);
လက်မှတ်ရယူပါ။
$95 စာရင်းသွင်းပါ။
Logical &&
Operator
React အစိတ်အပိုင်းတစ်ခုကို သတ်မှတ်ချက်အတိုင်း ပြန်ဆိုရန် နောက်တစ်နည်းမှာ &&
အော်ပရေတာကို အသုံးပြုခြင်းဖြစ်သည်။
ဥပမာ-
curly braces များကိုအသုံးပြုခြင်းဖြင့် JSX တွင် JavaScript အသုံးအနှုန်းများကို မြှုပ်နှံနိုင်သည်-
function Garage(props) {
const cars = props.cars;
return (
<>
<h1>Garage</h1>
{cars.length > 0 &&
<h2>
You have {cars.length} cars in your garage.
</h2>
}
</>
);
}
const cars = ['Ford', 'BMW', 'Audi'];
ReactDOM.render(
<Garage cars={cars} />,
document.getElementById('root')
);
အမှန်နှင့် ညီမျှ ပါ cars.length
က၊ နောက်မှ စကားရပ်သည် ပြန်ဆို &&
ပါမည်။
cars
array ကို ရှင်းထုတ်ကြည့်ပါ ။
ဥပမာ-
const cars = [];
ReactDOM.render(
<Garage cars={cars} />,
document.getElementById('root')
);
Ternary အော်ပရေတာ
အစိတ်အပိုင်းများကို သတ်မှတ်ချက်အတိုင်း render လုပ်ရန် အခြားသောနည်းလမ်းမှာ ternary operator ကိုအသုံးပြုခြင်းဖြင့်ဖြစ်သည်။
condition ? true : false
ဂိုးနမူနာသို့ ပြန်သွားပါမည်။
ဥပမာ-
အကယ်၍ ဖြစ်ပါက MadeGoal
အစိတ်အပိုင်း
ကို ပြန်ပေးပါ ၊ သို့မဟုတ် ပါက အစိတ်အပိုင်းကို ပြန်ပေးပါ ။isGoal
true
MissedGoal
function Goal(props) {
const isGoal = props.isGoal;
return (
<>
{ isGoal ? <MadeGoal/> : <MissedGoal/> }
</>
);
}
ReactDOM.render(
<Goal isGoal={false} />,
document.getElementById('root')
);
ပိုမိုလေ့လာရန်၊ ternary operator ကဏ္ဍကို ကြည့်ပါ။