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

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.

主站蜘蛛池模板: 那坡县| 任丘市| 和林格尔县| 怀宁县| 揭阳市| 历史| 郸城县| 蓝田县| 屯留县| 伊春市| 界首市| 许昌市| 和顺县| 阿拉善盟| 华容县| 洛川县| 合作市| 军事| 平乡县| 静乐县| 东乡族自治县| 怀柔区| 论坛| 秦安县| 筠连县| 邹平县| 明溪县| 上饶市| 平顺县| 沐川县| 广昌县| 佛学| 綦江县| 讷河市| 柯坪县| 江油市| 固镇县| 龙泉市| 阜新| 遂平县| 恭城|