Introduction To React - JSX Cheatsheet
Introduction To React - JSX Cheatsheet
JSX
JSX className
In JSX, you can’t use the word class ! You have to use // When rendered, this JSX expression...
className instead. This is because JSX gets translated
const heading = <h1 className="large-
into JavaScript, and class is a reserved word in
JavaScript. heading">Codecademy</h1>;
When JSX is rendered, JSX className attributes are
automatically rendered as class attributes. // ...will be rendered as this HTML
<h1 class="large-heading">Codecademy</h1>
JSX conditionals
JSX does not support if/else syntax in embedded // Using ternary operator
JavaScript. There are three ways to express conditionals
const headline = (
for use with JSX elements:
1. a ternary within curly braces in JSX <h1>
2. an if statement outside a JSX element, or { age >= drinkingAge ? 'Buy Drink' :
3. the && operator.
'Do Teen Stuff' }
</h1>
);
const update = (
<div>
{unreadMessages.length > 0 &&
<h1>
You have {unreadMessages.length}
unread messages.
</h1>
}
</div>
);
<ul>{listItems}</ul>
Print Share