[React MAIN CONCEPTS] 2. Introducing JSX
Introducing JSX
Consider this variable declaration:
1 | const element = <h1>Hello, world!</h1>; |
It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.
JSX produces React “elements”. We will explore rendering them to the DOM in the next section.
Why JSX?
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.
React doesn’t require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.
Embedding Expressions in JSX
In the example below, we declare a variable called name and then use it inside JSX by wrapping it in curly braces:
1 | const name = 'Josh Perez'; |
You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2
, user
.firstName
, or formatName(user)
are all valid JavaScript expressions.
In the example below, we embed the result of calling a JavaScript function, formatName(user)
, into an
element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const user = {
firstName: 'Harper',
lastName: 'Perez'
};
const element = (
<h1>
Hello, {formatName(user)}!
</h1>
);
ReactDOM.render(
element,
document.getElementById('root')
);
1 | function formatName(user) { |
Try it on CodePen - https://reactjs.org/redirect-to-codepen/introducing-jsx
JSX is an Expression Too
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
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:
1 | function getGreeting(user) { |
Specifying Attributes with JSX
You may use quotes to specify string literals as attributes:
1 | const element = <div tabIndex="0"></div>; |
You may also use curly braces to embed a JavaScript expression in an attribute:
1 | const element = <img src={user.avatarUrl}></img>; |
Don’t put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (for string values) or curly braces (for expressions), but not both in the same attribute.
Warning:
Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.
For example, class
becomes className
in JSX, and tabindex
becomes tabIndex
.
Specifying Children with JSX
If a tag is empty, you may close it immediately with />
, like XML:
1 | const element = <img src={user.avatarUrl} />; |
JSX tags may contain children:
1 | const element = ( |
JSX Prevents Injection Attacks
It is safe to embed user input in JSX:
1 | const title = response.potentiallyMaliciousInput; |
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) - https://en.wikipedia.org/wiki/Cross-site_scripting attacks.
JSX Represents Objects
Babel compiles JSX down to React.createElement()
calls.
These two examples are identical:
1 | const element = ( |
1 | const element = React.createElement( |
React.createElement()
performs a few checks to help you write bug-free code but essentially it creates an object like this:
1 | // Note: this structure is simplified |
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.
References
[1] Introducing JSX – React - https://reactjs.org/docs/introducing-jsx.html
[2] React – A JavaScript library for building user interfaces - https://reactjs.org/
[3] Babel · The compiler for next generation JavaScript - https://babeljs.io/