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

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.

主站蜘蛛池模板: 英超| 宁晋县| 屏东市| 崇义县| 巴林左旗| 栾城县| 广汉市| 长丰县| 临海市| 航空| 南江县| 凌源市| 罗平县| 河津市| 萨嘎县| 丹巴县| 北宁市| 光泽县| 灵山县| 天峻县| 福建省| 嵩明县| 马关县| 静宁县| 靖边县| 台中县| 定兴县| 中卫市| 卢龙县| 皮山县| 平果县| 新津县| 故城县| 方山县| 涞水县| 那坡县| 南皮县| 手机| 云龙县| 山丹县| 乌兰察布市|