Introduction to React_ JSX Cheatsheet _ Codecademy
Introduction to React_ JSX Cheatsheet _ Codecademy
JSX
JSX is a syntax extension of JavaScript. It’s used to import React from 'react';
create DOM elements which are then rendered in the
import { createRoot } from 'react-
React DOM.
A JavaScript file containing JSX will have to be dom/client';
compiled before it reaches a web browser. The code
block shows some example JavaScript code that will
const container =
need to be compiled.
document.getElementById('app');
const root = createRoot(container);
root.render(<h1>Render me!</h1>);
In the block of code we see the similarities between const title = <h1>Welcome all!</h1>
JSX syntax and HTML: they both use the angle bracket
opening and closing tags ( <h1> and </h1> ).
When used in a React component, JSX will be
rendered as HTML in the browser.
JSX attributes
The syntax of JSX attributes closely resembles that of const example = <h1 id="example">JSX
HTML attributes. In the block of code, inside of the
Attributes</h1>;
opening tag of the <h1> JSX element, we see an
id attribute with the value "example" .
JavaScript expressions may be embedded within JSX let expr = <h1>{10 * 10}</h1>;
expressions. The embedded JavaScript expression
// above will be rendered as <h1>100</h1>
must be wrapped in curly braces.
In the provided example, we are embedding the
JavaScript expression 10 * 10 within the <h1>
tag. When this JSX expression is rendered to the DOM,
the embedded JavaScript expression is evaluated and
rendered as 100 as the content of the <h1> tag.
The Virtual Dom
JSX className
In JSX, you can’t use the word class ! You have to // When rendered, this JSX expression...
use className instead. This is because JSX gets
const heading = <h1 className="large-
translated into JavaScript, and class is a reserved
heading">Codecademy</h1>;
word in JavaScript.
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>
In JSX, && is commonly used to render an element // All of the list items will display if
based on a boolean condition. && works best in
// baby is false and age is above 25
conditionals that will sometimes do an action, but
other times do nothing at all. const tasty = (
If the expression on the left of the && evaluates as <ul>
true, then the JSX on the right of the && will be <li>Applesauce</li>
rendered. If the first expression is false, however, then
{ !baby && <li>Pizza</li> }
the JSX to the right of the && will be ignored and not
rendered. { age > 15 && <li>Brussels Sprouts</li>
}
{ age > 20 && <li>Oysters</li> }
{ age > 25 && <li>Grappa</li> }
</ul>
);
JSX conditionals
JSX does not support if/else syntax in embedded // Using ternary operator
JavaScript. There are three ways to express
const headline = (
conditionals 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>
);
Embedding JavaScript code in JSX
Any text between JSX tags will be read as text content, <p>{ Math.random() }</p>
not as JavaScript. In order for the text to be read as
JavaScript, the code must be embedded between
curly braces { } . // Above JSX will be rendered something
like this:
<p>0.88</p>
When writing JSX, it’s common to set attributes using const introClass = "introduction";
embedded JavaScript variables.
const introParagraph = <p className=
{introClass}>Hello world</p>;
JSX .map() method
The array method map() comes up often in React. const strings = ['Home', 'Shop', 'About
It’s good to get in the habit of using it alongside JSX.
Me'];
If you want to create a list of JSX elements from a given
array, then map() over each element in the array,
returning a list item for each one. const listItems = strings.map(string =>
<li>{string}</li>);
<ul>{listItems}</ul>
Print Share