官术网_书友最值得收藏!

  • Getting Started with React
  • Doel Sengupta Manu Singhal Danillo Corvalan
  • 547字
  • 2021-07-16 11:09:21

What is JSX?

JSX is a JavaScript syntax extension that looks similar to XML. It is used to build UI components in ReactJS. It's very similar to HTML with some subtle differences. JSX extends JavaScript in such a way that you can easily build ReactJS components with the same understanding as building HTML pages. It's commonly mixed with your JavaScript code because ReactJS thinks about UI in a different way. This paradigm will be explained later in the chapter.

It's wrong to say that you are mixing up your HTML with JavaScript. As already said, JSX extends JavaScript. Actually, you're not writing HTML tags, but you're writing JavaScript objects in the JSX syntax. Of course, it has to be transformed into plain JavaScript first.

When you write this example:

var HelloWorld = React.createClass({
  render: function () {
    return <h1>Hello World from Learning ReactJS</h1>;
  }
});

It's transformed into this:

var HelloWorld = React.createClass({
  render: function () {
    return React.createElement('h1', null, "Hello World from Learning ReactJS");  }
});

This transformer script file detects JSX notations and transforms them into plain JavaScript notations. These scripts and tools should never be placed in a production environment because it would be painful for the server to transform the script on every request. For the production environment, we should provide the transformed file. We will be covering that process later in this chapter.

As discussed in Chapter 1, Getting Started with ReactJS, note the following:

  • ReactElement is the primary API of React. ReactElement has four properties: type, props, key, and ref.
  • ReactElement has no methods of itself, and nothing has been defined on the prototype also.
  • ReactElement objects can be created by calling the React.createElement method.
  • In the highlighted code mentioned earlier, we can see that the first argument for the React.createElement method is creating an h1 element, with properties being passed as null and the actual content of the h1 element being the string Hello World from Learning ReactJS
  • ReactElements are passed into DOM in order to create a new tree in DOM.
  • ReactElements are named virtual DOM and are not the same as DOM elements. Details of virtual DOM will be discussed in later chapters.
  • As per the official React documentation (

Let's check our previous example again when we didn't use the JSX syntax:

React.createElement('h1', null, "Hello World from Learning ReactJS");

This code is creating an h1 element. Think about it being like creating an element through JavaScript with the document.createElement() function, which makes the code very readable.

JSX is not mandatory, but it's highly recommended. It is painful to create large and complex components using JavaScript. For example, if we want to create nested elements using JSX, we would need to do the following:

var CommentList = React.createClass({
  render: function() {
    return (
        <ul>
            <li>ReactJS</li>
            <li>JSX</li>
            <li>
                <input type="text" />
                <button>Add</button>
            </li>
        </ul>
    );
  }
});

However, using plain JavaScript ReactJS objects, it would look like this:

var CommentList = React.createClass({displayName: "CommentList",
  render: function() {
    return (
        React.createElement("ul", null, 
            React.createElement("li", null, "ReactJS"), 
            React.createElement("li", null, "JSX"), 
            React.createElement("li", null, 
                React.createElement("input", {type: "text"}), 
                React.createElement("button", null, "Add")
            )
        )
    );
  }
});

We can see a big scary component that might grow in case of more complex logic. Such complex components are difficult to maintain and understand.

主站蜘蛛池模板: 左权县| 通许县| 敦煌市| 肃宁县| 灵石县| 岳池县| 睢宁县| 新宁县| 西盟| 弋阳县| 建水县| 海淀区| 阳谷县| 澜沧| 哈尔滨市| 中宁县| 黑山县| 聊城市| 漯河市| 宜昌市| 北海市| 都江堰市| 读书| 准格尔旗| 六安市| 三门县| 云霄县| 高平市| 太和县| 金平| 名山县| 庆云县| 松阳县| 长顺县| 保山市| 凌源市| 慈利县| 岢岚县| 深圳市| 阜新市| 师宗县|