Introduction To React: JSX Cheatsheet - Codecademy
Introduction To React: JSX Cheatsheet - Codecademy
JSX
JSX className
In JSX, you can’t use the word class ! You have to // When rendered, this JSX
use className instead. This is because JSX
expression...
gets translated into JavaScript, and class is a
reserved word in JavaScript.
const heading = <h1 className="large-
When JSX is rendered, JSX className heading">Codecademy</h1>;
attributes are automatically rendered as class
attributes.
// ...will be rendered as this HTML
<h1 class="large-
heading">Codecademy</h1>
about:srcdoc Page 1 of 7
21/7/23, 11:44 PM
JSX conditionals
JSX does not support if/else syntax in embedded // Using ternary operator
JavaScript. There are three ways to express
conditionals for use with JSX elements:
const headline = (
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>
);
about:srcdoc Page 2 of 7
21/7/23, 11:44 PM
about:srcdoc Page 3 of 7
21/7/23, 11:44 PM
<ul>{listItems}</ul>
about:srcdoc Page 4 of 7
21/7/23, 11:44 PM
about:srcdoc Page 5 of 7
21/7/23, 11:44 PM
JSX attributes
The syntax of JSX attributes closely resembles that const example = <h1 id="example">JSX
of HTML attributes. In the block of code, inside of
Attributes</h1>;
the opening tag of the <h1> JSX element, we see
an id attribute with the value "example" .
about:srcdoc Page 6 of 7
21/7/23, 11:44 PM
Print Share
about:srcdoc Page 7 of 7