Created
April 8, 2023 19:44
-
-
Save Elshaman/1cd077483e601ca73d27b6b4b8bbfad3 to your computer and use it in GitHub Desktop.
Conditionals in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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