JSX에서 조건 적용하기
상수 show의 내용에 따라
화면에 다른 텍스트가 표시되도록 해봅시다:
function App() {
let text;
const show = true;
if (show) {
text = 'text1';
} else {
text = 'text2';
}
return <div>
{text}
</div>;
}
변수에 텍스트 대신 태그를 저장하도록 할 수도 있습니다:
function App() {
let text;
const show = true;
if (show) {
text = <p>text1</p>;
} else {
text = <p>text2</p>;
}
return <div>
{text}
</div>;
}