22 Jun 2017

Basic JSX

ReactJs 0 Comment

JSX is a preprocessor step that adds XML syntax to JavaScript. You can definitely use React without JSX but JSX makes React a lot more elegant.

Just like XML, JSX tags have a tag name, attributes, and children. If an attribute value is enclosed in quotes, the value is a string. Otherwise, wrap the value in braces and the value is the enclosed JavaScript expression.

Take a look at the following line of code:

What kind of weird hybrid code is that? Is it JavaScript? HTML? Or something else.

It seems like it must be JavaScript, since it starts with var = and ends with ;. If you tried to run that in an HTML file, it wouldn’t work.

However, the code also contains <h1>Hello world</h1>, which looks exactly like HTML. That part wouldn’t work if you tried to run it in a JavaScript file.

What’s going on?
Take another look at the line of code that you wrote.

Does this code belong in a JavaScript file, an HTML file, or somewhere else?

The answer is…a JavaScript file! Despite what it looks like, your code doesn’t actually contain any HTML at all.

The part that looks like HTML, <h1>Hello world</h1>, is something called JSX.
JSX is a syntax extension for JavaScript. It was written to be used with React. JSX code looks a lot like HTML.

What does “syntax extension” mean?
In this case, it means that JSX is not valid JavaScript. Web browsers can’t read it!
If a JavaScript file contains JSX code, then that file will have to be compiled. That means that before the file reaches a web browser, a JSX compiler will translate any JSX into regular JavaScript.

JSX elements can have attributes, just like HTML elements can.
A JSX attribute is written using HTML-like syntax: a name, followed by an equals sign, followed by a value. The value should be wrapped in quotes, like this:

Here are some JSX elements with attributes:

A single JSX element can have many attributes, just like in HTML:

You can nest JSX elements inside of other JSX elements, just like in HTML.

Here’s an example of a JSX <h1> element, nested inside of a JSX <a> element:

To make this more readable, you can use HTML-style line breaks and indentation:

If a JSX expression takes up more than one line, then you should wrap the multi-line JSX expression in parentheses. This looks strange at first, but you get used to it:

Nested JSX expressions can be saved as variables, passed to functions, etc., just like non-nested JSX expressions can! Here’s an example of a nested JSX expression being saved as a variable:

There’s a rule that we haven’t mentioned: a JSX expression must have exactly one outermost element.
In other words, this code will work:

But this code will not work:

The first opening tag and the final closing tag of a JSX expression must belong to the same JSX element!
It’s easy to forget about this rule, and end up with errors that are tough to diagnose.
If you notice that a JSX expression has multiple outer elements, the solution is usually simple: wrap the JSX expression in a <div></div>.

You’ve learned how to write JSX elements! Now it’s time to learn how to render them.
To render a JSX expression means to make it appear onscreen.

App.js:

Let’s examine the code that you just wrote:

You can see something called ReactDOM. What’s that?
ReactDOM is the name of a JavaScript library. This library contains several React-specific methods, all of which deal with the DOM in some way or another.

We’ll talk more later about how ReactDOM got into your file. For now, just understand that it’s yours to use.

Move slightly to the right, and you can see one of ReactDOM’s methods: ReactDOM.render.
ReactDOM.render is the most common way to render JSX. It takes a JSX expression, creates a corresponding tree of DOM nodes, and adds that tree to the DOM. That is the way to make a JSX expression appear onscreen.

Move to the right a little more, and you come to this expression:

This is the first argument being passed to ReactDOM.render. ReactDOM.render’s first argument should be a JSX expression, and it will be rendered to the screen.
Move to the right a little more, and you will see this expression:

You just learned that ReactDOM.render makes its first argument appear onscreen. But where on the screen should that first argument appear?
The first argument is appended to whatever element is selected by the second argument.

Index.html:

That element acted as a container for ReactDOM.render’s first argument! At the end of the previous exercise, this appeared on the screen:

 

ReactDOM.render()’s first argument should evaluate to a JSX expression, it doesn’t have to literally be a JSX expression.

The first argument could also be a variable, so long as that variable evaluates to a JSX expression.

In this example, we save a JSX expression as a variable named toDoList. We then pass toDoList as the first argument to ReactDOM.render:

One special thing about ReactDOM.render is that it only updates DOM elements that have changed.

That means that if you render the exact same thing twice in a row, the second render will do nothing:

This is significant! Only updating the necessary DOM elements is a large part of what makes React so successful.

React accomplishes this thanks to something called the virtual DOM.

Congratulations! You’ve learned to create and render JSX elements! This is the first step towards becoming fluent in React.