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

Introduction to JSX

We have been using JSX so far, but what does it mean? JSX stands for JavaScript extension. How can it be an extension?

As you probably know, ECMAScript is also an extension to JavaScript (kind of). ECMAScript transpiles to JavaScript. What does this mean? It means that it just transforms ECMAScript code into valid JavaScript code. JavaScript misses out on many features that we like from ECMAScript, such as arrow functions, classes, and destructuring operators.

JSX works the same way. JSX is being transpiled to JavaScript, and its main feature is creating React elements based on the markup you write.
Could we use only JavaScript? Yes. Is it worth it? Most likely not.

Let's check this out in action. This is JSX and ECMAScript:

export default () => <Text style={{marginTop: 30}}>Example Text!</Text>

Now, compare this to pure JavaScript:

export default function() {
return React.createElement(
Text,
{style: {marginTop: 30}},
'Example Text!'
);
}

There's no doubt that the first code snippet is easier to read and understand.

Babel transpiles JSX to JavaScript. Check out this interactive tool so that you can play around and see what the output is in more complex examples:  https://goo.gl/RjMXKC.

主站蜘蛛池模板: 英德市| 且末县| 南昌县| 昂仁县| 横峰县| 同德县| 桐梓县| 通山县| 德化县| 申扎县| 潜江市| 高雄市| 夏邑县| 千阳县| 松原市| 长顺县| 梅河口市| 邮箱| 七台河市| 平顶山市| 海盐县| 九龙县| 镇江市| 子洲县| 调兵山市| 荔浦县| 洪洞县| 揭阳市| 武定县| 岢岚县| 宝应县| 大同县| 大丰市| 寻甸| 太谷县| 清涧县| 平昌县| 彭山县| 九寨沟县| 武陟县| 衡水市|