Utilizarea condițiilor în JSX
Să facem astfel încât, în funcție de
conținutul constantei show pe ecran
să fie afișat fie un text, fie altul:
function App() {
let text;
const show = true;
if (show) {
text = 'text1';
} else {
text = 'text2';
}
return <div>
{text}
</div>;
}
Se poate face astfel încât în variabilă să fie stocat nu textul, ci eticheta:
function App() {
let text;
const show = true;
if (show) {
text = <p>text1</p>;
} else {
text = <p>text2</p>;
}
return <div>
{text}
</div>;
}