Skip to content

Instantly share code, notes, and snippets.

@Elshaman
Created April 8, 2023 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elshaman/1cd077483e601ca73d27b6b4b8bbfad3 to your computer and use it in GitHub Desktop.
Save Elshaman/1cd077483e601ca73d27b6b4b8bbfad3 to your computer and use it in GitHub Desktop.
Conditionals in React
function App() {
const title:string = "Blog post";
const loading: boolean = false;
const showComments: boolean = true;
let myArray: Array<Post> = [{
id: 1,
text: "Carlos"
},
{
id: 2,
text: "Zarta"
}
];
if(loading) return <h1>Loading</h1>
// @ts-ignore
return (
<div>
<h1>{title}</h1>
{ // aqui comienza el condicional, con operador ternario }
{ //version apta para cuando se quiere mostrar un pedazo de vista u otro en virtud del valor de una variable}
{showComments ? (
<>
{myArray.map((post, index)=> (
<h1> { post.text} </h1>
) )})
</> ): ('no')}
</div>
);
}
export default App;
function App() {
const title:string = "Blog post";
const loading: boolean = false;
const showComments: boolean = true;
let myArray: Array<Post> = [{
id: 1,
text: "Carlos"
},
{
id: 2,
text: "Zarta"
}
];
if(loading) return <h1>Loading</h1>
// @ts-ignore
return (
<div>
<h1>{title}</h1>
{ // aqui comienza el condicional, con operador booleano && }
{ //version apta para cuando se quiere mostrar un pedazo de vista unicamente en virtud del valor de una variable}
{showComments && (
<>
{myArray.map((post, index)=> (
<h1> { post.text} </h1>
) )})
</> )}
</div>
);
}
export default App;
function App() {
const title:string = "Blog post";
const loading: boolean = false;
const showComments: boolean = false;
let myArray: Array<Post> = [{
id: 1,
text: "Carlos"
},
{
id: 2,
text: "Zarta"
}
];
const commentBlock = (
<>
{myArray.map((post, index)=> (
<h1> { post.text} </h1>
) )})
</> )
if(loading) return <h1>Loading</h1>
// @ts-ignore
return (
<div>
<h1>{title}</h1>
{showComments && commentBlock }
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment