As mentioned earlier, even though JSX looks like HTML, there are differences between them. Let’s look at some of these differences.
Let’s look at some more examples and reasons why JSX cannot be considered HTML.
In this segment, you learned the following differences between HTML and JSX:
1. Adjacent JSX elements must be wrapped in an enclosing tag
While returning JSX from a function or a class, you cannot return multiple elements. You can return only a single element. This is the reason why you need to encompass all children elements within a parent element and then, return this parent element. In case you fail to do this, you’ll get a syntax error saying, “Adjacent JSX elements must be wrapped in an enclosing tag”. However, in HTML, you can return as many DOM elements as you want. You do not have any rule of returning a single element.
Note that with the introduction to React 16, one can return an array consisting of multiple elements existing at the same level. These elements are separated from each other using a comma. Thus, one can write the following code snippet, which works fine in React 16:
return [ <div> Phone Directory </div>, <button>Add</button>, <div> <span>Name</span> <span>Phone</span> </div> ]
However, when React released its v16.2, it introduced Fragment, which allows you to return multiple elements. You can reference Fragment, as mentioned below:
import { Fragment } from 'react';
After including Fragment as a named import from the ‘react’ library, you can wrap multiple elements inside it. At last, the entire Fragment enclosing multiple elements can be returned. Thus, you can simply write the following code snippet:
return ( <Fragment> <div> Phone Directory </div> <button>Add</button> <div> <span>Name</span> <span>Phone</span> </div> </Fragment> )
2. Closing tag required
In JSX, you need to close both types of tags — opening-closing tags as well as self-closing tags. For an opening tag, you need to explicitly write a closing tag at the end. For a self-closing tag, you need to put a forward slash before the closing angular bracket. If you fail to do this, you’ll get a syntax error saying, “Expected corresponding JSX closing tag for <br>”.
However, in HTML, the browser sometimes takes care of closing the tags by itself. Don’t believe it? Try creating a simple webpage (HTML page) in which you write a paragraph tag inside the body tag. This paragraph consists of the text “Hello World”. Don’t close this paragraph tag. What I’m talking about looks something like this:
<p>Hello World
Now, run this on your browser. What do you see? Amazed?
3. JSX properties are not similar to HTML attributes
Some attributes that you use in HTML cannot be used as JSX properties. You can see the entire list of such attributes here. This is due to the reason that all of the JSX code gets converted to JavaScript code at the end. You know that JavaScript has its own set of keywords. If you try to write these keywords or reserved words as JSX properties, it gets confusing to identify when the word is being used as a JavaScript keyword (or reserved word) and when it is being used as a JSX property. To make this distinction, use alternative keywords in JSX for those HTML attributes, which exist in JavaScript language.
4. Case sensitiveness
React ‘reacts’ to cases that you use! It doesn’t allow you to write something in any case that you want. On the other hand, HTML syntax is not case sensitive. You can choose to write the div tag as <DIV>, <div> or <Div>. Well, can you do the same in React? A big NO.
Are you wondering why JSX is case sensitive? Do you remember me telling you that the React code (JSX code) gets converted to plain JavaScript at the end? Also, can you recall that JavaScript is case sensitive? I believe you have connected the dots and guessed the logic by now.
At this stage, commit your code with “Created Skeleton of Home Page” as the commit message. You can view the code diff here.