Verwendung von Bedingungen in JSX

Lassen Sie uns es so einrichten, dass je nach Inhalt der Konstante show entweder der eine oder der andere Text auf dem Bildschirm angezeigt wird:

jsxfunction App() {
  let text;
  const show = true;
  
  if (show) {
    text = 'text1';
  } else {
    text = 'text2';
  }
  
  return <div>
    {text}
  </div>;
}

Man kann es auch so machen, dass in der Variable nicht Text, sondern ein Tag gespeichert wird:

jsxfunction App() {
  let text;
  const show = true;
  
  if (show) {
    text = <p>text1</p>;
  } else {
    text = <p>text2</p>;
  }
  
  return <div>
    {text}
  </div>;
}