Skip to content

Instantly share code, notes, and snippets.

@jx13xx
Created February 2, 2022 01:59
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 jx13xx/e29103b6ae2cb7db278f6dbc68b27d67 to your computer and use it in GitHub Desktop.
Save jx13xx/e29103b6ae2cb7db278f6dbc68b27d67 to your computer and use it in GitHub Desktop.
Conditionals in JSX
const showComments = true;
function App() {
return (
<div className='container'>
<h1>Blog Post</h1>
{showComments ? (<div className='comments'>
<h3>Comments ({comments.length})</h3>
<ul>
{comments.map((comment, index) => (
<li key={index}>{comment.text}</li>
))}
</ul>
</div>) : 'no'}
// OR
{showComments && (<div className='comments'>
<h3>Comments ({comments.length})</h3>
<ul>
{comments.map((comment, index) => (
<li key={index}>{comment.text}</li>
))}
</ul>
</div>)}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment