Consider this variable declaration:
This funny tag syntax is neither a string nor HTML.
React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.
Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both logic and markup
Embedding Expressions in JSX
formatName(user), into an
This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:
Specifying Attributes with JSX
Attributes in JSX are specified in quotes.
Specifying Children with JSX
For every open JXS tag, there must be a corresponding closing JXS tag. If a tag is empty, you may close it immediately with />, like XML. The illustration below will clearify.
JSX tags may also contain other JSX tags as their children, as illustrated in the snippet below.
Preventing Injection Attacks with JSX
Embedding user input in JSX is completely safe, this is because by default React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent XSS (cross-site-scripting) attacks.
JSX Represents Objects
When JSX are transpiled, they compile down to React.createElement() calls. In React, these two examples are equal and identical
React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this:
These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date
In the next tutorial, we explore the act of rendering react elements on the DOM.