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

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.

主站蜘蛛池模板: 遂昌县| 钦州市| 鄂托克前旗| 攀枝花市| 罗甸县| 石泉县| 烟台市| 阳城县| 石嘴山市| 永丰县| 平陆县| 甘孜县| 建宁县| 青龙| 高邮市| 平南县| 买车| 镇远县| 景谷| 睢宁县| 惠州市| 东山县| 武川县| 阳泉市| 工布江达县| 巴东县| 利辛县| 临城县| 五原县| 开封市| 台湾省| 即墨市| 讷河市| 二连浩特市| 阿拉善盟| 轮台县| 于都县| 杭锦旗| 河北区| 北流市| 满洲里市|