Zastosowanie warunków w JSX
Sprawmy, aby w zależności od zawartości
stałej show na ekranie wyświetlił się
albo jeden, albo drugi tekst:
function App() {
let text;
const show = true;
if (show) {
text = 'text1';
} else {
text = 'text2';
}
return <div>
{text}
</div>;
}
Można zrobić tak, aby w zmiennej przechowywany był nie tekst, a tag:
function App() {
let text;
const show = true;
if (show) {
text = <p>text1</p>;
} else {
text = <p>text2</p>;
}
return <div>
{text}
</div>;
}