Learn React_ JSX Cheatsheet _ Codecademy
Learn React_ JSX Cheatsheet _ Codecademy
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
const heading = <h1 className="large-
translated 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>
const update = (
<div>
{unreadMessages.length > 0 &&
<h1>
You have {unreadMessages.length}
unread messages.
</h1>
}
</div>
);
<ul>{listItems}</ul>
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" .
ReactDOM JavaScript library
The JavaScript library react-dom/client contains the import React from 'react';
createRoot() method, which is used to create a React
import { createRoot } from 'react-
root at the HTML element used as an argument. The
React root renders JSX elements to the DOM by taking dom/client';
a JSX expression, creating a corresponding tree of DOM
nodes, and adding that tree to the DOM.
const container =
The code example begins by creating a React root at
the HTML element with the id app and storing it in document.getElementById('app');
root . Then, using root ‘s render() method, the JSX const root = createRoot(container);
used as an argument is rendered.
root.render(<h1>This is an example.
</h1>);
Print Share