React Tutorials
React ES6
React Hooks
JSX stands for JavaScript XML.
JSX allows us to write HTML in React
JSX makes it easy to write and add HTML to React.
JSX allows us to write HTML features in JavaScript and place them in DOM without any means of createElement()
and / or appendChild()
.
JSX converts HTML tags into responsive objects.
You do not have to use JSX, but JSX makes it easy to write React applications.
Here are two examples. The first one does not use JSX and the second does not:
JSX
const myelement = <h1>I Love JSX!</h1>;
ReactDOM.render(myelement, document.getElementById('root'));
Without JSX
const myelement = React.createElement('h1', {}, 'I do not use JSX!');
ReactDOM.render(myelement, document.getElementById('root'));
As you can see from the first example, JSX allows us to write HTML directly within a JavaScript code.
JSX is a JavaScript-based JavaScript language extension, and is translated into standard JavaScript during operation.
With JSX you can write expressions within the folded parentheses {}
.
An expression can be a React variable, or a location, or another valid JavaScript expression. JSX will extract a quote and return the result:
Execute the expression 5 + 5
:
const myelement = <h1>React is {5 + 5} times better with JSX</h1>;
To write HTML in multiple lines, insert HTML inside brackets:
Create a list with three list items:
const myelement = (
<ul>
<li>Apples</li>
<li>Bananas</li>
<li>Cherries</li>
</ul>
);
HTML code should be wrapped with ONE high-quality item.
So if you like to write two paragraphs, you have to put them inside the parent object, like the div
element.
Wrap two paragraphs inside one DIV element:
const myelement = (
<div>
<p>I am a paragraph.</p>
<p>I am a paragraph too.</p>
</div>
);
JSX will make a mistake if HTML is incorrect, or if HTML misses the parent feature.
Alternatively, you can use "episode" to wrap multiple lines. This will prevent you from adding unnecessarily additional nodes to the DOM.
Episode looks like a blank HTML tag: <>>
Wrap two paragraphs inside a fragment:
const myelement = (
<>
<p>I am a paragraph.</p>
<p>I am a paragraph too.</p>
</>
);
JSX complies with XML rules, so HTML components must be properly encrypted.
Close empty elements with />
const myelement = <input type="text" />;
JSX will crash if HTML is not properly encrypted.
The class
attribute is the most widely used attribute in HTML, but since JSX translates as JavaScript, and the class
keyword is a JavaScript-protected word, you are not allowed to use it in JSX.
Use the attribute className
instead.
JSX solved this by using className
instead. When JSX is provided, it translates className
attributes into class
attributes.
Use attribute className
instead of class
in JSX:
const myelement = <h1 className="myclass">Hello World</h1>;
React supports if
statements, but not inside JSX.
In order to use the conditional statements in the JSX, you must state that if
the statements are outside the JSX, or you can use the ternary expression instead:
Write if
statements are outside the JSX code:
Write "Hello" if x
is less than 10, otherwise "Goodbye":
const x = 5;
let text = "Goodbye";
if (x < 10) {
text = "Hello";
}
const myelement = <h1>{text}</h1>;
Instead, use the following expressions:
Write "Hello" if x
is less than 10, otherwise "Goodbye":
const x = 5;
const myelement = <h1>{(x) < 10 ? "Hello" : "Goodbye"}</h1>;
Note that in order to embed a JavaScript expression within JSX, JavaScript must be wrapped in folded parentheses, {}
.